我试图制作美国地图,以后可以添加交互式图层。然而,基于所创建的多边形,似乎存在订单问题;我已经用几种不同的方式命令它们,但它们似乎都没有正常工作。任何帮助将不胜感激。
library(ggplot2)
library(ggvis)
library(dplyr)
mdat<-map_data("state")
mdat %>%
arrange(group,order) %>%
ggvis(x=~long,y=~lat) %>%
layer_paths()
答案 0 :(得分:3)
我从我链接的帖子中略微抽了一个更完整的例子(其中包括对美国48个州连续使用更加合理的投影):
library(ggplot2)
library(ggvis)
library(dplyr)
library(rgdal)
library(httr)
# decent US shapefile and httr lets us only d/l when needed
stop_for_status(GET("http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json",
write_disk("us.geojson"), progress()))
states <- readOGR("us.geojson", "OGRGeoJSON")
states <- states[!states$NAME %in% c("Alaska", "Hawaii", "Puerto Rico", "District of Columbia"),]
states_aea <- spTransform(states, CRS("+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs"))
states_map <- fortify(states_aea, region="NAME")
states_map %>%
group_by(group) %>%
ggvis(~long, ~lat) %>%
layer_paths(strokeOpacity := 0.5, strokeWidth := 0.5) %>%
hide_axis("x") %>% hide_axis("y") %>%
set_options(width=960, height=600, keep_aspect=TRUE)