ggplot2-平面图上相等的x轴距离

问题描述

假设三个数据集(void DocumentHandler::setBackgroundColor(const QColor &color) { const QString &bgcolor("bgcolor=\""); QString html(m_doc->toHtml()); int n = html.indexOf(bgcolor,html.indexOf("<body")); int k = n + bgcolor.length(); m_doc->setDefaultStyleSheet("body { background-color: '" + color.name() + "' }"); if (n >= 0) html.replace(n + bgcolor.length(),html.mid(k,html.indexOf("\"",n + bgcolor.length()) - k).length(),color.name()); m_doc->setHtml(html); m_backgroundColor = color.name(); // QColor variable emit backgroundColorChanged(); } AB),每个数据集代表一个具有四列的data.frame。每个数据集中的数据都是成对的,所有行的前半部分表示状态“ bf ”,而所有行的后半部分表示状态“ 之后”。每个数据集中的行数是不同的,集合C包含等于或大于CA的行。

B

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

A = structure(list(marker = c("a","b","c","d","a","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.864","1"),metric = c("rc","rc","ci","ci"),treatment = c("b4","b4","after","after")),row.names = c(NA,-16L),class = "data.frame")
B = structure(list(marker = c("a","b"),value = c("1","0.966","0.962","0.965"),-8L),class = "data.frame")
C = structure(list(marker = c("a","e","f","f"),value = c("0.944","0.934","0.947","0.922","0.909","0.958","0.857","0.9","0.914","0.914"),-24L),class = "data.frame")

如何确保图A和B中两个标记间的x轴距离与图C中的相同?

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

Illustration of desired outcome

解决方法

也许这种方法可以为您提供帮助。您可以将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),axis.title.x=element_blank()) +
  ylab("")

Plot_C = ggplot(data=data.frame(C),legend.position="bottom") + 
  xlab("Marker") +
  ylab("Value")

ggarrange(Plot_A,Plot_B,Plot_C,nrow=3,ncol=1)

输出:

enter image description here