rggplot2ggsave

ggsave doesn't export well ggarrange plot


I want to export a ggarrange object as a .jpg or .tiff with dpi=300, using "mm" units, with a width of 107 mm.

Unfortunately, when using ggsave, I struggle to the desired output, every sizes are different when using "mm" units.

Output desired (an output where dimensions are realistic)

enter image description here

Current output (sizes are not correct)

enter image description here

Maybe the text size is too large for the output.

Please find a reproducible example

library(ggplot2)
library(ggpubr)

##### Initiating data
tmpDf <- data.frame(values1=1:12,
                    values2=rev(101:112),
                    group=rep(c("I", "II", "III"), 4))

##### Initiating plot
tmpPlot <- 
  ggplot(data=tmpDf, aes(x=values1, y=values2, color=group)) + 
  geom_point() + 
  ggtitle("Title") + 
  theme_minimal() + 
  theme(legend.position="bottom",
        axis.title.y=element_text(angle=0, vjust=0.5),
        plot.caption=element_text(hjust=0),
                     plot.title.position="plot", 
                     plot.caption.position="plot")

##### Initiating a group of plots
tmpPlotsGrouped <- 
  ggarrange(
    tmpPlot,
    tmpPlot,
    tmpPlot,
    nrow=1, ncol=3, widths=c(1, 1, 1))

##### Export as a jpg
ggsave(file="myPath/plotName.jpg", plot=tmpPlotsGrouped,
       units="mm", width=107, height=50, dpi=300)

Solution

  • To match what you see on screen with the plot panel or zoom, set the dpi to the ppi of your monitor, and adjust the size accordingly.

    I want an image that is 107x50mm at 300 dpi, which is

    > c(107,50)/25.5*300
    [1] 1258.8235  588.2353
    

    in pixels

    to get that to match what I see in my plot zoom (with window scaled to desired size), I need to use

    ggsave(file="myPath/plotName.jpg", plot=tmpPlotsGrouped, scale=1,
           units="px", width=1259, height=588, dpi=105)