当每条线使用不同的 x 和 y 值时,将图例添加到 ggplot2 图形

Add legend to ggplot2 graph when different x and y values are used for each line

我的问题与 Add legend to ggplot2 line plot 非常相似,除了我有多种颜色的情况 - 每种情况 1 种颜色,但它们没有绘制成共同的 x 轴值。相反,我有 2 个 x 轴值,使用哪个取决于条件。

示例数据:

# 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 脚本:

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()

如何为该图添加图例?我尝试将 colour = "[colour]" 放在 aes() 部分中,但是颜色在绘图或图例中没有正确匹配。

您可以为每个 geom_line 层使用 aes 内的 colour 参数,以及 scale_colour_manual 内的命名向量,以正确映射 geom_line层到 scale_colour_manual 像这样:

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()

很难通过不明确的列名准确地看到您想要什么 - 但这里有一种收集数据的方法。如果你只是手动添加多行,你将不得不 re-program 为以后添加的每个新 "condition" 做这个,这是不好的做法。

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()

你可以更进一步,使色阶连续,这样 "o25" 会是稍微亮一点的橙色,等等。