在ggplot2中改变密度图的颜色

时间:2021-08-01 14:58:39

I've got a nifty pair of probability density functions I created with ggplot2 as follows:

我用ggplot2创建了一对很好的概率密度函数如下:

require(ggplot2)
set.seed(2)
data <- rbind( data.frame(type="a", lr=rnorm(100)), data.frame(type="b", lr=rnorm(100,.5,1.2)))
m <- ggplot(data, aes(x=lr)) 
m <- m + geom_density(aes(fill=factor(type)), size=2, alpha=.4) 
m

and that produces a nice plot:

这就产生了一个很好的情节:

在ggplot2中改变密度图的颜色

However I would really like that plot to have different color shading. I can't seem to figure it out. I thought the following would work:

但是我真的希望这个图有不同的颜色阴影。我似乎搞不清楚。我想下面的方法会有用:

m + scale_colour_manual( values = c("red","blue"))

But no luck. I also tried passing fill colors to the geom_density() call, to no avail.

但没有运气。我还尝试将填充颜色传递给geom_density()调用,但没有效果。

How do I change the fill colors?

如何改变填充颜色?

1 个解决方案

#1


45  

Start kicking yourself:

开始踢自己:

m + scale_fill_manual( values = c("red","blue"))

在ggplot2中改变密度图的颜色

#1


45  

Start kicking yourself:

开始踢自己:

m + scale_fill_manual( values = c("red","blue"))

在ggplot2中改变密度图的颜色