cowplot::plot_grid with facet_wrap - y 轴标题与轴标签重叠

问题描述

我只使用 R 和 gglot 很短的时间,所以我非常感谢您的帮助! 由于我喜欢使用辅助 y 轴来显示带中断的平均值,因此 facet_wrap 变得仅适用于并排显示三个图。所以我使用库“cowplot”来管理它。

3 Plots side by side with cowplot

为了保持灰色标题字段 - 就像在其他图表中一样 - 毕竟我对单个图表应用了 facet_wrap。但是 y 轴标签在 y-ticks 标签上移动。无论是通过边距、边框还是其他方式,我都设法将 y 轴标签推到外面。

这是一个示例代码

set.seed(42)  ## for sake of reproducibility
n <- 6
dat_A <- data.frame(id=1:n,date=seq.Date(as.Date("2020-12-26"),as.Date("2020-12-31"),"day"),group=rep(LETTERS[1:1],n/2),age=sample(18:30,n,replace=TRUE),type=factor(paste("type",1:n)),x=rnorm(n))

dat_B <- data.frame(id=1:n,group=rep(LETTERS[2:2],x=rnorm(n))

p1 <- ggplot(dat_A,aes(x=x,y=age,fill=group))+
  geom_Boxplot()+
  geom_hline(aes(yintercept = age),linetype = "dashed")+
  geom_jitter(alpha = .4)+
  facet_wrap(~group,scales = "free")+
  scale_y_continuous(limits = c(10,30),expand = expansion(mult = c(.05,.05)),sec.axis = sec_axis(~ .,labels = scales::number_format(accuracy = 0.1),breaks = dat_A$age))+
  
  guides(fill=FALSE)+
  theme_bw(base_size = 9)+

labs(
  y = expression(~bold(age)~'in a'),color = NULL)+
theme(
  text = element_text(family="Arial",size=9),plot.margin = unit(c(0,.7,0),"cm"),# c(top,right,bottom,left)
  axis.title.x = element_blank(),legend.position = "none"
  )

p2 <- ggplot()+
  geom_point(aes(x,age),dat_B)+
  facet_wrap(~group,scales = "free")+
  labs(
    y = expression(~bold(age)~'in a'),color = NULL)+
  theme(
    plot.margin = unit(c(0,left)
    axis.line.x = element_blank()
  )

library(cowplot)
plot_grid(p1,p2,p1,align = "h",nrow = 1,ncol = 3,rel_widths = c(22/100,39/100,39/100),labels = c("A","B","C"),label_size = 9,label_fontfamily = "Cambria",## emf seems to overwrite "fontfamily"
          label_fontface = "bold",label_colour = NULL,label_x = -0.03,label_y = 1 #0.05
)

可能对某些人来说很有趣,否则问题可以被删除。对我来说已经解决了。

谢谢 马耳他

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)