Shading counties using FIPS code in R map

后端 未结 1 1271
清歌不尽
清歌不尽 2021-01-13 15:27

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 coun

相关标签:
1条回答
  • 2021-01-13 15:59

    Here's an example using the maps library:

    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)
    

    Here's the resulting map:

    Notice that counties with lower FIPS are darker, while counties with higher FIPS are lighter.

    0 讨论(0)
提交回复
热议问题