0

I'm making boxplots directly from summary stats, and the width parameter seems to not be taken into account for those. How can I change the width of the boxes in this case? I get a "unknown parameter ignored" if I specify width as I would normally do, and I can't quite figure out how (or if) I can use varwidth using the N column.

df <- structure(list(Area = c("a", "b", "c"), Q5 = c(8.545403328, 23.436503552, 
28.134954848), Q10 = c(10.197717376, 23.558146304, 31.434514496
), Q25 = c(15.15465952, 23.92307456, 41.33319344), Q50 = c(63.55833792, 
24.53128832, 58.74331232), Q75 = c(88.69784, 31.6778, 71.69319696
), Q90 = c(102.504292352, 35.965707008, 73.563454272), Q95 = c(107.106443136, 
37.395009344, 74.186873376), N = c(5L, 3L, 4L)), .Names = c("Area", 
"Q5", "Q10", "Q25", "Q50", "Q75", "Q90", "Q95", "N"), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

plot:

library(ggplot2)

ggplot(df) + 
geom_boxplot(aes(x = Area, lower = Q25, upper = Q75, middle = Q50, 
    ymin = Q10, ymax = Q90), stat = "identity", size = 0.1) 
user2602640
  • 640
  • 6
  • 21
  • 'width' is a computed variable in `geom_boxplot` so you can't set it yourself. you can set `varwidth = TRUE` to set the width based on the # of observations. If you need a similar summary geom that you can alter the width, perhaps look at `geom_crossbar`, and add `geom_path()` to simulate – Nate Feb 15 '18 at 13:35
  • https://github.com/tidyverse/ggplot2/issues/1904 – MLavoie Feb 15 '18 at 13:40
  • @Nate I tried setting varwidth but it doesn't change the output, even if I changed `df$N` to be wildly different (`c(1, 10, 20)`) – user2602640 Feb 15 '18 at 13:42
  • 1
    @MLavoie - huh, setting `width = N` inside the `aes()` call seemed to work, although it gives me a warning. If you want to write up the answer, I'll accept it. – user2602640 Feb 15 '18 at 13:44
  • Adding `width = N` in `aes` seems to do something. Add that with `coord_trans()` and it's closer but still not the answer. – tyluRp Feb 15 '18 at 13:45
  • but it really screw up your x axis – MLavoie Feb 15 '18 at 13:48
  • Yes it does, not sure how to fix that – tyluRp Feb 15 '18 at 13:49
  • Ah, I see what you mean. I just used a tiny N :-) – user2602640 Feb 15 '18 at 13:51
  • 1
    `width = N / 5` or some other number seems to make it look nice without screwing up the x axis. – tyluRp Feb 15 '18 at 13:55

0 Answers0