1

I am pretty new to ggplot2, and have so far succeeded in creating a 3 layered geom_line plot. Since the graph is for my color-blind supervisor, the line colors have to change (especially the red and green). I have tried different things, but cannot get it to work. Any good suggestions?

Thanks!

p = ggplot() + 
    geom_line(data = my_data, aes(x = E, y = N50_A, colour = "A")) +
    geom_line(data = my_data, aes(x = E, y = N50_C, colour = "C")) +
    geom_line(data = my_data, aes(x = E, y = N50_D, colour = "D")) + 
    xlab('E') + ylab('N50') + 
    theme_bw() + ggtitle("E90N50") +
    theme(legend.position = "bottom", 
          legend.title = element_blank(), 
          plot.title = element_text(hjust = 0.5)) 

p

Here is the resulting plot

alistaire
  • 42,459
  • 4
  • 77
  • 117
Biogitte
  • 303
  • 3
  • 9
  • 1
    See `?scale_colour_manual`, or perhaps `?scale_colour_brewer`. – Axeman Feb 22 '17 at 16:44
  • 2
    Reshape your data to long form so you can map color to a variable for whether a line is "A", "C", or "D" and only need one `geom_line` call. That way you can use `scale_colour_*`. – alistaire Feb 22 '17 at 16:44
  • @alistaire, while that's good general advice, applying scales to their current plot will work fine. – Axeman Feb 22 '17 at 16:45
  • 1
    Also more convenient: `labs(x = 'E', y = 'N50', title = 'E90N50')` – alistaire Feb 22 '17 at 16:46
  • 1
    @Axeman Hmm, you're right. I've never tried, since it seems awkward to specify a variable in the aesthetics, but ggplot figures it out nicely. – alistaire Feb 22 '17 at 16:50
  • @alistaire, sadly, Hadley advocates this [here](http://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph). – Axeman Feb 22 '17 at 16:51
  • @Axeman \*sigh.\* At least he mentions to only use it for simple plots, but that answer should at least mention that reshaping is the way to go when it gets more complicated. – alistaire Feb 22 '17 at 16:58
  • 2
    @Biogitte A plug for `scale_color_brewer`, which uses the [colorbrewer](http://colorbrewer2.org/) palettes. The website will tell you which ones are colorblind-safe. – alistaire Feb 22 '17 at 16:59
  • 1
    @Axeman That answer of Hadley's is also 6.5 years old - it actually came out about the same time as the first CRAN release of `reshape2` and predates `tidyr` by years. – Gregor Thomas Feb 22 '17 at 17:04
  • @Gregor, yeah I'm not blaming him, it's just unfortunate as that Q has 170k views and it will put some (not so patient) of those on the wrong path. – Axeman Feb 22 '17 at 17:06
  • Thank you, guys! Sorry I did not see the other post - think I was using the wrong search words when looking for it. I will try to reshape my data (I am really new to this, so I was just happy that I finally got a graph to work :-) ) and work from there. Will also check out the colorbrewer as suggested! – Biogitte Feb 23 '17 at 14:11

0 Answers0