将国家/地区多边形添加到地图框

时间:2019-09-02 12:17:17

标签: r plotly

我想重新创建一个具有国家多边形的Choropleth地图,例如县级Choropleth地图here。这就是它的结果。 enter image description here

我尝试对世界地图重复相同的步骤

library(ggplot2)
library(dplyr)
library(maps)
WorldData <- map_data('world') %>% filter(region != "Antarctica") %>% fortify

Country<-c("United States","Canada","France","Italy","Turkey","United States","Canada","France","Italy","Turkey")
Val<-c(50,67,89,567,9,50,67,89,567,9)
Name<-c('AD',"FGH","BGH","FGFG","EWRW",'ADy',"FGyH","BGyH","FGFyG","EyWRW")
Test<-data.frame(Country,Val,Name)
V1 <- aggregate(Val~Country,Test,sum)


colnames(WorldData)[5]<-"Country"
m2 <- data.frame(merge(V1,WorldData , by=c("Country"), all.x=T))


p <- m2 %>%
  group_by(group) %>%
  plot_mapbox(x = ~long, y = ~lat, color = ~Val1, colors = c('#ffeda0','#f03b20'),
              text = ~Country, hoverinfo = 'text', showlegend = FALSE) %>%
  add_polygons(
    line = list(width = 0.4)
  ) %>%
  add_polygons(fillcolor = ~Val1,
               line = list(color = 'black', width = 0.5),
               showlegend = FALSE, hoverinfo = 'none'
  ) %>%
  layout(
    xaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE),
    yaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE),
    mapbox = list(
      style = 'light',
      zoom = 4,
      center = list(lat = ~median(lat), lon = ~median(long))),
    margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0)
  )

p

但是我得到了

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为您只需要按照数据中的“订单”列对数据进行排序:

p <- m2 %>%
  arrange(order) %>%
  group_by(group) %>%
...
...

对我有用。