3

I can't change the width of ggplot2 boxplot.

I know it's probably the thousandth time someone makes a similar question 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"
 )
LuisSilva
  • 234
  • 1
  • 4
  • 13
  • It's easier to see what's up if the thing you've struggled with (changing the width) is included in your example code – camille Apr 23 '19 at 18:04
  • 1
    Use `ggplot(df, aes(as.factor(x))) + ...` and then `width = .1` (or whatever) in `geom_boxplot()` – markus Apr 23 '19 at 18:06
  • 1
    Use a factor for x instead of a number, or increase your x-axis with (e.g. with `xlim`). – Axeman Apr 23 '19 at 18:09
  • 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 Apr 23 '19 at 18:10
  • Yep, x-axis limits seem to be the problem. I've set fixed limits and the width arguments seems to work as intended. – teunbrand Apr 23 '19 at 18:10
  • could u share te code @teunbrand? – LuisSilva Apr 23 '19 at 18:13
  • If you've got a solution, you can post it as an answer to your own question – camille Apr 23 '19 at 18:25
  • Also, here's one (of a few) related post: https://stackoverflow.com/q/52376663/5325862 – camille Apr 23 '19 at 18:31
  • Yes I used the following to widen the boxplot: `ggplot(df, aes(x)) + geom_boxplot( aes(ymin = y0, lower = y25, middle = y50, upper = y75, ymax = y100), stat = "identity", width = 5 ) + scale_x_continuous(limits = c(-5, 5))` – teunbrand Apr 23 '19 at 18:32

2 Answers2

7

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/

Dylan_Gomes
  • 2,066
  • 14
  • 29
  • It's strange. In the documentation page of `geom_boxplot()`, no argument named 'width' is mentioned. Do u hv any idea why? – amsquareb Jan 07 '20 at 06:49
  • It actually is, it is just not in the `usage` description. If you scroll near the end you will find: `Computed variables` subtitle, and just underneath that you will see: `width` `width of boxplot` – Dylan_Gomes Jan 07 '20 at 22:30
  • But it's mentioned as a computed variable, not an argument. – amsquareb Jan 07 '20 at 23:28
  • 1
    hmm yes I see what you are saying. I actually think it is buried under the argument `...` "`Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3.`" Many of the `geom_` functions will take width as an argument, but none of them explicitly say this in the documentation. I think it is implied in the `...` but I am not sure where you can find the full list of what these 'other' arguments are... – Dylan_Gomes Jan 14 '20 at 16:52
1

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)

John Bell
  • 11
  • 5