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

sf object is not properly overlaid on ggmap layer in r

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

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")

哪个returns我

从图中可以看出,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?

this, this and 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)