根据组在图例中添加平均线 - geom_hline

Posted

技术标签:

【中文标题】根据组在图例中添加平均线 - geom_hline【英文标题】:Add mean line in the legend based on groups - geom_hline 【发布时间】:2017-12-04 09:38:28 【问题描述】:

我想在我的情节中添加一个图例。我一直在努力解决的问题是我无法在图例中显示平均线。相反,我有一个基于类型的线点。 我想在图例中得到的是 WT/SHC 的点和相同颜色的平均 WT/SHC 的线。这可能吗?

这是我的数据:

    structure(list(vmr_temp = c(0.000651505232465427, 0.0053517614358332, 
0.0146827882122448, 0.00635539145671089, 0.0343575014942268, 
0.36174893678659, 0.0778899883350459, 0.289848135670948, 0.209959339680751, 
0.00405913025062444, 0.0145486338811137, 0.00400672957234268, 
0.0343676930196838, 0.105470036120101, 1.36478599015949, 0.00271301387141199, 
0.0146894961514807, 0.00662479099801861, 0.00633206013278967, 
0.0826430886815124, 0.000236124989922332, 0.000540625939805667, 
0.00346901600194693, 0.00494484509687755, 0.0109781756539075, 
0.00106492834562357, 0.00279831192505938, 0.000463409848285264, 
0.000652145384164669, 0.0336332381486342, 0.343360081948801, 
0.166577300901611, 0.357270077835276, 0.26380987699571, 0.00302256823040492, 
0.00104296967581199, 0.0120476175691888, 0.0292940823835766, 
0.0553962706206964, 0.519685248761818, 0.000817020240142651, 
0.00093258668979934, 0.000105471759211712, 0.0170464412705115, 
0.0481955561250614, 0.00107243214969894, 0.0172713764321815, 
0.0722947706678404, 0.00580093907914223, 0.000263741516896747
), lipids = structure(c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 2L, 
3L, 4L, 5L, 6L, 8L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
18L, 17L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 2L, 5L, 3L, 
4L), .Label = c("CL [66:0]", "CL [70:2]", "CL [70:4]", "CL [72:4]", 
"CL [74:2]", "PC [32:1]", "PC [32:2]", "PC [34:1]", "PC [34:2]", 
"PC [34:3]", "PC [34:4]", "PC [36:2]", "PC [36:3]", "PC [36:4]", 
"PE [32:1]", "PE [34:1]", "PE [34:2]", "PE [36:1]", "PE [36:2]", 
"PE [36:3]", "PG [32:1]", "PG [34:1]", "PG [34:2]", "PG [36:2]", 
"PG [36:3]"), class = "factor"), type = structure(c(2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("SHC", 
"WT"), class = "factor"), mean_temp = c(0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.106450191953034, 
0.106450191953034, 0.106450191953034, 0.106450191953034, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 
0.0781567385802059, 0.0781567385802059, 0.0781567385802059, 0.0781567385802059
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-50L), vars = "type", labels = structure(list(type = structure(1:2, .Label = c("SHC", 
"WT"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L), vars = "type", labels = structure(list(type = structure(1:2, .Label = c("SHC", 
"WT"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L), vars = "type", labels = structure(list(type = structure(1:2, .Label = c("SHC", 
"WT"), class = "factor")), row.names = c(NA, -2L), class = "data.frame", vars = "type", drop = TRUE, .Names = "type"), indices = list(
    25:49, 0:24), drop = TRUE, group_sizes = c(25L, 25L), biggest_group_size = 25L, .Names = "type"), indices = list(
    25:49, 0:24), drop = TRUE, group_sizes = c(25L, 25L), biggest_group_size = 25L, .Names = "type"), indices = list(
    25:49, 0:24), drop = TRUE, group_sizes = c(25L, 25L), biggest_group_size = 25L, .Names = c("vmr_temp", 
"lipids", "type", "mean_temp"))

到目前为止,这就是我所拥有的:

ggplot(test, aes(x = lipids, y= vmr_temp,  col= type)) + 
  geom_point() +
 #facet_grid(type ~ .) +
 geom_hline(aes(yintercept = mean_temp, group = type,color = factor(type)),show.legend = TRUE)  +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

【问题讨论】:

不清楚你想要什么。我们没有mean_temp。您是否希望为平均值提供单独的图例?因为在你的情节中似乎已经有了彩色线条。 嘿,对不起,我编辑了我的帖子,应该有一个包含平均值的列。是的,我想要一个单独的图例来解释这些行是什么。 【参考方案1】:

这是你要找的吗?

library(dplyr)
mean_dat <- test %>% group_by(type) %>% summarise(mean_temp = mean(vmr_temp))

ggplot(test, aes(x = lipids, y= vmr_temp,  col= type)) + 
  geom_point() +
  # facet_grid(type ~ .) +
  geom_hline(data = mean_dat, aes(yintercept = mean_temp, col = type))  +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

【讨论】:

对不起,我应该解释得更好。我需要的基本上是这个图: ggplot(test, aes(x = 脂质, y= vmr_temp, col= type)) + geom_point() + geom_hline(data = mean_temp_wt, aes(yintercept = mean_temp, col = "Mean WT" ), show.legend = TRUE) + geom_hline(data = mean_temp_shc, aes(yintercept = mean_temp, col = "Mean SHC"), show.legend = TRUE) + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 但它应该有一个标题“Legend”,只有“SHC”/“WT”的点和“Mean SHC”/“Mean WT”的行。我希望现在更清楚了【参考方案2】:

这并不容易,因为一个单一的美学有一个单一的传奇。不过我们可以作弊,用fill 表示分数,color 表示行数:

ggplot(test, aes(x = lipids, y = vmr_temp)) + 
  geom_point(aes(fill = type), shape = 21, col = 1, stroke = 0, size = 3) +
  geom_hline(aes(yintercept = mean_temp, col = type))  +
  scale_fill_discrete(name = '') +
  scale_color_discrete(name = 'mean values') +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

给我们:

【讨论】:

以上是关于根据组在图例中添加平均线 - geom_hline的主要内容,如果未能解决你的问题,请参考以下文章

如何为 geom_hline 添加自定义图例

图例中 geom_hline 的颜色不正确 [ggplot2]

在 geom_line 图中显示多个分组 geom_hline 的图例

在使用 gghighlight 时为几个 geom_hlines 创建图例

如何在同一个散点图上使用 geom_vline() 和 geom_hline 避免图例中的交叉效应?

ggplot:geom_vline 和 geom_hline 的单独图例