使用ggmap绘制形状文件:当形状文件大于ggmap时裁剪 [英] Plotting shape files with ggmap: clipping when shape file is larger than ggmap

查看:70
本文介绍了使用ggmap绘制形状文件:当形状文件大于ggmap时裁剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将ggmap与形状文件组合时,出现裁剪问题. Kahle和Wickham(2013:158)中的示例工作良好,因为ggmap的光栅图像覆盖了整个形状文件.以下是当我尝试在覆盖较小区域的ggmap图上绘制美国各州的形状文件时发生的情况的示例. ggmap显示了纽约市,我想将其与美国各州的边界重叠(仅作为示例).生成的地图没有任何意义.问题在于形状文件被剪切,并且ggplot连接未剪切的点.下面是代码.形状文件来自此处.我只是在这里显示最后一个情节.

I am having clipping problems when I try to combine ggmap with shape files. The example in Kahle and Wickham (2013: 158) works fine because the raster image from ggmap covers the entire shape file. Below is an example of what happens when I try to plot the shape file for U.S. states on a ggmap plot that covers a smaller area. The ggmap shows New York City and I want to overlay it with the borders for U.S. states (just as an example). The resulting map doesn't make any sense. The problem is that the shape file gets clipped and ggplot connects the unclipped points. Below is the code. The shape file is from here. I am just showing the last plot here.

我该如何解决这个问题?

How can I solve this problem?

path <- "PATH TO SHAPEFILE"
library("ggmap")
library("rgdal")

# shapefile
states <- readOGR(dsn = path, layer = "states")
states_df <- fortify(states)
# plot shapefile
plot(states, lwd = 0.1)
ggplot(states_df, aes(long, lat, group = group)) +
    geom_polygon(colour = "black", fill = NA, size = 0.1)


# combine ggmap with shapefile
map <- get_map("new york city", zoom = 10, source = "stamen")
ggmap(map, extent = "device")

ggmap(map, extent = "device") +
    geom_polygon(aes(long, lat, group=group), data = states_df, colour = "red", fill = NA, size = 1)

Kahle,David和Hadley Wickham. 2013年."Ggmap:使用ggplot2进行空间可视化." R Journal 5(1):144–61.

Kahle, David and Hadley Wickham. 2013. "Ggmap: Spatial Visualization with ggplot2." The R Journal 5(1):144–61.

推荐答案

这是我的尝试.我经常使用GADM shapefile,您可以使用raster包直接将其导入.我为NY,NJ和CT子集了形状文件.您可能最终不必这样做,但是减少数据量可能更好.当我绘制地图时,ggplot会自动删除ggmap图像的bbox之外的数据点.因此,我不必做任何其他工作.我不确定您使用了哪个shapefile.但是,GADM的数据似乎可以与ggmap图片配合使用.希望这对您有所帮助.

Here is my attempt. I often use GADM shapefiles, which you can directly import using the raster package. I subsetted the shape file for NY, NJ and CT. You may not have to do this in the end, but it is probably better to reduce the amount of data. When I drew the map, ggplot automatically removed data points which stay outside of the bbox of the ggmap image. Therefore, I did not have to do any additional work. I am not sure which shapefile you used. But, GADM's data seem to work well with ggmap images. Hope this helps you.

library(raster)
library(rgdal)
library(rgeos)
library(ggplot2)

### Get data (shapefile)
us <- getData("GADM", country = "US", level = 1)

### Select NY and NJ
states <- subset(us, NAME_1 %in% c("New York", "New Jersey", "Connecticut"))

### SPDF to DF
map <- fortify(states)

## Get a map
mymap <- get_map("new york city", zoom = 10, source = "stamen")


ggmap(mymap) +
geom_map(data = map, map = map, aes(x = long, y = lat, map_id = id, group = group))

如果您只想显示行,那么下面就是您想要的.

If you just want lines, the following would be what you are after.

ggmap(mymap) +
geom_path(data = map, aes(x = long, y = lat, group = group))

这篇关于使用ggmap绘制形状文件:当形状文件大于ggmap时裁剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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