Jean, I think I fixed it: newpal <- colorRamp(c("yellow", "red")) missing <- is.na(mydata.final$Mean.Wait) newcol <- ifelse(missing, "white",
rgb(newpal(mydata.final$Mean.Wait[!is.na(mydata.final$Mean.Wait)]/ max(mydata.final$Mean.Wait, na.rm=T)), maxColorValue=255)) map('county', fill=TRUE, col=newcol, resolution=0, lty=0, bg="transparent") map('state', lwd=1, add=TRUE) One understanding question: what exactly does this rgb line do and why do we have to say "maxColorValue=255"? Thank you! On Thu, Apr 2, 2015 at 5:02 PM, Dimitri Liakhovitski <dimitri.liakhovit...@gmail.com> wrote: > Thank you, Jean, but I think this newcol line is not working. I am running: > > newcol <- ifelse(missing, "white", > > rgb(newpal(mydata.final$Mean.Wait/max(mydata.final$Mean.Wait, > na.rm=T)), > maxColorValue=255)) > > # And I am getting: > Error in rgb(newpal(mydata.final$Mean.Wait/max(mydata.final$Mean.Wait, : > color intensity NA, not in 0:255 > > I think it's not liking the NAs - despite the ifelse... > > On Thu, Apr 2, 2015 at 4:26 PM, Adams, Jean <jvad...@usgs.gov> wrote: >> Dimitri, >> >> You could use colorRamp() and rgb() to get more continuous colors. >> For example >> >> newpal <- colorRamp(c("yellow", "red")) >> missing <- is.na(mydata.final$Mean.Wait) >> newcol <- ifelse(missing, "white", >> rgb(newpal(mydat$Mean.Wait/max(mydat$Mean.Wait)), maxColorValue=255)) >> map('county', fill=TRUE, col=newcol, >> resolution=0, lty=0, bg="transparent") >> map('state', lwd=1, add=TRUE) >> >> Jean >> >> >> On Thu, Apr 2, 2015 at 12:03 PM, Dimitri Liakhovitski >> <dimitri.liakhovit...@gmail.com> wrote: >>> >>> I have a data frame 'mydata.final' (see below) that contains US >>> counties and a continuous numeric variable 'Mean.Wait' that ranges >>> from zero to 10 or so. I also created variable 'wait' that is based on >>> the 'Mean.Wait' and takes on discrete values from 1 (lowest values on >>> 'Mean.Wait') to 5 (highest values on 'Mean.Wait'). >>> >>> I can create a map of the US with the counties colored based on the >>> values of 'wait' using R package 'maps': >>> >>> ################################################################# >>> ### Generating an artificial data file: >>> ################################################################# >>> library(maps) >>> mydata.final <- data.frame(county = (map('county', plot = FALSE)$names), >>> stringsAsFactors = F) >>> >>> ### My numeric variable: >>> set.seed(123) >>> mydata.final$Mean.Wait <- runif(nrow(mydata.final)) * 10 >>> >>> ### Introducing NAs to mimic my real data set: >>> set.seed(1234) >>> mydata.final$Mean.Wait[sample(1:nrow(mydata.final), 1500)] <- NA >>> >>> ### Cutting the original numeric variable into categories >>> ### because I don't know how to color based on 'Mean.Wait': >>> mydata.final$wait <- cut(mydata.final$Mean.Wait, breaks = 5) >>> levels(mydata.final$wait) <- 1:5 >>> mydata.final$wait <- as.numeric(as.character(mydata.final$wait)) >>> >>> #################################################################### >>> Building a US map based on 'wait' (5 categories) >>> ################################################################# >>> >>> ### Creating my 5 colors: >>> pal <- colorRampPalette(c("yellow", "red")) >>> allcolors <- pal(5) >>> >>> ### Looking at my 5 colors: >>> barplot(1:5, rep(1,5), col = allcolors, horiz = T) >>> >>> ### Builiding the US map using 5 categories in 'wait': >>> map('county', fill = TRUE, col = allcolors[mydata.final$wait], >>> resolution = 0, lty = 0, bg = "transparent") >>> map('state', lwd=1, add=TRUE) >>> >>> My goal is: instead of splitting 'Mean.Wait' into 5 ordered categories >>> ('wait'), I'd like to color the counties on the map based on the >>> intensity of my (continuous) 'Mean.Wait'. What would be the way to do >>> it and maybe even to add a legend? >>> Thanks a lot! >>> >>> -- >>> Dimitri Liakhovitski >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> > > > > -- > Dimitri Liakhovitski -- Dimitri Liakhovitski ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.