在ggplot2图例中组合线型和颜色

Y.C*_*och 5 r legend ggplot2

我有一个像这样的数据框:

> head(df_graph)
   treatment year      mean         se
1:        AC 2005 0.3626147 0.03005057
2:        AC 2006 0.3925465 0.02370335
3:        AC 2007 0.3217444 0.02279881
4:        AC 2008 0.3895656 0.05985077
5:        AC 2009 0.3820191 0.01481586
6:        AC 2010 0.3732695 0.03544626
...
Run Code Online (Sandbox Code Playgroud)

和一个(长)ggplot 脚本:

df_graph %>% 

  # way to make 2 lines becoming 4 in a smooth way
  filter(treatment %in% c("Ambient", "Elevated")) %>%
  mutate(treatment = ifelse(treatment == "Ambient", "AA", "EE")) %>%
  bind_rows(df_graph) %>%
  mutate(treatment_group = ifelse(treatment %in% c("Ambient", "AC", "AF", "AA"),"treatment1","treatment2"),
         line_type = ifelse(treatment %in% c("AA", "EE", "AF", "EF"),"type1","type2")) %>%

  # plot
  ggplot(aes(x = year, y = mean,group = interaction(treatment_group, line_type),color = treatment_group)) +
  geom_line(aes(x = year, y = mean, linetype = line_type),size = 1.5, lineend = "round") +
  geom_point(size=5)+
  geom_errorbar(aes(ymin = mean-se, ymax = mean+se),width = 0.2, size = 1.5)+

  # scaling visual
  scale_color_manual(values=c('blue1','red3'))+
  scale_linetype_manual(values = c('dashed', 'solid'))+
  scale_x_continuous(breaks = c(1999:2010), limits = c(1998.5, 2010.5),labels = 1999:2010)+

  # axes and legend
  labs(title ="", x="year", y = expression(paste("result")))+
  theme_classic() + theme(text = element_text(size=20))
Run Code Online (Sandbox Code Playgroud)

我这样做是为了让2004年后2个治疗可以变成4个。我的问题与我的传奇有关。通过运行此脚本,我得到一个“两部分”图例,其中 1) 颜色 (treatment_group) 和 2) 线类型 (line_type)。

我需要的是一个仅显示 2004 年之后的 4 种治疗方法的图例。

我得到什么: 错误的传说

我想得到什么(理想情况下): 更好的传奇

我意识到我的数据框不是最好的格式,但为了从 2004 年到 2005 年顺利过渡,这是我发现的唯一方法。因此,一个好的解决方案是更改 ggplot 脚本,而不是数据框的形状。

我见过这个:Controlling line color and line type in ggplot legend

但它还会添加“环境”和“升高”处理,从而复制图例中的直线。感谢您的帮助。

jaz*_*rro 4

这是适合您的一种方法。鉴于您上面的数据不足以重现您的图形,我创建了一个示例数据。我想感谢在此问题中发布答案的 SO 用户。这篇文章的关键技巧是将相同的组分配给形状和线型。同样,在您的情况下,我需要对颜色和线型执行相同的操作。除此之外,还有一件事要做。我手动指定了特定的颜色和线型。这里,最终有四个级别(即treatment1.AC、treatment1.AE、treatment2.EC、treatment2.EF)。但我使用interaction()并创建了八个级别。因此,我需要指定八种颜色和线条类型。当我为图例指定名称时,我意识到我需要在 和 中具有相同的scale_color_manual()名称scale_linetype_manual()

library(ggplot2)

set.seed(111)

mydf <- data.frame(year = rep(1999:2010, time = 4),
                   treatment.type = rep(c("AC", "AF", "EC", "EF"), each = 12),
                   treatment = rep(c("treatment1", "treatment2"), each = 24),
                   mean = c(runif(min = 0.3, max = 0.55, 12),
                            rep(NA, 5), runif(min = 0.3, max = 0.55, 7),
                            runif(min = 0.3, max = 0.55, 12),
                            rep(NA, 5), runif(min = 0.3, max = 0.55, 7)),
                   se = c(runif(min = 0.01, max = 0.03, 12),
                           rep(NA, 5), runif(min = 0.01, max = 0.03, 7),
                           runif(min = 0.01, max = 0.03, 12),
                           rep(NA, 5), runif(min = 0.01, max = 0.03, 7)),
                   stringsAsFactors = FALSE)


ggplot(data = mydf, aes(x = year, y = mean,
                    color = interaction(treatment, treatment.type),
                    linetype = interaction(treatment, treatment.type))) +
geom_point(show.legend = FALSE) +
geom_line() +
geom_errorbar(aes(ymin = mean-se, ymax = mean+se),width = 0.1, size = 0.5) +
scale_color_manual(name = "Treatment conditions", values = rep(c("blue", "blue", "red", "red"), times = 2)) +
scale_linetype_manual(name = "Treatment conditions", values = rep(c(1,2), times = 4))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述