How to use different color palettes for different layers in ggplot2?

Is it possible to build two data sets on the same site, but use different color palettes for each set?

testdf <- data.frame( x = rnorm(100), y1 = rnorm(100, mean = 0, sd = 1), y2 = rnorm(100, mean = 10, sd = 1), yc = rnorm(100, mean = 0, sd = 3)) ggplot(testdf, aes(x, y1, colour = yc)) + geom_point() + geom_point(aes(y = y2)) 

What I would like to see is one data set, for example y1 , in the blues (the color specified by yc ), and the other set is in red (again the color is set by yc ).

The legend should display 2 color scales, one is blue, the other is red.

Thanks for your suggestions.

+4
colors r ggplot2 palette
source share
2 answers

If you translate β€œblues” and β€œred” to changing transparency, then this is not against the ggplot philosophy. So, using the Thierry Molten version of the dataset :

 ggplot(Molten, aes(x, value, colour = variable, alpha = yc)) + geom_point() 

Gotta do the trick.

+3
source share

This is not possible with ggplot2. I think this is against the ggplot2 philosophy, because it complicates the interpretation of the plot.

Another option is to use different shapes to separate the dots.

 testdf <- data.frame( x = rnorm(100), y1 = rnorm(100, mean = 0, sd = 1), y2 = rnorm(100, mean = 10, sd = 1), yc = rnorm(100, mean = 0, sd = 3)) Molten <- melt(testdf, id.vars = c("x", "yc")) ggplot(Molten, aes(x, value, colour = yc, shape = variable)) + geom_point() 
+4
source share

All Articles