I am trying to map my data onto the U.S. map with colors. Right now, my data frame is structured with the following fields: region (fips code), value, and color. The color is in hexadecimal format as a string, and the color is set to "#FFFFFF" when value is NA for that row.
However, when I run the following code, I get the error:
Error in polygon(coord, col = col, ...) : invalid RGB specification
library(maps)
library(dplyr)
data(county.fips)
counties <- data.frame(region=county.fips$fips)
fips_data <- merge(counties, year_df, all.x=TRUE)
fips_data$color <- numeric(length(fips_data$region))
for(i in 1:length(fips_data$region)) {
fips_data[i, ]$color <- setColors(fips_data[i, ]$value)
}
map("county", fill=TRUE, col=fips_data$color)
I also tried using the col2rgb function to convert the colors to RGB values, but I am getting the same error for that case as well.
Does anyone know what I am doing wrong? Thanks in advance!