繁体   English   中英

ggplot2 - 跨多面图的相等 x 轴距离

[英]ggplot2 - Equal x-axis distances across faceted plots

假设三个数据集( ABC ),每个数据集代表一个具有四列的 data.frame。 每个数据集中的数据都是成对的,所有行的前半部分表示状态“ bf ”,所有行的后半部分表示状态“ after ”。 每个数据集中的行数不同,集合C包含的行数等于或多于AB行数。

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

进一步假设我们希望可视化不同类别之间列的可变性。

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 中的相同?

这是我想要实现的(无需手动编辑单个绘图宽度):

预期结果的说明

也许这种方法可以帮助你。 您可以将scale_x_discrete(limits=unique(C$marker))AB图中,以便在所有图中保持相同的水平。 这里是代码。 您也可以为 x 轴中的级别创建一个独立的向量,并将其直接用作vec=c("a", "b", "c", "d", "e", "f")然后scale_x_discrete(limits=vec) 关键是在所有需要的图中修复这个元素:

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)) + 
  scale_x_discrete(limits=unique(C$marker))+
  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)) + 
  scale_x_discrete(limits=unique(C$marker))+
  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)) + 
  scale_x_discrete(limits=unique(C$marker))+
  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)

输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM