Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing grid between transparent ggplot plots arranged by ggarrange

Tags:

r

ggplot2

ggpubr

I'm trying to produce a figure containing multiple ggplot2 plots arranged by ggarrange from the package ggpubr and annotated on the left side. The ggplot2 plots are transparent or have a transparent background.

The issue arose lately (i.e. I did not confront it this year earlier) where a light grey line appears between the plots and the annotation text on the left side as shown in the image below. And it is more prominent when the figure is inserted in LaTeX.

I tried to play with the individual plots, but there is no plot.border only panel.border, thus, all my trials were not successful!

My maschine information:

R 3.6.3 on Ubuntu 20.04.2 LTS

ggplot2 3.3.5

ggpubr 0.4.0

11111111111

The code I used to produce it (and was inspired by this):

library(ggplot2)
library(ggpubr)

# Box plot (bp)
bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len",
                                 color = "dose", palette = "jco")+
    
    theme_bw(base_size = 8)+
    
    theme(legend.position = "bottom",
                legend.title = element_blank(),
                legend.text = element_text(size = 10),
                legend.background = element_blank(),
                legend.box.background = element_rect(colour = "black",fill=NA, 
                                                                                         size=0.1, linetype="solid"),
                legend.key.width = unit(5, "mm"),
                legend.spacing =  unit(1, 'mm'),
                legend.key.size  = unit(0.5, "mm"),
                legend.margin = margin(3,1,3,1, unit = "mm"))+
    
    theme(panel.grid.major =element_line(colour = "black",linetype = "dotted",size = 0.05), 
                panel.grid.minor = element_blank(),
                plot.margin = margin( 2.25,4.5,2.25,1, unit = "mm"),
                plot.title = element_text(margin = margin(1,0,1,0, unit = "mm"),size = 10),
                axis.line = element_blank(),
                axis.text = element_text(colour = "black"), axis.title= element_blank(),
                panel.border = element_rect(colour = "black", fill = NA, size=0.75),
                panel.background = element_rect(color = NA),
                rect = element_rect(fill = "transparent") )+ # all rectangles
    
    labs(title = expression("(a)"))+
    theme(axis.text = element_text(colour = "black"))


# Dot plot (dp)
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len",
                                color = "dose", palette = "jco", binwidth = 1)+
    
    theme_bw(base_size = 8)+
    
    theme(legend.position = "bottom",
                legend.title = element_blank(),
                legend.text = element_text(size = 10),
                legend.background = element_blank(),
                legend.box.background = element_rect(colour = "black",fill=NA, 
                                                                                         size=0.1, linetype="solid"),
                legend.key.width = unit(5, "mm"),
                legend.spacing =  unit(1, 'mm'),
                legend.key.size  = unit(0.5, "mm"),
                legend.margin = margin(3,1,3,1, unit = "mm"))+
    
    theme(panel.grid.major =element_line(colour = "black",linetype = "dotted",size = 0.05), 
                panel.grid.minor = element_blank(),
                plot.margin = margin( 2.25,4.5,2.25,1, unit = "mm"),
                plot.title = element_text(margin = margin(1,0,1,0, unit = "mm"),size = 10),
                axis.line = element_blank(),
                axis.text = element_text(colour = "black"), axis.title= element_blank(),
                panel.border = element_rect(colour = "black", fill = NA, size=0.75),
                panel.background = element_rect(color = NA),
                rect = element_rect(fill = "transparent") )+ # all rectangles
    
    labs(title = expression("(b)"))+
    theme(axis.text = element_text(colour = "black"))

bxp1<-bxp+labs(title = expression("(c)"))

dp1<-dp+labs(title = expression("(d)"))

figure <- ggarrange(bxp,bxp1,dp,dp1,
ncol = 2, nrow = 2,align = c("hv"))

figure <-annotate_figure(figure,left = text_grob("left side ",
color = "black",size = 10,  rot = 90))


ggsave(plot = figure, 
             filename = paste0("question.png"),
             height = 180, width =180, units = "mm", dpi = 300, device = "png",limitsize = FALSE)

Edit:

I can forget about the transparency by removeing rect = element_rect(fill = "transparent") ,however, the light grey gridline appears between the arragned plots and the antonatioed text of the left side.

The question can be: How to change the background of textgrob() to white instaed of transparent?

enter image description here

like image 938
ahmathelte Avatar asked Sep 06 '25 03:09

ahmathelte


1 Answers

The issue with transparent arranged plots still exists. However, a progressive solution can be:

  1. Producing ggplot2 plots with a white background by removing rect = element_rect(fill = "transparent") .
  2. Using another approach to annotate and arrange plots:
library(gridExtra)
library(grid)
library(gridtext)

yleft <- textGrob("left side", 
            rot = 90, gp = gpar(fontsize = 10, fill= "white",color="black"))

figure <- grid.arrange(bxp,bxp1,
          dp,dp1,
          ncol = 2, nrow = 2,
          left=yleft)

Update (March 2022): using the approach in the question and changing the background bg to white.

ggsave(plot = figure,
       filename = paste0("question.png"),
       height = 180, width =180, units = "mm", dpi = 300,
       device = "png",limitsize = FALSE,bg="white")

enter image description here

like image 90
ahmathelte Avatar answered Sep 08 '25 00:09

ahmathelte