简体   繁体   English

带有ggvis软件包的地图中的多层

[英]Multiple layer in a map with ggvis package

I'm using ssplot multiple layers to plot color of provinces inside the regional borders in the italy map. 我正在使用多层绘图来绘制意大利地图中区域边界内各省的颜色。 This is my code: 这是我的代码:

library(raster)
library(latticeExtra)
setwd("c:\\temp")
gadm<-getData('GADM', country='Italy', level=2)
gadm2<-getData('GADM', country='Italy', level=1)

spplot(gadm, "ID_2",col=NA)+
layer(sp.polygons(gadm2, first=F))

Now I'd like to create a interactive map with the ggvis package. 现在,我想使用ggvis包创建一个交互式地图。 I started creating the main map with regional borders: 我开始创建具有区域边界的主地图:

library(ggvis)
library(ggplot2)
library(rgeos)

#convert in a data frame
map2 <- fortify(gadm2, region="NAME_1")
map<- fortify(gadm, region="NAME_2")


map2%>%
  ggvis(~long, ~lat) %>%
  group_by(group, id) %>%
  layer_paths(strokeOpacity:=0.5, stroke:="#7f7f7f") %>%
   hide_axis("x") %>% hide_axis("y") %>%
  set_options(width=400, height=600, keep_aspect=TRUE)

Now I don't understand how to plot another layer with the provincial colors. 现在我不明白如何用省色绘制另一层。

Two keys: layer_paths can take a fill argument to fill the inside of the polygon and you can specify a new dataset in the layer itself (similar to ggplot2). 两个键:layer_paths可以使用fill参数填充多边形的内部,并且您可以在图层本身中指定一个新的数据集(类似于ggplot2)。

map2%>%
  ggvis(~long, ~lat) %>%
  group_by(group, id) %>%
  layer_paths(data = map %>% group_by(group, id), 
              strokeWidth := 0, fill = ~id) %>%
  layer_paths() %>%
  hide_axis("x") %>% hide_axis("y") %>%
  set_options(width=400, height=600, keep_aspect=TRUE)

(I got rid of strokeOpacity:=0.5, stroke:="#7f7f7f" , because I thought that it made it unclear where the borders are, but you could add them back in if you want) (我摆脱了strokeOpacity:=0.5, stroke:="#7f7f7f" ,因为我认为不清楚边界在哪里,但是可以根据需要重新添加边界)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM