【问题标题】:ggplot2 - Equal x-axis distances across faceted plotsggplot2 - 多面图上相等的 x 轴距离
【发布时间】:2020-10-22 12:46:19
【问题描述】:

假设三个数据集(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")

进一步假设我们希望可视化列 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


【解决方案1】:

也许这种方法可以帮助你。您可以将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)

输出:

【讨论】:

  • 谢谢,但这不是我想要的。我用期望结果的图像扩展了我的问题。
  • @MichaelG 我现在就去看看!
猜你喜欢
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-18
相关资源
最近更新 更多