Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
612 views
in Technique[技术] by (71.8m points)

r - How can I use different color palettes for different layers in ggplot2?

Is it possible to plot two sets of data on the same plot, 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 set of data, say y1, in blues (color set by yc), and the other set in reds (again color set by yc).

The legend should then show 2 color scales, one in blue, the other red.

Thanks for your suggestions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you translate the "blues" and "reds" to varying transparency, then it is not against ggplot's philosophy. So, using Thierry's Moltenversion of the data set:

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

Should do the trick.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...