Allow several simultaneous colour scales #578

jiho opened this Issue Jun 13, 2012 · 4 comments


None yet

4 participants

jiho commented Jun 13, 2012

While we think about reorganizing scale_colour/fill functions in pull request #439, here is an additional suggestion:

I think it would be worthwhile to have a variable argument to the scale functions, which would determine to which variable the scale applies and would therefore enable having several colour (or shape, or ...) scales on the same plot for various variables. Of course this can be a problem perceptually (if the colour scales are too similar) but it can also be efficient and even required to represent complex data sets.

Consider graphs like this: where there are various colour scales for various data sources (regions of the brain in that case).
Or maps like this: (sorry, quite small) with a blue scale for depth and another scale (green to red) for species abundance.

Here is a practical example

library(reshape2) # for melt

# volcano map, which really is the background of the plot
vd <- melt(volcano)
names(vd) <- c("x", "y", "z")

# point measurements of something (abund) at a few locations
d <- data.frame(x=runif(30, 1, 80), y=runif(30, 1, 60), abund=rnorm(30))

# plot the points on top of the map
ggplot(mapping=aes(x=x, y=y)) + geom_tile(aes(fill=z), data=vd) + geom_point(aes(colour=abund), data=d, size=3) + scale_colour_gradient(high="red", low="green")
# this works because I map *fill* in one geom and *colour* in the other

# but this collapses the scales of the background and the points because both use fill
ggplot(mapping=aes(x=x, y=y)) + geom_tile(aes(fill=z), data=vd) + geom_point(aes(fill=abund), data=d, size=3, shape=21) + scale_fill_gradient(high="red", low="green")

# I would like to do
ggplot(mapping=aes(x=x, y=y)) + geom_tile(aes(fill=z), data=vd) + geom_point(aes(fill=abund), data=d, size=3, shape=21) + scale_fill_gradient(variable=abund, high="red", low="green")
# and get a result similar to the first plot above

A quick +1 for this feature. Being able to assign colour scales to variables separately would be enormously useful.

For example, being able to build up a composite plot or image by plotting semi-transparent layers with different colour scales.

khughitt commented Jun 2, 2013


This would be useful, for example, to overlay a segmented line fit on top of a scatter plot, each with their own color scales.

hadley commented Feb 24, 2014

This sounds like a great feature, but unfortunately we don't currently have the development bandwidth to support it. If you'd like to submit a pull request that implements this feature, please follow the instructions in the development vignette.

@hadley hadley closed this Feb 24, 2014
hadley commented Feb 24, 2014

(btw ggvis supports this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment