sf对象未正确叠加在r中的ggmap层上

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

我正在尝试在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")

返回我的

enter image description here

如从图中所见,sf图层未正确覆盖在ggmap地形图层上。如何在sf地形图层上正确覆盖ggmap图层?

但是当我使用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()

enter image description here

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

r ggplot2 ggmap sf sp
1个回答
0
投票

我在spthisthis的帮助下使用this软件包解决了问题。问题在于,在空间数据上应用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")

enter image description here

为了创建离散类,可以使用以下代码

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

enter image description here

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