I have dataframe below:
mydf <- data.frame(FIP=c(1017,1059,1061,1133,5103,12129,13123,12129),
val=c(100,100,225,400,180,278,180,321))
I want to shade US counties on one single map whose FIPs are in FIP colum, but I am stuck at is how to shade these specific counties such that something like a heat map appears. the counties higher pop are more red and counties with lower pop are more blue (like typical gradient of heat map).
library(tidyverse)
library(usmap)
library(grDevices)
dt <- countypop %>%
dplyr::mutate(selected = factor(ifelse(fips %in% stringr::str_pad(mydf$FIP, 5, pad = "0"),
"1",
"0")))
usmap::plot_usmap(data = dt, values = "selected", color = "grey") +
ggplot2::scale_fill_manual(values = c(rainbow(8))
I can shade specific counties (although if all are going to be shaded with color!) but I want a gradient effect here.
Maybe this is what you are looking for. As far as I get it you want to make a map where only the counties present in your dataframe df get colored according to the
val
.To this end you could join
mydf
tocountypop
. Doing so will automatically assignNA
to theval
column of non-selected counties. To color byval
you have to set thevalues
argument equal to"val"
inusmap::plot_usmap
. To get a gradient of values useggplot2::scale_fill_gradient
where you could set the color used forNA
via thena.value
argument, e.g. in the code below I make use ofna.value = "transparent"
: