是否有可能在ggplot中有一个图层作为ggmap图层的掩码? Here他们在ggmap顶部添加了一个国家多边形。与ggplot2的地图 - 创建一个框,填写一个框,不包括一个国家
我所寻找的是,该国将在一个层(阿尔法)一个“洞”涵盖一切,但该国。以上述示例的反面的方式。该答案的代码(透明度添加并更新为使用geom_cartogram)。
library(mapdata)
library(ggmap)
library(ggplot2)
library(ggalt)
# Get Peru map
Peru <- get_map(location = "Peru", zoom = 5, maptype="satellite")
# This is the layer I wish to put over the top
coast_map <- fortify(map("worldHires", fill = TRUE, plot = FALSE))
# Subset data for Peru
peru.coast <- subset(coast_map, region == "Peru")
# Draw a graphic
ggmap(Peru) +
geom_cartogram(data = peru.coast, map = peru.coast, aes(x = long, y = lat, map_id = region),
fill="white", color="grey", alpha=.1) +
xlim(-86, -68) +
ylim(-20, 0) +
labs(x = "Longitude", y = "Latitude") +
coord_map() +
theme_classic()
有没有办法填补除ggplot2中的多边形之外的所有内容?
难道你'''子取代你peru.coast变量(coast_map,区域!= “秘鲁”)'''? – Nancy
感谢建议但不是,你会将所有数据都记录在worldHires中,但这不包括海洋。解决方法可能是选择所有邻居和海洋n shapefile,但这很容易变得复杂。我真的在寻找一种补充或反向的方法。 – Lod
我不明白这里的结果应该是什么样子 - 你是否想在中间出现一个洞(即秘鲁被裁掉了)? – lukeA