2016-11-13 51 views
3

我使用栅格包中的getData函数来检索阿根廷的地图。我想使用ggplot2绘制生成的地图,所以我使用扫帚包中的整洁函数转换为数据框。这工作正常,但我不知道如何保留联邦区的名称,以便我可以在地图上使用它们。使用扫帚包整理地图时保留区域名称

这里是我的原代码,不保留地区名称:

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

,这里是用劈拉地区名称了SPDF,并利用它们作为地图标识代码:

# 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([email protected]$NAME_1)){ 
    [email protected][[region_looper]]@ID <- 
    as.character([email protected]$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) 

我一直在想,必须有一些方法来使用保留名称的整齐函数,但对于我的生活,我无法弄清楚。

+0

我没有你的数据,但在'扫帚:: sp_tidiers',有一个'region'参数,如果你正在处理一个'SpatialPolygonsDataFrame'。 – alistaire

+0

如果在所提供的路径中未找到数据,'raster :: getData'函数将下载数据。我应该提到我在文档中发现了'region'参数,但我无法弄清楚如何将'arg_map_1 @ data $ NAME_1'的值传递给它。 – jkgrain

回答

2

alistaire的评论推动我继续推动region=参数。我尝试了很多次迭代,并且在这个线程中发现了一些想法https://github.com/tidyverse/ggplot2/issues/1447

这里是抓住了地区名称代码:

所有的
# 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) 

首先,注意到maptools库必须被加载的整洁操作才能正常工作。另外,我想强调要从中提取区域信息的变量必须用引号引起来。我错误地认为扫帚会识别变量名称,就像dplyr等其他翻转包一样识别列名未被引用或被反引号包围。

0

您可以使用包plyr中的join函数。这里是一个通用的解决方案(它看起来很长,但它实际上是很容易的):

  1. 负载shape文件:让我们说,你有你的工作目录shape文件my_shapefile.shp。让我们来加载:

    shape <- readOGR(dsn = "/my_working_directory", layer = "my_shapefile") 
    

    注意这个shape文件里面有一个数据帧,可以用[email protected]访问。例如,该数据帧看起来是这样的:

    > head([email protected]) 
         code     region  label 
    0 E12000006   East of England E12000006 
    1 E12000007     London E12000007 
    2 E12000002    North West E12000002 
    3 E12000001    North East E12000001 
    4 E12000004   East Midlands E12000004 
    5 E12000003 Yorkshire and The Humber E12000003 
    
  2. 从shape文件创建新的数据框:使用broom包潮shape文件数据框:

    new_df <- tidy(shape) 
    

这导致东西像这样:

> head(new_df) 
     long  lat order hole piece group id   
1 547491.0 193549.0  1 FALSE  1 0.1 0 
2 547472.1 193465.5  2 FALSE  1 0.1 0 
3 547458.6 193458.2  3 FALSE  1 0.1 0 
4 547455.6 193456.7  4 FALSE  1 0.1 0 
5 547451.2 193454.3  5 FALSE  1 0.1 0 
6 547447.5 193451.4  6 FALSE  1 0.1 0 

不幸的是,tidy()丢失了变量名称(在本例中为“region”)。相反,我们得到一个新的变量“id”,从0开始。幸运的是,“id”的排序与存储在[email protected]$region中的排序相同。让我们用这个来恢复名字。

  • 创建行名称辅助数据框:让我们创建一个行名字一个新的数据帧。使用“身份证”

    # Recover row name 
    temp_df <- data.frame([email protected]$region) 
    names(temp_df) <- c("region") 
    # Create and append "id" 
    temp_df$id <- seq(0,nrow(temp_df)-1) 
    
  • 新的数据帧合并行名称:此外,我们将一个“id”变量,相同添加到创建的tidy()最后,让我们把名字回新据帧:

    new_df <- join(new_df, temp_df, by="id") 
    
  • 这就是它!您甚至可以通过使用join命令和“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