R 使用扫帚包整理地图时保留区域名称

R 使用扫帚包整理地图时保留区域名称,r,geospatial,broom,R,Geospatial,Broom,我正在使用光栅包中的getData函数检索阿根廷地图。我想使用ggplot2绘制结果图,因此我正在使用broom包中的tidy函数转换为数据帧。这很好,但我不知道如何保存联邦区的名称,以便在地图上使用它们 以下是我的原始代码,不保留地区名称: # Original code: ################################## # get the map data from GADM.org and then simplify it arg_map_1 <- raster

我正在使用光栅包中的getData函数检索阿根廷地图。我想使用ggplot2绘制结果图,因此我正在使用broom包中的tidy函数转换为数据帧。这很好,但我不知道如何保存联邦区的名称,以便在地图上使用它们

以下是我的原始代码,不保留地区名称:

# Original code: ##################################
# get the map data from GADM.org and then simplify it
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/")     %>% 
  # simplify
  rmapshaper::ms_simplify(keep = 0.01) %>% 
  # tidy to a dataframe
  broom::tidy()

# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
  geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
       color="#000000", size=0.25)
# load the magrittr library to get the pipe
library(magrittr)
# load the maptools library to get the rgeos object
library(maptools)

arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>% 
  # simplify
  rmapshaper::ms_simplify(keep = 0.01) %>% 
  # tidy to a dataframe
  broom::tidy(region="NAME_1")

# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
  geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
           color="#000000", size=0.25)
#原始代码:##################################
#从GADM.org获取地图数据,然后对其进行简化
参数映射1%
#简化
rmapshaper::ms_simplify(保持=0.01)%>%
#整理到数据帧
扫帚:整洁的
#画地图
图书馆(GG2)
ggplot(数据=参数图1)+
几何地图(地图=arg\u地图=1,aes(x=long,y=lat,地图=id,fill=id),
color=“#000000”,大小=0.25)
这是一个从SPDF中提取地区名称并将其用作地图ID的黑客代码:

# Code with a hack to keep the district names: ################################
# get the map data from GADM.org and then simplify it
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>% 
  # simplify
  rmapshaper::ms_simplify(keep = 0.01)  

for(region_looper in seq_along(arg_map_1@data$NAME_1)){
  arg_map_1@polygons[[region_looper]]@ID <- 
    as.character(arg_map_1@data$NAME_1[region_looper]) 
}

# tidy to a dataframe
arg_map_1 <- arg_map_1 %>% 
  broom::tidy()

library(ggplot2)
ggplot(data=arg_map_1) +
  geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
           color="#000000", size=0.25)
#使用hack代码保留地区名称:################################
#从GADM.org获取地图数据,然后对其进行简化
参数映射1%
#简化
rmapshaper::ms_simplify(keep=0.01)
用于(区域)沿(参数图)顺序的活套_1@data$NAME_(1)){

arg_图_1@polygons[[region\u looper]@IDalistaire的评论促使我继续使用
region=
参数。我尝试了多次迭代,在这个线程中发现了一些想法

以下是获取地区名称的代码:

# Original code: ##################################
# get the map data from GADM.org and then simplify it
arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/")     %>% 
  # simplify
  rmapshaper::ms_simplify(keep = 0.01) %>% 
  # tidy to a dataframe
  broom::tidy()

# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
  geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
       color="#000000", size=0.25)
# load the magrittr library to get the pipe
library(magrittr)
# load the maptools library to get the rgeos object
library(maptools)

arg_map_1 <- raster::getData(country = "ARG", level = 1, path = "./data/") %>% 
  # simplify
  rmapshaper::ms_simplify(keep = 0.01) %>% 
  # tidy to a dataframe
  broom::tidy(region="NAME_1")

# plot the map
library(ggplot2)
ggplot(data=arg_map_1) +
  geom_map(map=arg_map_1, aes(x=long, y=lat, map_id=id, fill=id),
           color="#000000", size=0.25)
#加载magrittr库以获取管道
图书馆(magrittr)
#加载maptools库以获取rgeos对象
图书馆(地图工具)
参数映射1%
#简化
rmapshaper::ms_simplify(保持=0.01)%>%
#整理到数据帧
扫帚::整洁(region=“NAME_1”)
#画地图
图书馆(GG2)
ggplot(数据=参数图1)+
几何地图(地图=arg\u地图=1,aes(x=long,y=lat,地图=id,fill=id),
color=“#000000”,大小=0.25)

首先,请注意,必须加载maptools库才能使tidy操作正常工作。此外,我想强调的是,要从中提取区域信息的变量必须用引号括起来。我错误地假设broom会以与其他tidyverse Package相同的方式识别变量名dplyr等es可识别未加引号或被反勾号包围的列名。

您可以使用package
plyr
中的
join
功能。下面是一个通用解决方案(看起来很长,但实际上很简单):

  • 加载shapefile:假设您的工作目录中有一个shapefile
    my_shapefile.shp
    。让我们加载它:

    shape <- readOGR(dsn = "/my_working_directory", layer = "my_shapefile")
    
  • 从shapefile创建新数据框:使用
    扫帚
    包调整shapefile数据框:

    new_df <- tidy(shape)
    
    new_df <- join(new_df, temp_df, by="id")
    
    不幸的是,
    tidy()
    丢失了变量名(“region”,在本例中)。相反,我们得到了一个新变量“id”,从0开始。幸运的是,“id”的顺序与
    shape@data$region
    。让我们使用它来恢复名称

  • 使用行名称创建辅助数据框:让我们使用行名称创建一个新的数据框。此外,我们将添加一个“id”变量,与创建的
    tidy()
    变量相同:

    # Recover row name 
    temp_df <- data.frame(shape@data$region)
    names(temp_df) <- c("region")
    # Create and append "id"
    temp_df$id <- seq(0,nrow(temp_df)-1)
    

    我没有您的数据,但是在
    ?扫帚::sp_tidiers
    ,如果您处理的是
    空间多边形框架
    ,则有一个
    区域
    参数。如果在提供的路径上找不到数据,
    光栅::getData
    函数将下载该数据。我应该提到,我在中找到了
    区域
    参数文档,但我不知道如何传递
    arg\u映射_1@data$NAME_1
    它的值。
    tidy()
    有一个
    区域
    参数:
    新建_df@0range哦!我不知道。好吧,这简化了一点,呵呵。也许你可以为这个问题添加一个进一步的答案。我一直在努力让
    region
    参数起作用……这个解决方案很好——只需注意shape@data不一定总是0…n-在这种情况下,您可以用行名填充temp_df ID(shape@data)获取正确的ID
    > head(new_df)
          long      lat order  hole piece group id            name    var1    var2 
    1 547491.0 193549.0     1 FALSE     1   0.1  0 East of England   0.525   0.333   
    2 547472.1 193465.5     2 FALSE     1   0.1  0 East of England   0.525   0.333   
    3 547458.6 193458.2     3 FALSE     1   0.1  0 East of England   0.525   0.333   
    4 547455.6 193456.7     4 FALSE     1   0.1  0 East of England   0.525   0.333   
    5 547451.2 193454.3     5 FALSE     1   0.1  0 East of England   0.525   0.333   
    6 547447.5 193451.4     6 FALSE     1   0.1  0 East of England   0.525   0.333