我有一个像这样的数据框:
> 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.
这是为您提供的一种方法。鉴于上面的数据不足以复制您的图形,因此我创建了一个样本数据。我想感谢在此问题上发表答案的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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句