Having difficulty setting the colour scales for maps in ggplot. I need greyscale. Very grateful for an idea where I'm going wrong. I also wonder if there is a more efficient way of getting the colour variable into ggplot (i.e. than by attaching it to 'fortified' data)?
library(ggplot2)
states <- map_data("state")
var <- data.frame(table(states$region)) # using rows as a dummy variable
states$variable <- var$Freq[match(states$region,var$Var1)]
map <- ggplot(states, aes(x=long, y=lat)) +
geom_polygon(aes(group=group, fill=variable), col=NA,lwd=0)
map + scale_colour_gradient(low='white', high='grey20')
map + scale_colour_grey()
This code works for me.
An easy way to handle problems with discrete variables is to create a "fake" continuous palette using the color palette function. See the below example.
Define color palette, here I used the hex codes for black and white but you can use any colors
Now create some fake data
Next plot it with the ggplot function
scale_*
type_manual
in this case since its color in you'd usescale_colour_manual
but above you'd usescale_fill_manual
You need to use
scale_fill_*
and notscale_color_*
. For the polygon geometry the fill color of the polygons is coupled to thefill
aesthetic, not to thecolor
aesthetic. In general, the function used to change the details of a particular scale isscale_
aesthetic_name_
type_of_scale, e.g.scale_fill_gradient
.