1

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!

Alan
  • 389
  • 3
  • 16
  • 1
    What does the `setColors` function do? What package is that from? See here for how to make a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Stewart Macdonald Sep 28 '18 at 05:28
  • Code throws an error at `year_df` (object not found.) – IRTFM Sep 28 '18 at 05:29

0 Answers0