【发布时间】:2021-08-10 19:07:06
【问题描述】:
我正在尝试生成一个分组的 geom_line 图,其中两个分组的 geom_hline 重叠。所有 3 例中的分组都相同。问题是图例只出现在 geom_line (+ geom_point),而不是两个 geom_hline 命令。理想的结果是 3 个图例,每个图例有 3 条线代表每个组(geom_hline 具有相关的虚线/点线)。或者,在 geom_hline 命令的颜色变量中显示黑色虚线和虚线的附加图例也可以工作。数据、代码和绘图如下所示,提前感谢您的帮助!
数据(输入):
acc.df <- structure(list(intersect.nn = structure(c(1L, 2L, 3L, 1L, 2L,
3L, 1L, 2L, 3L), .Label = c("CD4", "CD8", "Treg"), class = "factor"),
prop = c(0.689912280701754, 0.999746643020015, 0.779661016949153,
0.671249111163783, 0.988757981832899, 0.769230769230769,
0.666112680737909, 0.981778644271146, 0.776223776223776),
K = c("k 1", "k 1", "k 1", "k 2", "k 2", "k 2", "k 3", "k 3",
"k 3")), row.names = c(NA, -9L), class = "data.frame")
acc.exp <- structure(list(intersect.exp = structure(1:3, .Label = c("CD4",
"CD8", "Treg"), class = "factor"), prop = c(0.689912280701754,
0.999746610921069, 0.779661016949153)), row.names = c(1L, 5L,
9L), class = "data.frame")
acc.clust <- structure(list(seurat_clusters = structure(1:3, .Label = c("CD4",
"CD8", "Treg"), class = "factor"), prop = c(0.666275954454119,
0.981845461365341, 0.774647887323944)), row.names = c(1L, 5L,
9L), class = "data.frame")
代码:
ggplot(acc.df, aes(x = K, y = prop, group = intersect.nn)) +
geom_line(aes(color = intersect.nn)) +
geom_point(aes(color = intersect.nn)) +
geom_hline(data = acc.exp, aes(yintercept = prop, color = intersect.exp), show.legend = T, linetype = "dashed") +
geom_hline(data = acc.clust, aes(yintercept = prop, color = seurat_clusters), show.legend = T, linetype = "dotted")
【问题讨论】: