[英]R overlay geom_polygon with ggmap object, spatial file conversion
我已經在幾個地方看到過這樣的例子,包括在 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=-1125%4244 -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")
問題是我不能把這些結合起來。 我假設整潔的轉換肯定有問題,或者我錯過了一步。 我確實收到一個錯誤:
在 min(x) 中:沒有非缺失的 min 參數; 返回信息
我認為這是因為我在某處有一個零長度向量。
這是命令:
ggmap(wisc) +
geom_polygon(aes(x=long, y=lat, group=group),
data=tidydta,
color="dark red", alpha=.2, size=.2)
我已經使用 geom_point 成功地將地理編碼點添加到地圖中,但我被多邊形困住了。
誰能告訴我我做錯了什么?
您的 shapefile 使用的坐標系不是經緯度。 您應該在將其轉換為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)
為了將來參考,請通過將數據框打印到控制台/使用可見的 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")
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.