问题描述
我正在寻找一种在R中的美国地图上给县添加阴影的方法.我有一个数字/字符县FIPS代码列表,可以将其输入作为参数.我只需要突出显示这些县-所以只需要阴影它们,就没有与这些县相对应的值或变体.我试图查找
I am looking for a way to shade counties on the US maps in R. I have list of numeric/char county FIPS code that I can input as parameter. I just need to highlight these counties -- so would just need to shade them and there are no values or variations corresponding to the counties. I tried to look up
library(choroplethr)
library(maps)
和
county_choropleth(df_pop_county)
head(df_pop_county)
region value
1 1001 54590
2 1003 183226
3 1005 27469
4 1007 22769
5 1009 57466
6 1011 10779
但是这些需要一个区域,值对.例如,上面的fips代码和填充.有没有一种方法可以仅使用fipscode数据帧来调用county_choropleth函数而不必使用值.这样,我就可以用一种颜色来编码我的fips了.使用Choroplethr在R中完成此任务的有效方法是什么?
But these need a region, value pair. For e.g.,fips code and population in the above. Is there a way to call the county_choropleth function without having to use the values, just with the fipscode dataframe. In that way, I can my fips code with one color. What would be an efficient way to accomplish this in R using Choroplethr?
推荐答案
以下是使用maps
库的示例:
library(maps)
library(dplyr)
data(county.fips)
## Set up fake df_pop_county data frame
df_pop_county <- data.frame(region=county.fips$fips)
df_pop_county$value <- county.fips$fips
y <- df_pop_county$value
df_pop_county$color <- gray(y / max(y))
## merge population data with county.fips to make sure color column is
## ordered correctly.
counties <- county.fips %>% left_join(df_pop_county, by=c('fips'='region'))
map("county", fill=TRUE, col=counties$color)
这是生成的地图:
请注意,FIPS较低的县较暗,而FIPS较高的县较亮.
Notice that counties with lower FIPS are darker, while counties with higher FIPS are lighter.
这篇关于在R地图中使用FIPS代码对县进行着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!