如何在ggplot2中为不同的图层使用不同的调色板? [英] How can I use different color palettes for different layers in ggplot2?

查看:200
本文介绍了如何在ggplot2中为不同的图层使用不同的调色板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在同一个图上绘制两组数据,但是为每组设置不同的调色板?

  testdf  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,color = yc))+ geom_point geom_point(aes(y = y2))



我想看到的是一组数据,在蓝色中设置 y1 (颜色由 yc 设置),另一个设置为红色code> yc )。



图例应显示两个色标,一个为蓝色,另一个为红色。 >

感谢您的建议。

解决方案

如果翻译blues 红到不同的透明度,那么它不反对ggplot的哲学。因此,使用 Thierry的 Molten 版本的数据集

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

应该做的。


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.

解决方案

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.

这篇关于如何在ggplot2中为不同的图层使用不同的调色板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆