0

does anyone know how I can plot several line graphs in one graph with different color codes?

ggplot(df, aes(x=variable1)) +
geom_line(aes(y=variable2,color=group1))+
geom_line(aes(y=variable3,color=group1))

I would like to have one color code for the first geom_line and a different one for the second geom_line.

color_group <- c("blue","black","yellow2","orange")
color_flag <- c("green","red","yellow2","cyan")

With

 scale_colour_manual(values=color_group)

I can only assign a color code to both of them simultaneously and not separately. Thanks for your help!

Envoy1
  • 37
  • 5
  • Please share your data using `dput(df)`? – Quinten May 11 '22 at 08:24
  • 1
    It looks like you could pivot your y axis variables into long format to achieve this. If you're stuck on how to do that, please include some reproducible data we can use to demonstrate. – Allan Cameron May 11 '22 at 08:29

1 Answers1

1

You could use the ggnewscale package

library(ggnewscale)

ggplot(df, aes(x = variable1)) +
  geom_line(aes(y = variable2, color = group1)) +
  scale_colour_manual(values = color_group) +
  new_scale_color() +
  geom_line(aes(y = variable3, color = group1)) +
  scale_colour_manual(values = color_flag)
abichat
  • 2,317
  • 2
  • 21
  • 39
  • Thanks, I have tried it out a bit and it works like intended. Now I can plot sets of linear graphs with two different axis/ scales (one on the left and one on the right) and by changing the colors of the line graphs separately I can visualize to which scale they belong! :) – Envoy1 May 11 '22 at 09:36