【问题标题】:Plot shapefile on ggplot在 ggplot 上绘制 shapefile
【发布时间】:2018-11-11 22:00:40
【问题描述】:
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 的右侧作为小插图。

【问题讨论】:

    标签: r ggplot2 shapefile sp


    【解决方案1】:

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

    我正在使用sfgeom_sfggplot2 开发版devtools::install_github("hadley/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) 
    

    抱歉,shapefile 对我来说似乎渲染得很慢,所以无法过多地调整定位。你追求的是这样的东西吗?

    【讨论】:

    • 是的。这就是我想要达到的目标。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多