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.

Reply via email to