【发布时间】:2020-10-22 12:46:19
【问题描述】:
假设三个数据集(A、B 和 C),每个数据集代表一个包含四列的 data.frame。每个数据集中的数据是成对的,所有行的前半部分表示状态“bf”,所有行的后半部分表示状态“after”。每个数据集中的行数不同,集合C 包含的行数等于或多于A 和B 加起来的行数。
A = structure(list(marker = c("a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"), value = c("0.962", "0.923", "0.921", "0.938", "0.949", "0.898", "0.811", "1", "0.967", "0.944", "0.946", "0.96", "0.889", "0.923", "0.864", "1"), metric = c("rc", "rc", "rc", "rc", "ci", "ci", "ci", "ci", "rc", "rc", "rc", "rc", "ci", "ci", "ci", "ci"), treatment = c("b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "after", "after", "after", "after", "after", "after", "after", "after")), row.names = c(NA, -16L), class = "data.frame")
B = structure(list(marker = c("a", "b", "a", "b", "a", "b", "a", "b"), value = c("1", "0.967", "0.966", "0.962", "1", "1", "0.967", "0.965"), metric = c("rc", "rc", "ci", "ci", "rc", "rc", "ci", "ci"), treatment = c("b4", "b4", "b4", "b4", "after", "after", "after", "after")), row.names = c(NA, -8L), class = "data.frame")
C = structure(list(marker = c("a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f", "a", "b", "c", "d", "e", "f"), value = c("0.944", "0.934", "0.947", "0.944", "0.949", "0.922", "0.909", "0.923", "0.958", "0.857", "0.9", "0.914", "0.944", "0.934", "0.947", "0.944", "0.949", "0.922", "0.909", "0.923", "0.958", "0.857", "0.9", "0.914"), metric = c("rc", "rc", "rc", "rc", "rc", "rc", "ci", "ci", "ci", "ci", "ci", "ci", "rc", "rc", "rc", "rc", "rc", "rc", "ci", "ci", "ci", "ci", "ci", "ci"), treatment = c("b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "b4", "after", "after", "after", "after", "after", "after", "after", "after", "after", "after", "after", "after")), row.names = c(NA, -24L), class = "data.frame")
进一步假设我们希望可视化列 value 在不同类之间的可变性。
library(ggplot2)
library(ggpubr)
Plot_A = ggplot(data=data.frame(A), aes(x=marker, y=as.numeric(value), group=treatment, color=metric)) +
facet_grid(metric ~ .) +
geom_line(aes(linetype=treatment)) +
theme(axis.text.x=element_text(angle=45, hjust=1),
legend.position="none",
axis.title.x=element_blank()) +
ylab("")
Plot_B = ggplot(data=data.frame(B), aes(x=marker, y=as.numeric(value), group=treatment, color=metric)) +
facet_grid(metric ~ .) +
geom_line(aes(linetype=treatment)) +
theme(axis.text.x=element_text(angle=45, hjust=1),
legend.position="none",
axis.title.x=element_blank()) +
ylab("")
Plot_C = ggplot(data=data.frame(C), aes(x=marker, y=as.numeric(value), group=treatment, color=metric)) +
facet_grid(metric ~ .) +
geom_line(aes(linetype=treatment)) +
theme(axis.text.x=element_text(angle=45, hjust=1),
legend.position="bottom") +
xlab("Marker") +
ylab("Value")
ggarrange(Plot_A, Plot_B, Plot_C, nrow=3, ncol=1)
如何确保图 A 和图 B 中各个标记之间的 x 轴距离与图 C 中的相同?
这是我想要实现的(无需手动编辑各个绘图宽度):
【问题讨论】:
-
我认为拼凑包在这里可能会有所帮助。
(Plot_A | plot_spacer()) / (Plot_B | plot_spacer() | plot_spacer()) / Plot_C得到了接近你想要的东西,但轴没有完美对齐。所以不是一个解决方案......但在这里发布它,以防万一它有帮助。
标签: r ggplot2 data-visualization facet ggpubr