且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用ggplot()更改线条颜色

更新时间:2022-10-22 23:10:22

color $ c> and 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.