使用Cowplot套件的多点检测的居中X轴标签 [英] Centered X-axis label for muliplot using cowplot package
问题描述
我有一个多图图形,它由2x2配置中的4个图组成.我使用"cowplot"包安排了地块,并使用下面的代码安排了plot_grid函数
I have a multiplot figure consisting of 4 plots in a 2x2 configuration. I arranged the plots using the "cowplot" package and the plot_grid function using the code below
plot_grid(p1, p2, p3, p4, align='vh', vjust=1, scale = 1)
其中p1-p4是我的4个情节.生成的图形具有一个x轴标签,该标签与多图中的每一列关联:
where p1-p4 are my 4 plots. The resulting figure has an x-axis label associated with each column in the multiplot:
有人知道我该如何用Cowplot或另一种方式对以多图底部为中心的单个x轴标签进行编码?
Does anyone know how I can code a single x-axis label centered at the bottom of the multiplot, either with cowplot or another way?
推荐答案
另一种选择是使用textGrob
为常见的x和y标签添加注释.
Another option is to use textGrob
to add annotations for common x and y labels.
在此示例中,如果要删除单个轴标签,只需在绘图调用中包括theme(axis.title = element_blank())
即可. (或者,对于y轴,请使用theme(axis.title.y=element_blank())
).
In this example, if you wish to remove the individual axis labels, just include theme(axis.title = element_blank())
in the plot calls. (or, for just y-axis, use theme(axis.title.y=element_blank())
).
library(ggplot2)
library(cowplot)
library(grid)
library(gridExtra)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
#make 4 plots
p1<-ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()
p2<-ggplot(ToothGrowth, aes(x=dose, y=supp)) +
geom_boxplot()
p3<-ggplot(ToothGrowth, aes(x=supp, y=len)) +
geom_boxplot()
p4<-ggplot(ToothGrowth, aes(x=supp, y=dose)) +
geom_boxplot()
#combine using cowplot
plot<-plot_grid(p1, p2, p3, p4, align='vh', vjust=1, scale = 1)
#create common x and y labels
y.grob <- textGrob("Common Y",
gp=gpar(fontface="bold", col="blue", fontsize=15), rot=90)
x.grob <- textGrob("Common X",
gp=gpar(fontface="bold", col="blue", fontsize=15))
#add to plot
grid.arrange(arrangeGrob(plot, left = y.grob, bottom = x.grob))
这篇关于使用Cowplot套件的多点检测的居中X轴标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!