Reputation: 101
I am attempting to make a map of global data anomaly (precip, temp etc) and need to use a diverging color palette. I have code that runs well with scale_color_viridis
but it does not easily convert with scale_color_brewer
. I would like to use the RdBu
diverging palette.
I have tried supplying the values vars
within the function scale_color_brewer
and using scale_fill
brewer but I would like to continue plotting using the geom_points
I have so far.
titletext <- paste(VAR, "Response")
expand.grid(lon, lat) %>%
rename(lon = Var1, lat = Var2) %>%
mutate(lon = ifelse(lon > 180, -(360 - lon), lon),
vars = as.vector(global.array)) %>%
ggplot() +
geom_point(aes(x = lon, y = lat, color = vars),
size = 2.0) +
borders("world", colour="black", fill=NA) +
scale_color_brewer(type="div", palette="RdBu")+
#scale_color_viridis(na.value="white",name = VAR) +
theme(legend.direction="vertical", legend.position="right",
legend.key.width=unit(0.4,"cm"), legend.key.heigh=unit(2,"cm")) +
coord_quickmap() + ggtitle(titletext)
Error message is
Error: Continuous value supplied to discrete scale
and
Warning messages: 1: In RColorBrewer::brewer.pal(n, pal) : n too large, allowed maximum for palette RdBu is 11 Returning the palette you asked for with that many colors
Upvotes: 10
Views: 16161
Reputation: 7455
You can bin/coarse-grain the continuous values to give them a discrete-ordered value to map to the discrete 8 to 12 colors included in your Brewer palette of choice.
But I think the solution you are looking for is to "distill" them with the scale_color_distiller()
or scale_fill_distiller()
functions already included in ggplot2, effectively converting them to a continuous color scale. Documentation and examples are provided in some canonical locations:
Upvotes: 9
Reputation: 6106
RdBu
is a discrete color palette, but you are trying to use it for continuous data.
Please see this documentation and use scale_colour_gradient()
instead.
Upvotes: -2