Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Filling Counties by FIPS code with heat map

Tags:

r

This is my first time plotting in R, and I would greatly appreciate any help.

I have a dataframe df with two columns, region and value. The region column represents the FIPS codes and the values represents the number I want to plot with regards to the color scale ranging from 0-5000.

I looked at Shading counties using FIPS code in R map but I am not sure how to convert this gray scale shading to colors with either hex or rgb representation. In addition, I do not know how I would shade in colors based off a certain row's values.

This is the code I currently have, and it is plotting a map of the counties (all white) along with their borders.

library(maps)
library(dplyr)

data(county.fips)
year_df <- year_df[c("region", "value")]
counties <- county.fips %>% left_join(year_df, by=c('fips'='region'))
map("county", fill=TRUE)

Thanks in advance!

like image 982
Alan Avatar asked Oct 19 '25 10:10

Alan


1 Answers

The answer you linked to is really quite well done and provides a great reference for this question. I took the code in that answer and changed gray() to rainbow() like so:

df_pop_county$color <- rainbow(n = 50, y / max(y))

Which produced this: enter image description here

like image 112
Stedy Avatar answered Oct 21 '25 23:10

Stedy