2017-10-16 93 views
0

我很難將漸變顏色映射到某些縣級人口數據,我使用的是基本R包圖。我知道必須將顏色插值到數據框,但我不確定那是如何轉換爲地圖的。這裏是代碼我使用:如何使用地圖包映射單個美國州(MO)縣人口數據?

library(dplyr) 
require(maps) 

my_fake_data <- data_frame(
    county = sample(c('list','of','all','counties'),115,T), 
    county_population = sample(1:1000000,115,T)) 

grey_black <- colorRampPalette(c('grey50','black')) 

map_data <- my_fake_data %>% 
    arrange(county_population) %>% 
    mutate(county_population_color = grey_black(nrow(.))) 

map('county','missouri',interior = T,fill =T, 
col = grey_black(map_data$county_population_color)) 

如何讓R能夠以正確的順序繪製的顏色?我的感覺告訴我將我的數據附加到地圖的內部數據庫,但我找不到要正確執行此操作的文檔 - 或者更可能是我錯了。任何幫助將不勝感激。

+0

[這個問題](https://stackoverflow.com/questions/24399367/plot-fill-color-map-with-usa -states-data-in-r)可能會有所幫助。 – duckmayr

+0

嘗試'col = grey_black(10)'其中10是'grey50'和'black'之間的顏色數 – missuse

回答

0

要回答您的問題,您需要訪問地圖包中包含的county.fips數據框。這將有fips號和州,縣名。該列表按映射的正確順序排列。下面的代碼示例提取密蘇里縣和隨機顏色幾個進行驗證:

mocounties<-county.fips[grepl('missouri', county.fips$polyname),] 
    mocounties$col<-"grey" 
    mocounties$col[5]<-"red" 
    mocounties$col[15]<-"green" 
    mocounties$col[115]<-"blue" 

map('county','missouri', interior = T,fill =T, 
    col = mocounties$col) 
相關問題