问题
I have a map of dispersed parts in the Us. This is in the following question (that contains link to the data):
mapping by ggplot2 geom_polygon goes crazy after merging data
It was answered very well. Then I tried to add the US border line, therefore I added the geom_path to the answered code,but no result, it creates the same map just containing the dispersed areas.
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)
)
I have tried geom_polygon() and geom_maps(). no difference. What's the reason, and how can it be solved?
Thank you so much for your help!
回答1:
So, the problem was the differences in the projections. The us map was in a UTM system, giving Easting and Northing in meters, but named as long and lat. while the US map was in a lat/lon coordinating system. I transformed the shape just before the fortify line in the code as below:
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()
and this is the resulted map:
来源:https://stackoverflow.com/questions/48472163/us-border-line-is-not-added-to-geom-map-maps-of-dispersed-us-regions