我正在构建一个具有离散 Y 轴的图,并进行多个成对比较。然而,我没有使用文本作为标签,而是创建了一个辅助图,其中的颜色点指示参与比较的 2 个组,为了清晰起见,用一个线段连接两个点。见左图:
我用来生成左侧图的代码是:
p_left <- ggplot(tb_deg) +
geom_segment(aes(y=comparison, x=var1, xend=var2), size=2, color="slategrey", show.legend = F) +
geom_point(aes(y=comparison, x=var1, fill=var1), shape=21, size=5, show.legend = F) +
geom_point(aes(y=comparison, x=var2, fill=var2), shape=21, size=5, show.legend = F) +
scale_fill_manual(name="Group",
values=c('ControlD1' = "limegreen",
'HSP90iD1' = "firebrick1",
'PRRTD1' = "deepskyblue",
'CombinationD1' = "orchid1",
'HSP90iD3' = "firebrick4",
'PRRTD3' = "deepskyblue3",
'CombinationD3' = "purple3")
) +
scale_x_discrete(limits=rev(levels(md$group)) ) +
theme_void() +
labs(x=NULL, y=NULL, title = NULL) +
theme(legend.position = "none",
aspect.ratio = 2,
axis.text.x = element_text(angle=90, hjust=1),
axis.text.y = element_blank())
现在我正在将连接线段设置为灰色,但我想知道是否可以将该线段设置为两个连接点的颜色之间的渐变。是的,它可能会很难看,我不会在最终的图中使用它,但我想尝试一下。
我一直在寻找如何做到这一点,但我发现的所有解决方案都使用固定(渐变)色标,该色标对于所有绘制的段都相同(主要使用 geom_link)。我找不到任何片段尖端颜色取决于变量的示例。理想情况下,我会寻找类似的东西
... + geom_segment(aes(y=comparison, x=var1, xend=var2,
***color_start=var1, color_end=var2***) ) + ...
有什么方法可以做到这一点,而无需手动为 15 次比较中的每一个比较编码具有不同比例的渐变吗?
谢谢,
编辑:数据:
> dput(unique(tb_deg[,c("comparison","var1","var2")]))
structure(list(comparison = structure(c(3L, 7L, 8L, 15L, 6L,
10L, 11L, 1L, 4L, 13L, 2L, 9L, 5L, 12L, 14L), levels = c("without001 - HSP90iD1_vs_ControlD1",
"without001 - PRRTD1_vs_ControlD1", "without001 - CombinationD1_vs_ControlD1",
"without001 - HSP90iD3_vs_ControlD1", "without001 - PRRTD3_vs_ControlD1",
"without001 - CombinationD3_vs_ControlD1", "without001 - CombinationD1_vs_HSP90iD1",
"without001 - CombinationD1_vs_PRRTD1", "without001 - PRRTD1_vs_HSP90iD1",
"without001 - CombinationD3_vs_HSP90iD3", "without001 - CombinationD3_vs_PRRTD3",
"without001 - PRRTD3_vs_HSP90iD3", "without001 - HSP90iD3_vs_HSP90iD1",
"without001 - PRRTD3_vs_PRRTD1", "without001 - CombinationD3_vs_CombinationD1"
), class = "factor"), var1 = structure(c(4L, 4L, 4L, 7L, 7L,
7L, 7L, 2L, 5L, 5L, 3L, 3L, 6L, 6L, 6L), levels = c("ControlD1",
"HSP90iD1", "PRRTD1", "CombinationD1", "HSP90iD3", "PRRTD3",
"CombinationD3"), class = "factor"), var2 = structure(c(1L, 2L,
3L, 4L, 1L, 5L, 6L, 1L, 1L, 2L, 1L, 2L, 1L, 5L, 3L), levels = c("ControlD1",
"HSP90iD1", "PRRTD1", "CombinationD1", "HSP90iD3", "PRRTD3",
"CombinationD3"), class = "factor")), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list(
comparison = structure(1:15, levels = c("without001 - HSP90iD1_vs_ControlD1",
"without001 - PRRTD1_vs_ControlD1", "without001 - CombinationD1_vs_ControlD1",
"without001 - HSP90iD3_vs_ControlD1", "without001 - PRRTD3_vs_ControlD1",
"without001 - CombinationD3_vs_ControlD1", "without001 - CombinationD1_vs_HSP90iD1",
"without001 - CombinationD1_vs_PRRTD1", "without001 - PRRTD1_vs_HSP90iD1",
"without001 - CombinationD3_vs_HSP90iD3", "without001 - CombinationD3_vs_PRRTD3",
"without001 - PRRTD3_vs_HSP90iD3", "without001 - HSP90iD3_vs_HSP90iD1",
"without001 - PRRTD3_vs_PRRTD1", "without001 - CombinationD3_vs_CombinationD1"
), class = "factor"), var1 = structure(c(2L, 3L, 4L, 5L,
6L, 7L, 4L, 4L, 3L, 7L, 7L, 6L, 5L, 6L, 7L), levels = c("ControlD1",
"HSP90iD1", "PRRTD1", "CombinationD1", "HSP90iD3", "PRRTD3",
"CombinationD3"), class = "factor"), .rows = structure(list(
8L, 11L, 1L, 9L, 13L, 5L, 2L, 3L, 12L, 6L, 7L, 14L, 10L,
15L, 4L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -15L), .drop = TRUE))
同时使用 ggforce 的 geom_link 和 ggnewscale,可以逐个元素创建一个循环,为每个元素创建一个新的色阶:
library(ggforce)
library(ggnewscale)
## Index with colors
color_list <- c('ControlD1' = "limegreen",
'HSP90iD1' = "firebrick1",
'PRRTD1' = "deepskyblue",
'CombinationD1' = "orchid1",
'HSP90iD3' = "firebrick4",
'PRRTD3' = "deepskyblue3",
'CombinationD3' = "purple3")
## old-ish code
p_left <- ggplot(tb_deg) +
geom_segment(aes(y=comparison, x=var1, xend=var2), size=2, color="slategrey", show.legend = F) +
geom_point(aes(y=comparison, x=var1, fill=var1), shape=21, size=5, show.legend = F) +
geom_point(aes(y=comparison, x=var2, fill=var2), shape=21, size=5, show.legend = F) +
scale_fill_manual(name="Group",
values=color_list ) +
scale_x_discrete(limits=rev(levels(md$group)) ) +
theme_minimal() +
labs(x=NULL, y=NULL, title = NULL) +
theme(legend.position = "none",
aspect.ratio = 2,
axis.text.x = element_text(angle=90, hjust=1),
axis.text.y = element_blank(),
panel.grid.major.y = element_blank())
p_left
### Loop through each element of the Y-axis, creating a geom_link + a new color scale
p2<-p_left
for (comp in unique(tb_deg$comparison)) {
v1 <- unique(tb_deg[tb_deg$comparison==comp, c("comparison","var1","var2")])$var1
v2 <- unique(tb_deg[tb_deg$comparison==comp, c("comparison","var1","var2")])$var2
p2 <- p2 +
new_scale_color() +
geom_link(data=unique(tb_deg[tb_deg$comparison==comp, c("comparison","var1","var2")]),
aes(y=comparison, yend=comparison, x=var1, xend=var2, color=stat(index)), size=2) +
scale_color_gradient(high=color_list[v2],low=color_list[v1])
}
p2