在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
...

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

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

我需要的是一个传奇,只有2004年之后展示的4种治疗方法。

我得到的是: 错误的传说

我想要得到的(理想情况下): 更好的传说

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

I've seen this : Controlling line color and line type in ggplot legend

But it will also add the 'ambient' and 'elevated' treatment, and so duplicate the straight lines in the legend. Thank you for your help.

jazzurro

这是为您提供的一种方法。鉴于上面的数据不足以复制您的图形,因此我创建了一个样本数据。我想感谢在此问题上发表答案的SO用户这篇文章的主要技巧是将相同的组分配给形状和线型。同样,在您的情况下,我需要对颜色和线型进行相同的设置。除此之外,还有一件事要做。我手动分配了特定的颜色和线型。在这里,最后有四个级别(即,处理1.AC,处理1.AE,处理2.EC,处理2.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))

enter image description here

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

在ggplot2中使用组,线型和颜色时使用单个图例?

ggplot2:组合组,颜色和线型

无法在 r 中使用 ggplot2 将组合 geom_point 和 _line 图的颜色和线型图例分开

如何将颜色和线型的图例组合成 ggplot 中的单个图例?

图例匹配 ggplot2 中的线型

在图例ggplot2中反映线型

ggplot2:创建图例,其中包括多个符号,线型和颜色

如何使用ggplot2中的geom_pointrange()自动消除线型图例的形状和图例的线型?

根据ggplot中的线型和颜色添加图例

在ggplot图例中控制线型,颜色和标签

如何更改 ggplot2 中的图例线型?

ggplot2中不同线型的图例条目

具有ggplot2中定义的形状,线型和标签的完整手动图例

带线型的R ggplot2图例

更改 ggplot 中图例线型的颜色

ggplot2 中的图例和图形颜色不匹配

ggplot:在图例中组合大小和颜色

将颜色转换为ggplot2中的线型/符号

在图例中获取线型虚线和颜色

如何在ggplot2中调整颜色形状组合的图例?

如何在ggplot2中为单个几何/颜色组合创建图例条目?

无法通过线型在ggplot2图例中分配自定义名称和样式

ggplot2 scale_color_identity() 如何改变线型和图例的设计?

为ggplot2(r)中的单个颜色框图/点定义图例和颜色

R:线型和颜色手册未显示在 ggplot 图例中

ggplot2中带有图例的geom_vline中的线型不正确

ggplot2 中的线条和图例

更改图例以指示线型...(ggplot2)