在ggplot上绘制shapefile

3
library(raster)
library(ggplot2)

map.shp <- getData('GADM', country='FRA', level=1)

plot(map.shp)
ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()

我该如何将 map.shp 作为小插图放在 ggplot 的右侧?
1个回答

6
我认为有多种方法可以实现这个目标,但一个简单的方法是使用 cowplotggdraw
我正在使用 sfgeom_sf,它们来自于 ggplot2 的开发版本,可在 devtools::install_github("hadley/ggplot2") 中获取。
require(ggplot2)
require(cowplot)
require(sf)
require(raster)

map.shp <- getData('GADM', country='FRA', level=1) %>% st_as_sf()



plot.cars <- ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
plot.map <- ggplot() + geom_sf(data=map.shp)

inset_map <- ggdraw() +
  draw_plot(plot.cars, 0, 0, 1, 1)+
  draw_plot(plot.map, 0.5, 0.52, 0.5, 0.4) 

enter image description here

抱歉,对我来说,SHP文件似乎渲染得非常慢,所以我不能太多地调整位置。像这样的东西是否符合您的要求?

是的,这就是我想要实现的目标。谢谢。 - 89_Simple

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接