I am working on creating a map of Latin America (including Spain) which fills in each country according to a number which is the average of certain speech features. The number is on a continuum, so having a gradient color fill (ie higher numbers are darker) would be perfect. Anyway, here's the code I'm doing:
loc <- levels(data$country)
cases <- levels(as.factor(data$mean))
frics <- data.frame(loc,cases)
m <- map('worldHires',plot=F, xlim=c(-120,-35),ylim=c(-60,30), fill=T)
stm <- match.map(m, frics$loc)
frics$rank <- rank(as.numeric(frics$cases), ties='min')
pal <- brewer.pal(max(frics$rank),'Reds')
color <- pal[frics$rank]
frics.color <- color[stm]
map(m,col=frics.color,fill=T, lty=0,boundary=F,interior=F) # fill regions
map('worldHires',interior=T,add=T,col='grey30')
Also, here are the corresponding vectors for frics$loc and frics$cases:
structure(list(loc = structure(c(7L, 1L, 10L, 8L, 9L, 12L, 14L,
4L, 3L, 13L, 11L, 2L, 5L, 6L), .Label = c("Argentina", "Bolivia",
"Chile", "Colombia", "Dominican Republic", "Ecuador", "El Salvador",
"Guatemala", "Honduras", "Mexico", "Nicaragua", "Paraguay", "Puerto Rico",
"Spain"), class = "factor"), cases = c(765, 1582, 928, 1055,
608, 1780, 1976, 802, 1795, 530, 644, 1160, 633, 930)), .Names = c("loc",
"cases"), row.names = c(NA, -14L), class = "data.frame")
So, when I plot the map, it turns out that some countries are not being filled. Most notably, Ecuador, but other Central American Countries as well. I suspect it may be because the color palette I am using does not support 14 different colors. Or maybe something else.
After some research, it seems that RColorBrewer does not support more than 12 sequential colors in any of their palettes that I could find. I was recommended instead to use the default function in R colorRampPalette(). To solve my problem, I replaced:
pal <- brewer.pal(max(frics$rank),'Reds')
color <- pal[frics$rank]
With:
wbPal <- colorRampPalette(c('white','black')) #spectrum ranges from white to black
color <- wbPal(14) #number of distinct colors
The resulting color vector can be as long as you specify, and the colors are gradient steps along the spectrum defined in the first line. In this case, "white -> black". Once I got a color vector that had 14 steps, the other countries filled in appropriately. map()'s "col=" argument will default to NA as a color once the maximum number of colors in the palette has been reached.
Thanks for the feedback.