2017-10-16 77 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) 
相关问题