gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:04:49 26 4
gpt4 key购买 nike

我有一个这样的数据框:

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

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

我需要的是一个图例,其中仅显示 2004 年之后的 4 次治疗。

我得到的:
wrong legend

我想得到什么(理想情况下):
better legend

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

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

但它也会添加“环境”和“高架”处理,因此复制图例中的直线。感谢您的帮助。

最佳答案

这是适合您的一种方法。鉴于以上数据不足以重现您的图形,我创建了一个示例数据。我想感谢在 this question 中发布答案的 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

关于r - 在ggplot2图例中组合线型和颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48450762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com