Agus camacho
Agus camacho

Reputation: 898

control color of multiple geom_line in ggplot2 in R

I want to plot 3 regression lines, one for each value of tau, and each one with its colour, as specified in the dataset. The internet says you provide the color variable at the general plot aesthetics (http://ggplot.yhathq.com/docs/geom_line.html), but that does not seem to work. Any body would konw what to do?

Thanks in advance,

here is the dataframe https://www.dropbox.com/s/fqmayg6asf5e3eu/tautest.csv?dl=0 here is the code

 ddl=read.csv("tautest.csv")
 ggplot(ddl, aes(x = predictor, y = value, color=col)) + 
 geom_line(aes(group = tau),size = 0.8)

ggplot2 gives some colors, but I want the colors in my dataframe, why would ggplot2 give other colors?

Upvotes: 1

Views: 703

Answers (1)

teunbrand
teunbrand

Reputation: 37913

ggplot 'maps' aesthetics to scales, meaning that it tries to fit a variable onto a scale. The default scale for categorical variables is scale_colour_discrete(), which matches a colour to every unique entry. Since ggplot doesn't know that the values in your col column are colour names, you have to let ggplot know that these colours are to be interpreted literally. You can do that with scale_colour_identity().

ggplot(ddl, aes(x = predictor, y = value, color = col)) + 
  geom_line(aes(group = tau),size = 0.8) +
  scale_colour_identity()

enter image description here

Upvotes: 1

Related Questions