Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing line colors with ggplot()

Tags:

r

ggplot2

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:

enter image description here

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?

like image 726
JD Long Avatar asked Mar 02 '11 17:03

JD Long


People also ask

How do you change the color of a line in Ggplot?

Change manually the appearance of lines scale_linetype_manual() : to change line types. scale_color_manual() : to change line colors. scale_size_manual() : to change the size of lines.

How do I change colors in R?

In R, colors can be specified either by name (e.g col = “red”) or as a hexadecimal RGB triplet (such as col = “#FFCC00”). You can also use other color systems such as ones taken from the RColorBrewer package.


1 Answers

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.

like image 198
Ista Avatar answered Oct 15 '22 11:10

Ista