2
votes

I can't change the width of ggplot2 boxplot.

I know it's probably the thousandth someone make a question similar to this but after spending my last 2 hours trying to find a solution (which i rarely do) i'm all out.

Besides i guess someone knowledgeable about ggplot2 can answer in 30 seconds.

Example code of similar problem

# Code from official ggplot2 help page
# https://ggplot2.tidyverse.org/reference/geom_boxplot.html
y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(x)) +
  geom_boxplot(
   aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
   stat = "identity"
 )

I tried to changed the width so it is not the whole graphic with the boxplot, to no avail. i've tried putting width = 0.5/width = 0.1 in ggplot, geom_boxplot and in aes and it changes nothing.

Can someone help? Thanks

EDIT: Thanks for the help guys. For future reference the code i used was:

y <- rnorm(100)
df <- data.frame(
  x = 1,
  y0 = min(y),
  y25 = quantile(y, 0.25),
  y50 = median(y),
  y75 = quantile(y, 0.75),
  y100 = max(y)
)
ggplot(df, aes(as.factor(x))) +
  geom_boxplot(
   aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
   width = 0.1,
   stat = "identity"
 )
2
It's easier to see what's up if the thing you've struggled with (changing the width) is included in your example codecamille
Use ggplot(df, aes(as.factor(x))) + ... and then width = .1 (or whatever) in geom_boxplot()markus
Use a factor for x instead of a number, or increase your x-axis with (e.g. with xlim).Axeman
Thanks Markus, that did it. R and factor always seem to get me :-/ Code above with smaller width y <- rnorm(100) df <- data.frame( x = 1, y0 = min(y), y25 = quantile(y, 0.25), y50 = median(y), y75 = quantile(y, 0.75), y100 = max(y) ) ggplot(df, aes(as.factor(x))) + geom_boxplot( aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100), width = 0.1, stat = "identity" )LuisSilva
Yep, x-axis limits seem to be the problem. I've set fixed limits and the width arguments seems to work as intended.teunbrand

2 Answers

4
votes

The problem with the example that you have given seems to be that the X values are numeric. I am not sure that a boxplot is the best way to go if you have x and y values that are both numeric, but see John Bell's answer if this is, in fact, what you want.

But, in the case that your X values are actually factors, this answer should work. If you create a data frame with factors (groups) then the width functionality returns.

data<-data.frame("Group"=as.factor(rep(c(1,2),16)),"Y"=rnorm(32))

ggplot(aes(x=Group,y=Y),data=data)+
  geom_boxplot(width=.15)

enter image description here

ggplot(aes(x=Group,y=Y),data=data)+
  geom_boxplot(width=1)

enter image description here

Also might be worth checking out this USGS page for tips on ggplot2 boxplots: https://owi.usgs.gov/blog/boxplots/

1
votes

Adding xlim(0,2) widens the x-axis and thus narrows the boxplot:

ggplot(df, aes(x)) +
  geom_boxplot(
   aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100),
   stat = "identity"
 ) +
 xlim(0,2)