Load packages and graphics
::p_load(sf,sp,stringr,readxl,dplyr,leaflet,leaflet.minicharts)
pacman
# leaflet world map url
<- "http://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}"
tilesURL
# Geo-Spatial Data
= read_excel("../../data/BR.xlsx")
B $GDP = B$GDP %>%
Bstr_remove(",") %>% str_extract("^[0-9\\.]+") %>% as.numeric
= st_read("../../data/BRUFE250GC_SIR.shp")[B$sfid,]
G = cbind(B, st_centroid(G) %>% st_coordinates)
B names(B)[12:13] = c('lng','lat')
Import the location of customers and vendors
load("../../data/olist.rdata")
= left_join(I[,c(1,4)], S[,c(1,4)]) %>%
O2 left_join(O) %>%
left_join(C[,c(1,5)]) %>%
rename(from=seller_state, to=customer_state)
Calculate the amount of intra, export, and import data of states in Brazil
= sapply(unique(O2$to), function(s) c(
states Intra = sum(s == O2$from & s == O2$to, na.rm=T),
Import = sum(s != O2$from & s == O2$to, na.rm=T),
Export = sum(s == O2$from & s != O2$to, na.rm=T)
%>% t %>% data.frame %>%
)) mutate(
total = Intra + Import + Export,
stCode = unique(O2$to)) %>%
left_join(B[,c(3,12,13)]) %>% data.frame
Logistics of 10 southeastern provinces vs. Sao Paulo (SP)
= c("MG","PR","RJ","RS","SC","SP","DF","GO","ES","MS")
s10 = which(B$stCode %in% s10)
i10 = subset(states, stCode %in% s10) S10
Logistics between 10 southeastern provinces
= xtabs(~from+to, subset(O2, from%in%s10 & to%in%s10)); mx mx
to
from DF ES GO MG MS PR RJ RS SC SP
DF 61 22 37 108 8 33 110 23 26 336
ES 1 9 3 52 0 16 74 21 14 119
GO 30 5 39 100 9 16 62 14 11 141
MG 221 206 173 1709 71 415 1329 315 288 2964
MS 4 0 1 4 0 4 9 1 1 19
PR 178 117 94 938 66 827 1147 696 451 3410
RJ 114 111 127 622 43 234 1122 227 138 1518
RS 50 29 26 219 17 149 267 327 131 762
SC 75 54 77 483 31 324 540 331 311 1506
SP 1599 1647 1701 8703 561 3667 9688 4194 2749 36192
The logistics between the 10 southeastern provinces accounted for 86% of Brazil
sum(mx)/nrow(O2)
[1] 0.86839
The logistics of Sao Paulo (SP) accounts for 81% of Brazil
mean(O2$from=="SP" | O2$to=="SP", na.rm=T)
[1] 0.81313
Data for pie chart
= S10 %>% mutate(
S10 lngSP= B$lng[B$stCode == "SP"],
latSP = B$lat[B$stCode == "SP"]) %>%
arrange(stCode)
$fromSP = mx["SP",]
S10$toSP = mx[,"SP"]
S10= subset(S10, stCode != "SP") S9
Data for flow chart (n > 300)
= 300
N = O2 %>% filter(from%in%s10 & to%in%s10) %>%
d group_by(from, to) %>% summarise(
n = n(),
rDelivery = mean(order_status=="delivered"),
rDelay = mean(order_delivered_customer_date >
na.rm=T)
order_estimated_delivery_date, %>%
) filter(n >= N) %>%
arrange(desc(rDelay)) %>%
left_join(B[,c(3,12,13)], by=c("from" = "stCode")) %>%
left_join(B[,c(3,12,13)], by=c("to" = "stCode"))
`summarise()` regrouping output by 'from' (override with `.groups` argument)
= round(100*d$rDelay) %>% range # K
K = colorRampPalette(c('yellow','red'))(K[2]-K[1]+1)
cols $color = cols[ round(100*d$rDelay) - K[1] + 1 ]
d# table(d$color)
sprintf("min. flow (order.items) = %d", N)
[1] "min. flow (order.items) = 300"
sprintf("range of delay percentage (%%) = [%d, %d]", K[1], K[2])
[1] "range of delay percentage (%) = [2, 15]"
Logistics of Sao Paulo (SP)
= d %>% filter(to=="SP" | from=="SP", from!=to) %>% mutate_at(
dx vars(lng.x:lat.y), ~ifelse(from>to, ., .+0.2))
= leaflet(width = "100%", height = "800px") %>%
basemap addTiles(tilesURL) %>%
addPolylines(data=G[i10,], color="gray", weight=2, fillOpacity=0)
%>%
basemap addFlows(
$lng.x, dx$lat.x, dx$lng.y, dx$lat.y,
dxflow=dx$n, color = dx$color, opacity = 0.6,
maxThickness = 10) %>%
addMinicharts(
$lng, S10$lat, type = "pie",
S10chartdata = S10[,c("Intra","Import","Export")],
colorPalette = c("lightgray", "purple", "green"),
width = 100 * sqrt(S10$total) / sqrt(max(S10$total))
)