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

时间:2017-06-30 12:13:06

标签: r ggplot2 geom-hline

我想在我的情节中添加一个传奇。我一直在努力解决的问题是我无法在图例中显示平均线。相反,我有一个基于类型的线的点。 我想在传奇中得到的是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)) 

2 个答案:

答案 0 :(得分:1)

这并不是特别容易,因为单一的审美获得了一个传奇。我们可以作弊,使用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)) 

给我们:

enter image description here

答案 1 :(得分:0)

这是你在找什么?

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