2016-11-13 54 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