我在美国有一张分散的地图。这是在以下问题(包含数据的链接):
mapping by ggplot2 geom_polygon goes crazy after merging data
回答得非常好。然后我尝试添加美国边界线,因此我将geom_path添加到已回答的代码中,但没有结果,它创建了仅包含分散区域的相同地图。
library(ggplot2)
#library(tidyverse)
library(dplyr)
library(maps)
load("./data.rda")
usa <- map_data("usa")
shape_map <- tbl_df(fortify(shape, region="Name"))
colnames(shape_map) <- c("long", "lat", "order", "hole", "piece", "region", "group")
ggplot() +
geom_path(data = usa, aes(long, lat, group=group))+
geom_map(data=shape_map, map=shape_map, aes(long, lat, map_id=region)) +
geom_map(
data=filter(prop.test, season=="DJF"),
map=shape_map, aes(fill=prop.mega, map_id=megaregion)
)
我尝试过geom_polygon()和geom_maps()。没有不同。原因是什么,以及如何解决?
非常感谢你的帮助!
答案 0 :(得分:3)
所以,问题是预测的差异。美国地图是在UTM系统中,以米为单位给出Easting和Northing,但命名为long和lat。美国地图是在拉特/伦恩协调系统中。我在代码中的fortify行之前转换了 shape ,如下所示:
library(ggplot2)
library(tidyverse)
usa <- map_data("usa", )
shape <- spTransform(shape, CRS("+proj=longlat +datum=WGS84"))
shape_map <- fortify(shape, region="Name")
colnames(shape_map) <- c("long", "lat", "order", "hole", "piece", "region", "group")
prop.test <- proptest.result[which(proptest.result$variable=="Upward N"),]
ggplot() +
geom_map(
data=usa, map=usa, aes(long, lat, map_id=region),
color="#2b2b2b", fill="#00000000"
) +
geom_map(
data=shape_map, map=shape_map,
aes(long, lat, map_id=region)
) +
geom_map(
data=filter(prop.test, season=="DJF"),
map=shape_map, aes(fill=prop.mega, map_id=megaregion)
) +
viridis::scale_fill_viridis(direction=-1) +
coord_map("polyconic") +
ggthemes::theme_map()
这是结果地图: