-1

My question is very similar to Add legend to ggplot2 line plot except I have the situation where I have multiple colours - 1 for each condition, but they aren't plotted to a common x-axis value. Instead, I have 2 x-axis values and the one used depends on the condition.

Example Data:

# dataframe name = df

e0 e200 o0 o50 o200
0.5715707 0.5755010 0.9151736 0.8858229 1.2488826
0.5570928 0.5610231 0.8724417 0.8851889 1.2135041
0.5430821 0.5470124 0.8692482 0.8793603 1.2051914
0.5295093 0.5334396 0.8251555 0.8636917 1.0951763
0.5163479 0.5202782 0.8149114 0.8519220 1.0787246
0.5035736 0.5075039 0.7875460 0.7521003 1.0655470
0.4911643 0.4950946 0.7724218 0.7394516 1.0616154
0.4790998 0.4830301 0.7306038 0.6997307 1.0214771
0.4673614 0.4712917 0.6373668 0.6333903 0.9179331
0.4559320 0.4598622 0.5898641 0.6314342 0.8713423
0.4447956 0.4487259 0.5870693 0.6266098 0.8208793

R Script:

ggplot(df) +
geom_line(aes(x = e0,y = o0), size = 2, colour = "red") +
geom_line(aes(x = e0,y = o50), size = 2, colour = "orange") +
geom_line(aes(x = e200,y = o200), size = 2, colour = "gold") +
scale_colour_manual(name="Condition", 
   values=c("red", "orange", "gold"),
   labels = c("0", "50", "200")) +
    theme_classic()

How can I add a legend to this plot? I tried putting the colour = "[colour]" within the aes() section but then the colours don't match up correctly on the plot or in the legend.

Bob
  • 295
  • 2
  • 4
  • 14

2 Answers2

5

Quite hard to see exactly what you want with the ambiguous column names - but here's a way to gather the data. if you just add multiple lines manually, you will have to re-program this for each new "condition" added in the future, which is bad practice.

library(ggplot2)
library(tidyr)
library(dplyr)

df_raw <- read.delim(
  text = "e0 e200 o0 o50 o200
0.5715707 0.5755010 0.9151736 0.8858229 1.2488826
0.5570928 0.5610231 0.8724417 0.8851889 1.2135041
0.5430821 0.5470124 0.8692482 0.8793603 1.2051914
0.5295093 0.5334396 0.8251555 0.8636917 1.0951763
0.5163479 0.5202782 0.8149114 0.8519220 1.0787246
0.5035736 0.5075039 0.7875460 0.7521003 1.0655470
0.4911643 0.4950946 0.7724218 0.7394516 1.0616154
0.4790998 0.4830301 0.7306038 0.6997307 1.0214771
0.4673614 0.4712917 0.6373668 0.6333903 0.9179331
0.4559320 0.4598622 0.5898641 0.6314342 0.8713423
0.4447956 0.4487259 0.5870693 0.6266098 0.8208793",
  sep = " ",
  header = TRUE
)


df1 <- df_raw %>%
  select(c(e0, o0, o50)) %>% 
  gather(o, y, o0:o50) %>% 
  mutate(e = "e0") %>% 
  rename(x = e0)


df2 <- df_raw %>% 
  select(c(e200, o200)) %>% 
  rename(x = e200, y = o200) %>% 
  mutate(e = "e200", o = "o200")


bind_rows(df1, df2) %>% 
  ggplot(aes(x, y, color = o)) + 
  geom_line(size = 2) + 
  scale_colour_manual(name="Condition", 
                      values=c(o0 = "red", o50 = "orange", o200 = "gold"), 
                      breaks = c("o0", "o50", "o200"))+
  theme_classic()

enter image description here

You could take this further and make the color scale continuous, such that "o25" would be a slightly lighter orange, etc.

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
  • Thanks @Jack Brookes. This works except I had to convert the Condition column to a factor and relevel it to get the colours in the right order. – Bob Mar 24 '18 at 18:12
1

You can use the colour argument inside aes for each geom_line layer, along with a named vector inside scale_colour_manual, to correctly map geom_line layers to scale_colour_manual like so:

ggplot(df) +
  geom_line(aes(x = e0, y = o0, colour = "0"), size = 2) +
  geom_line(aes(x = e0, y = o50, colour = "50"), size = 2) +
  geom_line(aes(x = e200, y = o200, colour = "200"), size = 2) +
  scale_colour_manual(name = "Condition",
                      values = c("0" = "red", "50" = "orange", "200" = "gold"),
                      breaks = c("0", "50", "200"),
                      guide = "legend") +
  theme_classic()

ggplot output

cmaher
  • 5,100
  • 1
  • 22
  • 34
  • Thanks @cmaher except you have done what I have already tried. The colours in the legend are in the right order but not on the plot itself. The top line should be gold - not red. – Bob Mar 24 '18 at 17:53
  • @Bob then why was your question "How can I add a legend to this plot?". Try to avoid burying the lede in your question. In any event, please see the revised answer for the simplest way to fix your problem. – cmaher Mar 24 '18 at 18:33