在ggplot上绘制shapefile

89_*_*ple 2 r shapefile ggplot2 r-sp

library(raster)
library(ggplot2)

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

plot(map.shp)
ggplot(mtcars, aes(x = hp, y = disp)) + geom_point()
Run Code Online (Sandbox Code Playgroud)

如何map.shp将ggplot的右侧作为小插图放置.

Chr*_*ris 6

我认为有多种方法可以实现这一目标,但一种简单的方法是使用cowplotggdraw:

我使用sf连同geom_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) 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

抱歉,shapefile似乎对我来说渲染速度很慢,所以不能过多地定位.你是这样的人吗?