sf 对象未正确覆盖在 r 中的 ggmap 层上

Posted

技术标签:

【中文标题】sf 对象未正确覆盖在 r 中的 ggmap 层上【英文标题】:sf object is not properly overlaid on ggmap layer in r 【发布时间】:2020-09-23 07:00:03 【问题描述】:

我正在尝试在 R 中将 sf 对象绘制在 ggmap 地形层上。我正在使用以下代码

library(ggmap)
library(sf)
library(tidyverse)

#Downloading data from DIVA GIS website
get_india_map <- function(cong=113) 
  tmp_file <- tempfile()
  tmp_dir  <- tempdir()
  zp <- sprintf("http://biogeo.ucdavis.edu/data/diva/adm/IND_adm.zip",cong)
  download.file(zp, tmp_file)
  unzip(zipfile = tmp_file, exdir = tmp_dir)
  fpath <- paste(tmp_dir)
  st_read(fpath, layer = "IND_adm2")

ind <- get_india_map(114)

#To view the attributes & first 3 attribute values of the data
ind[1:3,]

#Selecting specific districts
Gujarat <- ind %>% 
  filter(NAME_1=="Gujarat") %>%
  mutate(DISTRICT = as.character(NAME_2)) %>%
  select(DISTRICT)

#Added data to plot
aci <- tibble(DISTRICT=Gujarat$DISTRICT,
       aci=c(0.15,0.11,0.17,0.12,0.14,0.14,0.19,0.23,0.12,0.22,
                         0.07,0.11,0.07,0.13,0.03,0.07,0.06,0.04,0.05,0.04,
                         0.03,0.01,0.06,0.05,0.1))

Gujarat <- Gujarat %>% left_join(aci, by="DISTRICT")

#Plotting terrain layer using ggmap
vt <- get_map("India", zoom = 5, maptype = "terrain", source = "google")
ggmap(vt)

#Overlaying 'sf' layer
ggmap(vt) + 
  geom_sf(data=Gujarat,aes(fill=`aci`), inherit.aes=F, alpha=0.9) + 
  scale_fill_distiller(palette = "Spectral")

让我回来

从图中可以看出,sf 图层未正确覆盖在ggmap 地形图层上。如何在ggmap地形图层上正确叠加sf图层?

但是当我使用sp 对象代替sf 对象时,多边形适合ggmap 喜欢

library(sp)
# sf -> sp
Gujarat_sp <- as_Spatial(Gujarat) 

viet2<- fortify(Gujarat_sp)
ggmap(vt) + geom_polygon(aes(x=long, y=lat, group=group), 
                         size=.2, color='black', data=viet2, alpha=0) + 
  theme_map() + coord_map()

但是我不知道如何根据aci填写geom_polygon

【问题讨论】:

对于fill,您正在为连续数据提供谨慎的色标。有ways 强制它工作,但更容易使用为连续数据设计的色标,例如scale_fill_distiller(palette = "Spectral") 非常感谢,我也试过了,效果很好。但主要问题仍然存在(sf 对象未正确覆盖在ggmap 层上)。 【参考方案1】:

在得到this、this 和this 的帮助后,我使用sp 包解决了这个问题。问题是在空间数据上应用fortify 后,只有多边形信息没有属性数据。因此,我已将属性数据合并到强化多边形。这是完整的代码

library(ggmap)
library(sf)
library(tidyverse)
library(sp)

#Downloading data from DIVA GIS website
get_india_map <- function(cong=113) 
  tmp_file <- tempfile()
  tmp_dir  <- tempdir()
  zp <- sprintf("http://biogeo.ucdavis.edu/data/diva/adm/IND_adm.zip",cong)
  download.file(zp, tmp_file)
  unzip(zipfile = tmp_file, exdir = tmp_dir)
  fpath <- paste(tmp_dir)
  st_read(fpath, layer = "IND_adm2")

ind <- get_india_map(114)

#To view the attributes & first 3 attribute values of the data
ind[1:3,]
#To plot it
plot(ind["NAME_2"])

#Selecting specific districts
Gujarat <- ind %>% 
  filter(NAME_1=="Gujarat") %>%
  mutate(DISTRICT = as.character(NAME_2)) %>%
  select(DISTRICT)

#Creating some data
aci <- tibble(DISTRICT=Gujarat$DISTRICT,
       aci=c(0.15,0.11,0.17,0.12,0.14,0.14,0.19,0.23,0.12,0.22,
                         0.07,0.11,0.07,0.13,0.03,0.07,0.06,0.04,0.05,0.04,
                         0.03,0.01,0.06,0.05,0.1))

vt <- get_map("India", zoom = 5, maptype = "terrain", source = "google")
ggmap(vt)

# sf -> sp
Gujarat_sp <- as_Spatial(Gujarat) 

# fortify the shape file
viet2<- fortify(Gujarat_sp, region = "DISTRICT")

# merge data
map.df <- left_join(viet2, aci, by=c('id'='DISTRICT')) 

#Plotting
ggmap(vt) + geom_polygon(aes(x=long, y=lat, group=group, fill = `aci`), 
                         size=.2, color='black', data=map.df, alpha=0.8) + 
  theme_map() + coord_map() + scale_fill_distiller(name = "ACI", palette = "Spectral")

可以使用以下代码制作离散类

#For discrete classes
map.df$brks <- cut(map.df$aci, 
                   breaks=c(0, 0.05, 0.1, 0.15, 0.2, 0.25), 
                   labels=c("0 - 0.05", "0.05 - 0.10", "0.10 - 0.15", 
                            "0.15 - 0.20", "0.20 - 0.25"))

# Mapping with the order of colour reversed 
ggmap(vt) + geom_polygon(aes(x=long, y=lat, group=group, fill = brks), 
                     size=.2, color='black', data=map.df, alpha=0.8) +
  theme_map() + coord_map() + 
  scale_fill_brewer(name="ACI", palette = "Spectral", direction = -1)

【讨论】:

以上是关于sf 对象未正确覆盖在 r 中的 ggmap 层上的主要内容,如果未能解决你的问题,请参考以下文章

绘制 ggmap 和 sf 点

将 ggmap crs 与 geom_sf 对齐并使用 coord_sf 应用地图限制时出现问题

如何将 geom_sf 生成的地图放在 ggmap 生成的栅格之上

使用“r”和ggmap制作航空事故地图

使用 ggplot 在 R 中绘制组合 shapefile 时出现问题,未给出定义错误

choroplethr、ggmap 或 ggplot2 中的道路和半径圆