R使用ggmap对象覆盖geom_polygon,进行空间文件转换

问题描述 投票:2回答:1

我已经在几个地方看到了这方面的例子,包括在Rjournal(https://journal.r-project.org/archive/2013-1/kahle-wickham.pdf)中的ggmap文章,并看到这个用于另一个演练 - https://markhneedham.com/blog/2014/11/17/r-ggmap-overlay-shapefile-with-filled-polygon-of-regions/

我遇到的问题只是实现这一点。这似乎是直截了当但我错过了一些东西。

我正在使用来自威斯康星州自然资源部的威斯康星州的形状文件(免费)https://data-wi-dnr.opendata.arcgis.com/datasets/8b8a0896378449538cf1138a969afbc6_3?geometry=-110.743%2C42.025%2C-68.93%2C47.48

这是代码:

library(rgdal)
shpfile <- readOGR(dsn = "[file path to the shapefile directory]", 
                   stringsAsFactors = FALSE ) 

我可以使用plot(shpfile)绘制shapefile。接下来,我将其转换为适合在ggplot中绘图的格式。许多例子使用“强化”但似乎已经被“整洁”所取代,“整洁”是“扫帚”包的一部分。 FWIW,我用fortify试过它并得到相同的结果。

library(broom)
library(ggplot2)
library(ggmap)

tidydta <- tidy(shpfile, group=group) 

现在我可以成功地将ggplot中的shapefile绘制为多边形

ggplot() +
  geom_polygon(data=tidydta, 
               mapping=aes(y=lat , x=long, group=group), 
               color="dark red", alpha=.2) +
  theme_void()

接下来,我使用ggmap检索背景地图。

wisc <- get_map(center = c(lon= -89.75, lat=44.75), zoom=7, maptype="toner")

问题是我无法将这些结合起来。我假设整洁的转换肯定有问题,或者我错过了一步。我收到一个错误:

in min(x):min没有非缺失参数;返回Inf

我认为这是因为我在某处有一个零长度矢量。

这是命令:

ggmap(wisc) +
  geom_polygon(aes(x=long, y=lat, group=group), 
               data=tidydta, 
               color="dark red", alpha=.2, size=.2) 

我已经使用geom_point成功地将地理编码点添加到地图中,但我坚持使用多边形。

谁能告诉我我做错了什么?

r ggplot2 spatial shapefile ggmap
1个回答
2
投票

shapefile使用的坐标系不是lat-lon。您应该在将其转换为ggplot的数据框之前对其进行转换。以下作品:

shpfile <- spTransform(shpfile, "+init=epsg:4326") # transform coordinates
tidydta2 <- tidy(shpfile, group=group) 

wisc <- get_map(location = c(lon= -89.75, lat=44.75), zoom=7)

ggmap(wisc) +
  geom_polygon(aes(x=long, y=lat, group=group), 
               data=tidydta2, 
               color="dark red", alpha=.2, size=.2) 

plot

为了将来参考,请通过将数据框打印到带有可见x / y轴标签的控制台/绘图来检查坐标值。如果数字与背景地图的边界框的数字大不相同(例如7e + 05与47),则可能需要进行一些转换。例如。:

> head(tidydta) # coordinate values of dataframe created from original shapefile
# A tibble: 6 x 7
     long     lat order hole  piece group id   
    <dbl>   <dbl> <int> <lgl> <chr> <chr> <chr>
1 699813. 246227.     1 FALSE 1     0.1   0    
2 699794. 246082.     2 FALSE 1     0.1   0    
3 699790. 246007.     3 FALSE 1     0.1   0    
4 699776. 246001.     4 FALSE 1     0.1   0    
5 699764. 245943.     5 FALSE 1     0.1   0    
6 699760. 245939.     6 FALSE 1     0.1   0    

> head(tidydta2) # coordinate values of dataframe created from transformed shapefile
# A tibble: 6 x 7
   long   lat order hole  piece group id   
  <dbl> <dbl> <int> <lgl> <chr> <chr> <chr>
1 -87.8  42.7     1 FALSE 1     0.1   0    
2 -87.8  42.7     2 FALSE 1     0.1   0    
3 -87.8  42.7     3 FALSE 1     0.1   0    
4 -87.8  42.7     4 FALSE 1     0.1   0    
5 -87.8  42.7     5 FALSE 1     0.1   0    
6 -87.8  42.7     6 FALSE 1     0.1   0 

> attr(wisc, "bb") # bounding box of background map
# A tibble: 1 x 4
  ll.lat ll.lon ur.lat ur.lon
   <dbl>  <dbl>  <dbl>  <dbl>
1   42.2  -93.3   47.2  -86.2

# look at the axis values; don't use theme_void until you've checked them
cowplot::plot_grid(
  ggplot() +
    geom_polygon(aes(x=long, y=lat, group=group), 
                 data=tidydta, 
                 color="dark red", alpha=.2, size=.2),
  ggplot() +
    geom_polygon(aes(x=long, y=lat, group=group), 
                 data=tidydta2, 
                 color="dark red", alpha=.2, size=.2),
  labels = c("Original", "Transformed")
)

comparison

© www.soinside.com 2019 - 2024. All rights reserved.