【发布时间】:2016-10-10 20:53:51
【问题描述】:
我对 R 完全陌生,想在下面的qplot 绘图函数中插入一个简单的彩色框图例,将每个分布标识为“data.1”和“data.2” (来自ggplot2 包):
> V1 <- matrix(unlist(rnorm(200,-0.2,1)))
> V2 <- matrix(unlist(rnorm(200,0.3,4)))
> m <- data.frame(V1,V2)
> qplot(V1, main="Observed distr.", data=m,
geom='density',xlab="x",ylab="count",fill=I('green'), alpha=I(.5)) +
geom_density(aes(V2),data=m,fill='red', alpha=I(.5))
我找到了ggplot 的解决方案,但没有找到qplot 和geom='density' 的解决方案。曲线绘制得很好,但没有出现图例。
我会接受任何能给我提供透明度、标记轴、标题和彩色框图例的密度图的解决方案。谢谢。
【问题讨论】:
-
恕我直言:忘记
qplot,使用ggplot:library(tidyverse);ggplot(m %>% gather, aes(value, fill = key)) + geom_density(alpha = .5) + scale_fill_manual(values = c("green", "red"), labels = c("V1" = "myV1"))。我想这将是他们无论如何都要走的路in the future:“示例中 qplot() 的使用已大大减少。这与 ggplot2 书的第二版一致,它消除了 qplot() 的支持ggplot() 的。” -
@lukeA:是的,我开始看到这一点。我完成了
qplot。谢谢。