改变ggplot2中密度图的颜色 [英] Changing color of density plots in ggplot2

查看:903
本文介绍了改变ggplot2中密度图的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  require(ggplot2)$ ggplot2创建的一对漂亮的概率密度函数如下: b $ b set.seed(2)
data <-rbind(data.frame(type =a,lr = rnorm(100)),data.frame(type =b,lr = rnorm (100,.5,1.2)))
m < - ggplot(aes(fill = factor(type)),size(aes(x = lr))
m <-m + geom_density = 2,alpha = .4)
m

p>



然而,我真的很希望那个情节有不同的颜色阴影。我似乎无法弄清楚。我认为以下方法可行:

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

但是没有运气。我也尝试将填充颜色传递给geom_density()调用,但无济于事。



如何更改填充颜色?

解决方案

开始踢自己:

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

< img src =https://i.stack.imgur.com/RNdhM.pngalt =在这里输入图片描述>


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

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:

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.

How do I change the fill colors?

解决方案

Start kicking yourself:

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

这篇关于改变ggplot2中密度图的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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