使用ggplot()更改线条颜色 [英] Changing line colors with ggplot()
本文介绍了使用ggplot()更改线条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有使用ggplot2那么多,但今天我想我会在一些图表上放一下。但我无法弄清楚如何在 geom_line()
中手动控制颜色。这里是我的测试代码:
x < - c(1:20,1:20)$ (rnorm(20),rnorm(20,.5))b $ b变量<-c(rep(y1,20),rep(y2,20))
value <-c
df < - data.frame(x,variable,value)
d < - ggplot(df,aes(x = x,y = value,group = variable ,color = variable))+
geom_line(size = 2)
d
这给了我预期的输出:
我以为我所要做的只是简单的事情:
d + scale_fill_manual(values = c(#CC6666,#9999CC))
但这没有改变。我缺少什么?
解决方案
fill
是单独的美学。由于您要修改颜色,因此您需要使用相应的比例:
d + scale_color_manual(值= c(#CC6666,#9999CC))
是您想要的。 / p>
I don't use ggplot2 that much, but today I thought I'd give it a go on some graphs. But I can't figure out how to manually control colors in geom_line()
I'm sure I'm overlooking something simple, but here's my test code:
x <- c(1:20, 1:20)
variable <- c(rep("y1", 20), rep("y2", 20) )
value <- c(rnorm(20), rnorm(20,.5) )
df <- data.frame(x, variable, value )
d <- ggplot(df, aes(x=x, y=value, group=variable, colour=variable ) ) +
geom_line(size=2)
d
which gives me the expected output:
I thought all I had to do was something simple like:
d + scale_fill_manual(values=c("#CC6666", "#9999CC"))
But that changes nothing. What am I missing?
解决方案
color
and fill
are separate aesthetics. Since you want to modify the color you need to use the corresponding scale:
d + scale_color_manual(values=c("#CC6666", "#9999CC"))
is what you want.
这篇关于使用ggplot()更改线条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文