绘制许多组的平均值 - 如何修复分组和标签?

时间:2017-07-20 12:23:28

标签: r ggplot2

我想知道是否可以绘制一条反映df中特定行的平均值的线。我有一组脂质,我想从WT / SHC的色谱柱代表中强调pH = 7的平均值。我几乎设法做到了,问题是线应该更短并显示特定脂质的平均值。并且在图例中,点应仅代表脂质的类型,线应呈现标准的平均值。

这是我的情节: enter image description here

这是我正在使用的数据:

test_cl <- structure(list(type = c("WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC", "WT", "WT", "WT", "WT", "WT", "SHC", 
"SHC", "SHC", "SHC", "SHC"), lipids = structure(c(3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 
5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 
4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 
5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 
4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 
5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 
4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 
5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 
4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 
5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 
4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 
6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 
7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 
3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L, 3L, 5L, 4L, 6L, 7L), .Label = c("DIP", 
"mDIP", "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"), rep = c("early1", "early1", "early1", "early1", 
"early1", "early1", "early1", "early1", "early1", "early1", "early2", 
"early2", "early2", "early2", "early2", "early2", "early2", "early2", 
"early2", "early2", "early3", "early3", "early3", "early3", "early3", 
"early3", "early3", "early3", "early3", "early3", "mid1", "mid1", 
"mid1", "mid1", "mid1", "mid1", "mid1", "mid1", "mid1", "mid1", 
"mid2", "mid2", "mid2", "mid2", "mid2", "mid2", "mid2", "mid2", 
"mid2", "mid2", "mid3", "mid3", "mid3", "mid3", "mid3", "mid3", 
"mid3", "mid3", "mid3", "mid3", "late1", "late1", "late1", "late1", 
"late1", "late1", "late1", "late1", "late1", "late1", "late2", 
"late2", "late2", "late2", "late2", "late2", "late2", "late2", 
"late2", "late2", "late3", "late3", "late3", "late3", "late3", 
"late3", "late3", "late3", "late3", "late3", "stat1", "stat1", 
"stat1", "stat1", "stat1", "stat1", "stat1", "stat1", "stat1", 
"stat1", "stat2", "stat2", "stat2", "stat2", "stat2", "stat2", 
"stat2", "stat2", "stat2", "stat2", "stat3", "stat3", "stat3", 
"stat3", "stat3", "stat3", "stat3", "stat3", "stat3", "stat3", 
"pH 5.5 1", "pH 5.5 1", "pH 5.5 1", "pH 5.5 1", "pH 5.5 1", "pH 5.5 1", 
"pH 5.5 1", "pH 5.5 1", "pH 5.5 1", "pH 5.5 1", "pH 5.5 2", "pH 5.5 2", 
"pH 5.5 2", "pH 5.5 2", "pH 5.5 2", "pH 5.5 2", "pH 5.5 2", "pH 5.5 2", 
"pH 5.5 2", "pH 5.5 2", "pH 5.5 3", "pH 5.5 3", "pH 5.5 3", "pH 5.5 3", 
"pH 5.5 3", "pH 5.5 3", "pH 5.5 3", "pH 5.5 3", "pH 5.5 3", "pH 5.5 3", 
"pH 6 1", "pH 6 1", "pH 6 1", "pH 6 1", "pH 6 1", "pH 6 1", "pH 6 1", 
"pH 6 1", "pH 6 1", "pH 6 1", "pH 6 2", "pH 6 2", "pH 6 2", "pH 6 2", 
"pH 6 2", "pH 6 2", "pH 6 2", "pH 6 2", "pH 6 2", "pH 6 2", "pH 6 3", 
"pH 6 3", "pH 6 3", "pH 6 3", "pH 6 3", "pH 6 3", "pH 6 3", "pH 6 3", 
"pH 6 3", "pH 6 3", "pH 7 1", "pH 7 1", "pH 7 1", "pH 7 1", "pH 7 1", 
"pH 7 1", "pH 7 1", "pH 7 1", "pH 7 1", "pH 7 1", "pH 7 2", "pH 7 2", 
"pH 7 2", "pH 7 2", "pH 7 2", "pH 7 2", "pH 7 2", "pH 7 2", "pH 7 2", 
"pH 7 2", "pH 7 3", "pH 7 3", "pH 7 3", "pH 7 3", "pH 7 3", "pH 7 3", 
"pH 7 3", "pH 7 3", "pH 7 3", "pH 7 3", "pH 7.5 1", "pH 7.5 1", 
"pH 7.5 1", "pH 7.5 1", "pH 7.5 1", "pH 7.5 1", "pH 7.5 1", "pH 7.5 1", 
"pH 7.5 1", "pH 7.5 1", "pH 7.5 2", "pH 7.5 2", "pH 7.5 2", "pH 7.5 2", 
"pH 7.5 2", "pH 7.5 2", "pH 7.5 2", "pH 7.5 2", "pH 7.5 2", "pH 7.5 2", 
"pH 7.5 3", "pH 7.5 3", "pH 7.5 3", "pH 7.5 3", "pH 7.5 3", "pH 7.5 3", 
"pH 7.5 3", "pH 7.5 3", "pH 7.5 3", "pH 7.5 3", "13C 1", "13C 1", 
"13C 1", "13C 1", "13C 1", "13C 1", "13C 1", "13C 1", "13C 1", 
"13C 1", "13C 2", "13C 2", "13C 2", "13C 2", "13C 2", "13C 2", 
"13C 2", "13C 2", "13C 2", "13C 2", "13C 3", "13C 3", "13C 3", 
"13C 3", "13C 3", "13C 3", "13C 3", "13C 3", "13C 3", "13C 3", 
"20C 1", "20C 1", "20C 1", "20C 1", "20C 1", "20C 1", "20C 1", 
"20C 1", "20C 1", "20C 1", "20C 2", "20C 2", "20C 2", "20C 2", 
"20C 2", "20C 2", "20C 2", "20C 2", "20C 2", "20C 2", "20C 3", 
"20C 3", "20C 3", "20C 3", "20C 3", "20C 3", "20C 3", "20C 3", 
"20C 3", "20C 3", "30C 1", "30C 1", "30C 1", "30C 1", "30C 1", 
"30C 1", "30C 1", "30C 1", "30C 1", "30C 1", "30C 2", "30C 2", 
"30C 2", "30C 2", "30C 2", "30C 2", "30C 2", "30C 2", "30C 2", 
"30C 2", "30C 3", "30C 3", "30C 3", "30C 3", "30C 3", "30C 3", 
"30C 3", "30C 3", "30C 3", "30C 3", "NaCl 0.05 1", "NaCl 0.05 1", 
"NaCl 0.05 1", "NaCl 0.05 1", "NaCl 0.05 1", "NaCl 0.05 1", "NaCl 0.05 1", 
"NaCl 0.05 1", "NaCl 0.05 1", "NaCl 0.05 1", "NaCl 0.05 2", "NaCl 0.05 2", 
"NaCl 0.05 2", "NaCl 0.05 2", "NaCl 0.05 2", "NaCl 0.05 2", "NaCl 0.05 2", 
"NaCl 0.05 2", "NaCl 0.05 2", "NaCl 0.05 2", "NaCl 0.05 3", "NaCl 0.05 3", 
"NaCl 0.05 3", "NaCl 0.05 3", "NaCl 0.05 3", "NaCl 0.05 3", "NaCl 0.05 3", 
"NaCl 0.05 3", "NaCl 0.05 3", "NaCl 0.05 3", "NaCl 0.1 1", "NaCl 0.1 1", 
"NaCl 0.1 1", "NaCl 0.1 1", "NaCl 0.1 1", "NaCl 0.1 1", "NaCl 0.1 1", 
"NaCl 0.1 1", "NaCl 0.1 1", "NaCl 0.1 1", "NaCl 0.1 2", "NaCl 0.1 2", 
"NaCl 0.1 2", "NaCl 0.1 2", "NaCl 0.1 2", "NaCl 0.1 2", "NaCl 0.1 2", 
"NaCl 0.1 2", "NaCl 0.1 2", "NaCl 0.1 2", "NaCl 0.1 3", "NaCl 0.1 3", 
"NaCl 0.1 3", "NaCl 0.1 3", "NaCl 0.1 3", "NaCl 0.1 3", "NaCl 0.1 3", 
"NaCl 0.1 3", "NaCl 0.1 3", "NaCl 0.1 3", "MetOH 0.1 1", "MetOH 0.1 1", 
"MetOH 0.1 1", "MetOH 0.1 1", "MetOH 0.1 1", "MetOH 0.1 1", "MetOH 0.1 1", 
"MetOH 0.1 1", "MetOH 0.1 1", "MetOH 0.1 1", "MetOH 0.1 2", "MetOH 0.1 2", 
"MetOH 0.1 2", "MetOH 0.1 2", "MetOH 0.1 2", "MetOH 0.1 2", "MetOH 0.1 2", 
"MetOH 0.1 2", "MetOH 0.1 2", "MetOH 0.1 2", "MetOH 0.1 3", "MetOH 0.1 3", 
"MetOH 0.1 3", "MetOH 0.1 3", "MetOH 0.1 3", "MetOH 0.1 3", "MetOH 0.1 3", 
"MetOH 0.1 3", "MetOH 0.1 3", "MetOH 0.1 3", "MetOH 1 1", "MetOH 1 1", 
"MetOH 1 1", "MetOH 1 1", "MetOH 1 1", "MetOH 1 1", "MetOH 1 1", 
"MetOH 1 1", "MetOH 1 1", "MetOH 1 1", "MetOH 1 2", "MetOH 1 2", 
"MetOH 1 2", "MetOH 1 2", "MetOH 1 2", "MetOH 1 2", "MetOH 1 2", 
"MetOH 1 2", "MetOH 1 2", "MetOH 1 2", "MetOH 1 3", "MetOH 1 3", 
"MetOH 1 3", "MetOH 1 3", "MetOH 1 3", "MetOH 1 3", "MetOH 1 3", 
"MetOH 1 3", "MetOH 1 3", "MetOH 1 3"), num = c(0.009523686, 
0.043189398, 0.420979104, 0.246671197, 3.451885409, 0.015840778, 
0.066796591, 0.711819877, 0.266602215, 3.454463141, 0.007613802, 
0.046278008, 0.392051405, 0.25502036, 3.159879284, 0.023401093, 
0.083028102, 0.496309773, 0.312034391, 2.537644531, 0.011478203, 
0.054305349, 0.464326108, 0.307066853, 3.6602462, 0.022539611, 
0.088932716, 0.561775816, 0.306176596, 2.67694279, 0.010537286, 
0.04003825, 0.433807129, 0.265128974, 3.793074386, 0.033332936, 
0.125527261, 0.665685118, 0.433383167, 3.079080661, 0.007334728, 
0.050505078, 0.380642914, 0.297303594, 3.223705784, 0.028779332, 
0.133574985, 0.627769179, 0.44754806, 2.958721891, 0.006611086, 
0.030062788, 0.368471191, 0.196684816, 3.41357708, 0.029912455, 
0.123199878, 0.725256378, 0.429472199, 3.320616551, 0.01118598, 
0.079753642, 0.42517786, 0.475724091, 3.136961558, 0.02541216, 
0.092255666, 0.700284371, 0.310205814, 3.021941072, 0.011543492, 
0.074750731, 0.436643281, 0.41869653, 3.16178206, 0.016723165, 
0.107711653, 0.478617926, 0.327257309, 2.187418617, 0.014526945, 
0.083030295, 0.421018391, 0.453286503, 3.034308389, 0.013312235, 
0.094337923, 0.604186915, 0.319925114, 2.710050374, 0.018963327, 
0.134905674, 0.324578481, 0.675510653, 2.075633975, 0.048011844, 
0.173339125, 0.671572835, 0.410361322, 1.686563816, 0.020817582, 
0.138788879, 0.322903267, 0.686792019, 2.160872891, 0.045915999, 
0.186073834, 0.623858447, 0.427157351, 1.630243088, 0.02334196, 
0.141525911, 0.326552113, 0.705404966, 2.160032852, 0.056147482, 
0.177409855, 0.776909273, 0.40933411, 1.976242185, 0.004633127, 
0.148588149, 0.486888237, 0.692898358, 2.642813555, 0.04514786, 
0.293542177, 0.628124318, 0.92269088, 2.427045621, 0.009330446, 
0.157310452, 0.389913986, 0.69311545, 2.373673687, 0.032803203, 
0.293311922, 0.512466096, 0.94176273, 1.966838355, 0.016920389, 
0.153930147, 0.407931604, 0.66933255, 2.43192777, 0.045960156, 
0.270842741, 0.599652483, 0.820429999, 2.189575381, 0.017778907, 
0.118991857, 0.472321709, 0.613337385, 3.115415618, 0.028992327, 
0.229761789, 0.492154055, 0.842800722, 2.242849613, 0.023182626, 
0.119038523, 0.582892594, 0.597699301, 3.779952255, 0.03861396, 
0.233904848, 0.569834529, 0.811853695, 2.573955654, 0.023031255, 
0.119210287, 0.572679168, 0.611935847, 3.685191127, 0.04358965, 
0.334758071, 0.736448747, 1.231148933, 3.280921801, 0.007902522, 
0.098307685, 0.616613495, 0.437905544, 4.150185864, 0.01725995, 
0.154284081, 0.709570519, 0.426857487, 2.970822428, 0.005021786, 
0.092351365, 0.331772789, 0.476369777, 2.082020853, 0.018188753, 
0.136003722, 0.569063573, 0.454751015, 2.296572566, 0.006793196, 
0.090945613, 0.410991263, 0.449364368, 2.536806135, 0.032270599, 
0.150618359, 0.759625702, 0.447729593, 3.01282956, 0.007153489, 
0.10780106, 0.574597942, 0.476387687, 2.915254622, 0.06383629, 
0.264258274, 0.548375265, 0.711833684, 2.155158445, 0.017068877, 
0.118768278, 0.599951373, 0.477224531, 3.186731876, 0.026518865, 
0.26922641, 0.682501252, 0.753471226, 2.681314829, 0.014697648, 
0.11854954, 0.537912378, 0.532150804, 2.962629872, 0.02748779, 
0.262262926, 0.701643907, 0.730112753, 2.943014213, 0.001144995, 
0.108661125, 0.41072655, 0.401729495, 3.349649409, 0.004814308, 
0.138971717, 0.351954191, 0.397538279, 1.810974482, 0.000854878, 
0.076091557, 0.317566463, 0.265579988, 2.697381593, 0.006671769, 
0.143615213, 0.375693318, 0.432116471, 1.964832118, 0.001387258, 
0.089433716, 0.347318908, 0.310210558, 2.831744905, 0.011747384, 
0.161909067, 0.483097774, 0.434049624, 2.495174754, 0.002421467, 
0.052017808, 0.32257307, 0.321733033, 2.913409569, 0.006364221, 
0.108896499, 0.400344628, 0.45083928, 2.612358054, 0.002088531, 
0.062392598, 0.345417305, 0.376732525, 3.148227718, 0.007314019, 
0.115997721, 0.382326299, 0.456267995, 2.548147974, 0.002197505, 
0.056822917, 0.329202368, 0.327668171, 3.060426171, 0.004130378, 
0.095727468, 0.256862326, 0.449779773, 1.707117768, 0.009523686, 
0.043189398, 0.420979104, 0.246671197, 3.451885409, 0.015840778, 
0.066796591, 0.711819877, 0.266602215, 3.454463141, 0.007613802, 
0.046278008, 0.392051405, 0.25502036, 3.159879284, 0.023401093, 
0.083028102, 0.496309773, 0.312034391, 2.537644531, 0.011478203, 
0.054305349, 0.464326108, 0.307066853, 3.6602462, 0.022539611, 
0.088932716, 0.561775816, 0.306176596, 2.67694279, 0.013238195, 
0.116731516, 0.513870128, 0.714466207, 4.13215759, 0.017830501, 
0.256775249, 0.566607585, 0.768765952, 2.099092988, 0.005996389, 
0.131612407, 0.391253262, 0.807713856, 3.192910963, 0.010803128, 
0.267512693, 0.49323859, 0.809247089, 1.75861532, 0.007420278, 
0.130344538, 0.370661217, 0.821807125, 3.157958777, 0.027113392, 
0.260864254, 0.697775301, 0.828808324, 2.503637112, 0.008109203, 
0.126053618, 0.424421426, 0.736527329, 3.138838862, 0.030241241, 
0.401973349, 0.749110588, 0.982360012, 1.95767926, 0.008821784, 
0.120380007, 0.445323375, 0.698091901, 3.2819844, 0.035769782, 
0.383012382, 0.745898962, 0.940316819, 2.021141233, 0.009709852, 
0.119421859, 0.460181194, 0.662124848, 3.418103048, 0.035575143, 
0.408529835, 0.817553592, 0.979699253, 2.191139604, 0.014092665, 
0.088921329, 0.400118909, 0.533636754, 3.211855928, 0.023826824, 
0.21030273, 0.450340544, 0.813043891, 2.450184786, 0.002871928, 
0.093172622, 0.313849684, 0.56060567, 2.618043655, 0.022990767, 
0.190713274, 0.542822273, 0.771433207, 2.977290039, 0.018301729, 
0.086026637, 0.440453336, 0.560300392, 3.600196218, 0.026158589, 
0.176415047, 0.514817064, 0.713635504, 2.745529632, 0.019531106, 
0.093643383, 0.419362398, 0.54614676, 3.085470045, 0.028815782, 
0.187065712, 0.520767972, 0.645079007, 2.374529212, 0.021278598, 
0.099665663, 0.412005508, 0.60694041, 3.16902752, 0.041771475, 
0.171868691, 0.517726713, 0.580779114, 2.261748875, 0.01654279, 
0.104298166, 0.362437548, 0.602720163, 2.799936307, 0.033620427, 
0.203394035, 0.589582994, 0.690476977, 2.759532167), groups = c("Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Growth stage", 
"Growth stage", "Growth stage", "Growth stage", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", 
"Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Ph", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "Temperature", "Temperature", 
"Temperature", "Temperature", "Temperature", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", "NaCl", 
"NaCl", "NaCl", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH", 
"MetOH", "MetOH", "MetOH", "MetOH", "MetOH", "MetOH")), row.names = c(NA, 
-450L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("type", 
"lipids", "rep", "num", "groups"))

到目前为止我的代码:

ph7 <- test_cl
ph7$rep<-sub("X","",ph7$rep)
ph7$rep<-sub("\\d$","",ph7$rep)
ph7$rep<-sub("\\.$","",ph7$rep)
ph7 <- ph7 %>% group_by(type, lipids, groups, rep)%>% summarise_all( funs(mean(.)))

ggplot(test_cl, aes(x = factor(lipids), y= num, col = type)) + 
  geom_point(size = 0.4, position = position_dodge(width = 0.3))+
  geom_hline(data = filter(ph7, grepl('pH 7 ', rep)),aes(yintercept = num ,col = type)) +
  scale_fill_discrete(name = '') +
  scale_color_discrete(name = 'standard') +
  coord_flip() +
  theme_bw() +
  theme( panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         axis.text.x=element_text(angle=0,hjust=1),
         text = element_text(size=5)
        ) +
  scale_colour_manual(values=c("WT" = "grey50", "SHC" = "tomato") ) +
  scale_x_discrete(expand=c(0, 0.9))

我试图使用stat_summary,但我不知道如何将点更改为短线(没有连接)以及如何修复那里的图例。

ph7 <- test_cl
ph7$rep<-sub("X","",ph7$rep)
ph7$rep<-sub("\\d$","",ph7$rep)
ph7$rep<-sub("\\.$","",ph7$rep)
ph7 <- ph7 %>% group_by(type, lipids, groups, rep)%>% summarise_all( funs(mean(.)))

ggplot(test_cl, aes(x = factor(lipids), y= num, col = type)) + 
  geom_point(size = 0.4, position = position_dodge(width = 0.3))+
  #geom_hline(data = filter(ph7, grepl('pH 7 ', rep)),aes(yintercept = num ,col = type)) +
  scale_fill_discrete(name = '') +
  scale_color_discrete(name = 'standard') +
  #geom_point(data = ph7,aes(col = type), size= 0.7, position = position_dodge(width = 1)) +
  stat_summary(data = filter(ph7,grepl('pH 7 ', rep)), fun.y=mean, geom="point", lwd=2, aes(group=factor(type)), show.legend= TRUE,position = position_dodge(width = 0.3)) +
  coord_flip() +
  theme_bw() +
  theme( panel.grid.major = element_blank(),
         panel.grid.minor = element_blank(),
         axis.text.x=element_text(angle=0,hjust=1),
         text = element_text(size=5)
        ) +
  scale_colour_manual(values=c("WT" = "grey50", "SHC" = "tomato") ) +
  scale_x_discrete(expand=c(0, 0.9))

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以像这样使用geom_segment

定义开始和结束x以及开始和结束y职位

positions <- data.frame(x1=c(0.8),
                       x2=c(1.0),
                       y1=filter(ph7, grepl('pH 7 ', rep))$num[1],
                       y2=filter(ph7, grepl('pH 7 ', rep))$num[1],
                       label=filter(ph7, grepl('pH 7 ', rep))$type[1])

然后在线添加geom_segment

ggplot(test_cl, aes(x = factor(lipids), y= num, col = type)) + 
   geom_point(size = 0.4, position = position_dodge(width = 0.3))+
geom_segment(aes(x=x1,y=y1,xend=x2,yend=y2,colour=label),lwd=1,data=positions)

我没有添加您的所有值,因为您需要适当地格式化filter(ph7, grepl('pH 7 ', rep)),以及每行的起始和结束位置。