使用 ggplot() 更改线条颜色 [英] Changing line colors with ggplot()

查看:72
本文介绍了使用 ggplot() 更改线条颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不怎么使用 ggplot2,但今天我想我会尝试一些图表.但我不知道如何在 geom_line()

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

这给了我预期的输出:

我以为我所要做的只是一些简单的事情:

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?

推荐答案

colorfill 是独立的美学.既然要修改颜色就需要使用对应的比例:

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

就是你想要的.

这篇关于使用 ggplot() 更改线条颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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