Home > Net >  Change Plot Size of ggboxplot
Change Plot Size of ggboxplot

Time:12-17

I created this ggboxplot (from ggpubr) with the following code in R. I want to reduce the width of the plot, i.e. get the boxes closer together. But as far as I can tell there is no way to directly manipulate plot width. Can someone point me in the right direction?

ggboxplot(
  df_outcome.scaled, 
  x="Schedule", 
  y="Cort",
  notch = TRUE,
  width=0.2
)

enter image description here

CodePudding user response:

You can set the width to 0.75 and increase the aspect ratio:

library(ggpubr)

ggboxplot(
  df_outcome.scaled, 
  x = "Schedule", 
  y = "Cort",
  notch = TRUE,
  width = 0.5
)   theme(aspect.ratio = 1.5)

enter image description here


Data used

set.seed(3)

df_outcome.scaled <- data.frame(Schedule = rep(c('Acute', 'Chronic'), 20),
                                Cort = rnorm(40, rep(c(5, 7.5), 20), 2))

CodePudding user response:

Allan's answer is already good, but here is another quick and hacky way of changing the width using scale_x_discrete using the iris data in R. You just set the numeric limits to where you want the x and y axes to expand (positive values widen/lengthen the plot more, whereas negative values do the opposite).

#### Load Libraries ####
library(tidyverse)
library(ggpubr)

#### Use Scale X ####
ggboxplot(iris,
          x="Species",
          y="Petal.Width") 
  scale_x_discrete(expand = c(-1,-1))

enter image description here

Another way is to manually change it's width and height in the plot window if you are exporting the plot elsewhere.

enter image description here

  • Related