将ggplot2 coord_map()图表保存在shapefile中 [英] Save a ggplot2 coord_map() chart in shapefile

查看:123
本文介绍了将ggplot2 coord_map()图表保存在shapefile中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在CARTO(又称cartodb)中导出等高线图,因此我试图将此stat2density图表保存为地理数据文件格式(例如shapefile或geojson). 我可以使用ggsave将其保存在SVG中,但是将其转换为spdf或sf格式将非常有帮助.

i need to export a contour map in CARTO (aka cartodb), so i'm trying to save this stat2density chart in a geodata file format like shapefile or geojson. I'm able to save it in SVG with ggsave, but would be very helpful to convert it in a spdf or sf oblejct.

library(ggplot2)
library(ggmap)
data("crime")
crime<- head(crime,1000)

gg <- ggplot(aes(x = lon, y = lat), data=crime) + 
stat_density2d(aes(alpha=..level.., color=..level.., fill=..level..),geom='polygon', bins = 10, size=0.5) +
scale_color_gradient(low = "grey", high = "#444444", guide = F)+
scale_fill_gradient(low = "yellow", high = "red", guide = F)+
scale_alpha( guide = F)+
coord_map()+
ggthemes::theme_map()

有什么主意吗?

推荐答案

以下是结合了@hrbrmstr& amp;提出的想法的解决方案. @Rich Pauloo以及此问题的答案问题:

Here's a solution that incorporates the ideas proposed by @hrbrmstr & @Rich Pauloo above, as well as the answer to this question:

第1步.从ggplot对象中提取相关数据:

Step 1. Extract the relevant data from the ggplot object:

library(dplyr)

# return a list of data frames (each data frame contains coordinates for one contour);
# note that there may be multiple contours at the same alpha / colour / fill,
# hence the need to split by group rather than by these aesthetic mappings.
dg <- layer_data(gg) %>% 
  select(group, x, y) %>% 
  split(.$group) %>%
  lapply(function(d){d[,-1]})

第2步.将数据帧转换为SpatialPolygonsDataFrame对象,以传递给writeOGR:

Step 2. Convert the data frames into a SpatialPolygonsDataFrame object, to be passed to writeOGR:

library(sp)

# convert each data frame to a Polygon class object
polygons <- lapply(dg, Polygon) 

# convert each Polygon class object to Polygons class object
polygons <- lapply(seq_along(polygons), 
                   function(i){
                     Polygons(list(polygons[[i]]),
                              ID = names(dg)[i])
                   })

# convert list of Polygons class object to one SpatialPolygons object
polygons <- SpatialPolygons(polygons)

# convert SpatialPolygons object to SpatialPolygonsDataFrame object
polygons <- SpatialPolygonsDataFrame(polygons,
                                     data = layer_data(gg) %>% 
                                       select(group, alpha, colour, fill) %>% 
                                       unique(),
                                     match.ID = "group")

第3步.将SpatialPolygonsDataFrame对象另存为shapefile:

Step 3. Save the SpatialPolygonsDataFrame object as a shapefile:

rgdal::writeOGR(obj = polygons,
                dsn = getwd(),      # or wherever you wish to store it
                layer = "filename", # or whatever you wish to name it
                driver = "ESRI Shapefile")

在R中验证结果(我希望在单独的GIS程序中对此进行验证,但我在此计算机上未安装任何程序):

Verifying the results in R (I would have preferred to verify this in a separate GIS programme, but I don't have any installed on this computer):

# read the shapefile back into R
sp <- rgdal::readOGR(dsn = getwd(),
                     layer = "filename")

# fortify as a data frame
spdf <- left_join(broom::tidy(sp, region = "group"),
                  sp@data,
                  by = c("id" = "group"))

# plot
ggplot(spdf,
       aes(x = long, y = lat, group = group, alpha = alpha)) +
  geom_polygon(color = "black") +
  coord_map()

这篇关于将ggplot2 coord_map()图表保存在shapefile中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆