8

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

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))

我想看到的是一组数据,比如说y1,蓝色(颜色设置为yc),另一组数据设置为红色(同样颜色设置为yc)。

然后图例应显示 2 个色标,一个为蓝色,另一个为红色。

感谢您的建议。

4

2 回答 2

5

如果您将“蓝色”和“红色”翻译为不同的透明度,那么这并不违反 ggplot 的理念。因此,使用Thierry 的Molten数据集版本

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

应该做的伎俩。

于 2012-02-24T11:42:09.717 回答
4

ggplot2 不可能做到这一点。我认为这违背了 ggplot2 的哲学,因为它使情节的解释复杂化。

另一种选择是使用不同的形状来分隔点。

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()
于 2012-02-24T10:08:22.217 回答