You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ggarrange组合多图后,ggsave保存出现背景黑、轴名丢失问题

问题描述

使用ggplot2结合ggarrange函数组合多张图表后,调用ggsave保存时,图片背景显示为黑色且左侧轴名称不可见,但通过RStudio导出按钮导出组合图则无此问题。相关代码如下:

library(ggplot2)

col<-c("#F8766D", "#619CFF","#00BA38")

fine<-(rep(c("Fischer-like","Medium","Lewis-like"),each=2))
sex<-rep(c("Males","Females"), times=3)

prop<-c(0.41*100,0.59*100,0.65*100,0.35*100,0.45*100,0.55*100)
etapa_a<-data.frame(fine,sex,prop)

a<-ggplot(data=etapa_a, aes(x=sex,y=prop))+
  geom_col(aes(fill=fine),width = 0.5,color = "black") + theme_classic()+
  scale_fill_manual(values= c("#F8766D", "#619CFF","#00BA38"),
                    limits = c("Fischer-like","Medium",  "Lewis-like"))+
  theme(legend.title = element_blank(),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_blank(),
        axis.text.x=element_text(colour="black",size= 15),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  labs(x=" Sex",y="")+
  theme(text = element_text(size = 20))+
  geom_text(aes(y = prop,x=sex, label = prop, group =fine), 
            color = "black", 
            position = position_stack(vjust = 0.5),size=8)

prop<-c(0.13*100,0.86*100,0.78*100,0.22*100,0.54*100,0.45*100)
etapa_b<-data.frame(fine,sex,prop)

b<-ggplot(data=etapa_b, aes(x=sex,y=prop))+
  geom_col(aes(fill=fine),width = 0.5,color = "black") + theme_classic()+
  scale_fill_manual(values= c("#F8766D", "#619CFF","#00BA38"),
                    limits = c("Fischer-like","Medium",  "Lewis-like"))+
  theme(legend.title = element_blank(),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_blank(),
        axis.text.x=element_text(colour="black",size= 15),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  labs(x=" Sex",y="")+
  theme(text = element_text(size = 20))+
  geom_text(aes(y = prop,x=sex, label = prop, group =fine), 
            color = "black", 
            position = position_stack(vjust = 0.5),size=8)

prop<-c(0.26*100,0.73*100,0.48*100,0.51*100,0.78*100,0.22*100)
etapa_c<-data.frame(fine,sex,prop)

c<-ggplot(data=etapa_c, aes(x=sex,y=prop))+
  geom_col(aes(fill=fine),width = 0.5,color = "black") + theme_classic()+
  scale_fill_manual(values= c("#F8766D", "#619CFF","#00BA38"),
                    limits = c("Fischer-like","Medium",  "Lewis-like"))+
  theme(legend.title = element_blank(),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_blank(),
        axis.text.x=element_text(colour="black",size= 15),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  labs(x=" Sex",y="")+
  theme(text = element_text(size = 20))+
  geom_text(aes(y = prop,x=sex, label = prop, group =fine), 
            color = "black", 
            position = position_stack(vjust = 0.5),size=8)

prop<-c(0.16*100,0.84*100,0.89*100,0.11*100,0.51*100,0.48*100)
etapa_d<-data.frame(fine,sex,prop)

d<-ggplot(data=etapa_d, aes(x=sex,y=prop))+
  geom_col(aes(fill=fine),width = 0.5,color = "black") + theme_classic()+
  scale_fill_manual(values= c("#F8766D", "#619CFF","#00BA38"),
                    limits = c("Fischer-like","Medium",  "Lewis-like"))+
  theme(legend.title = element_blank(),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_blank(),
        axis.text.x=element_text(colour="black",size= 15),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+
  labs(x=" Sex",y="")+
  theme(text = element_text(size = 20))+
  geom_text(aes(y = prop,x=sex, label = prop, group =fine), 
            color = "black", 
            position = position_stack(vjust = 0.5),size=8)

library(ggpubr)
library(grid)

prop_sex <- ggarrange(a + rremove("ylab") + rremove("xlab"),b + rremove("ylab") 
                  + rremove("xlab"), c + rremove("ylab") + rremove("xlab"), 
                  d + rremove("ylab") + rremove("xlab"), # remove axis labels from plots
                  labels = c("A", "B", "C","D"),label.x = -0.01,
                  ncol = 1, nrow = 4,
                  common.legend = TRUE, legend = "bottom",
                  align = "hv", 
                  font.label = list(size = 9, color = "black", face = "bold",
                                    family = NULL, position = "top"))

prop_sex<-annotate_figure(prop_sex, left = textGrob("Percentage of individuals", 
                                                         rot = 90, vjust = 1, 
                                            gp = gpar(cex = 1.3),(vjust=-0.02)),
                      bottom = textGrob("", gp = gpar(cex = 1.3)))

prop_sex

ggsave(filename = "test1.jpg", plot = last_plot(),
       path = "C:/Users/Usuario/Dropbox/Inmunoneuro/uno_a_uno/plots_multivar",
       width=15,height =30,units="cm",
       dpi = 300)
原因分析
  • 绘图设备背景兼容性问题:ggsave默认使用的绘图设备对grid包的textGrob这类非ggplot原生元素支持不佳,保存为JPG(不支持透明格式)时,透明背景会被渲染为黑色。
  • textGrob参数错误:annotate_figure中重复设置了vjust参数(vjust = 1和(vjust=-0.02)),导致左侧轴标签位置异常,被挤出画布外,造成不可见。
解决方案

1. 修复textGrob参数错误

删除重复的vjust参数,调整后的代码:

prop_sex<-annotate_figure(prop_sex, 
                          left = textGrob("Percentage of individuals", 
                                          rot = 90, vjust = 1, 
                                          gp = gpar(cex = 1.3)),
                          bottom = textGrob("", gp = gpar(cex = 1.3)))

2. 给ggsave指定白色背景

在ggsave中添加bg="white"参数,强制设置不透明背景:

ggsave(filename = "test1.jpg", plot = last_plot(),
       path = "C:/Users/Usuario/Dropbox/Inmunoneuro/uno_a_uno/plots_multivar",
       width=15,height =30,units="cm",
       dpi = 300,
       bg="white")

3. 推荐:直接指定保存的组合图对象

避免依赖last_plot()的绘图上下文,直接传入prop_sex对象更可靠:

ggsave(filename = "test1.jpg", plot = prop_sex,
       path = "C:/Users/Usuario/Dropbox/Inmunoneuro/uno_a_uno/plots_multivar",
       width=15,height =30,units="cm",
       dpi = 300,
       bg="white")

内容的提问来源于stack exchange,提问作者MyName

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 04:14:52