3
votes

I have created some boxplots using ggplot2 geom_boxplot. I see that it computes a set of variables on my dataframe; width, ymin, lower, notchlower, middle, notchupper, upper, and ymax. It seems like there should be a way to extract these computed values so that I can look at them. I have tried summary(boxplot) and str(boxplot). These give me a lot of information about how my plot was created but they do not list the computed variables.

Is there a way to quickly display these variables?

2

2 Answers

-2
votes

Edit of my previous answer

This question isn't well-posed, because (a) it doesn't give a reproducible example and (b) it admits the possibility of using the result of graphics::boxplot() to get an answer.

What the OP is missing is that ggplot2::geom_boxplot returns a ggplot2 graphics object, not the summary statistics for the data.

For the base graphics solution, see help(boxplot) -- the boxplot function returns what you want, rather than trying to get this from geom_boxplot().

> bp <- boxplot(mpg~cyl, data=mtcars) 
> 
> str(bp)
List of 6
 $ stats: num [1:5, 1:3] 21.4 22.8 26 30.4 33.9 ...
 $ n    : num [1:3] 11 7 14
 $ conf : num [1:2, 1:3] 22.4 29.6 18.3 21.1 14.3 ...
 $ out  : num [1:2] 10.4 10.4
 $ group: num [1:2] 3 3
 $ names: chr [1:3] "4" "6" "8"

The stats component contains the 5-number summary for each group.

> bp$stats
     [,1]  [,2] [,3]
[1,] 21.4 17.80 13.3
[2,] 22.8 18.65 14.3
[3,] 26.0 19.70 15.2
[4,] 30.4 21.00 16.4
[5,] 33.9 21.40 19.2
>

In ggplot2 the summaries are calculated by stat_boxplot. However, I know of no way to extract these from the result

2
votes

You can use ggplot_build as suggested here: https://stackoverflow.com/a/25381297/3638464

library(ggplot)

gg_bp <- 
  ggplot(mpg, aes(class, hwy)) +
  geom_boxplot()

gg_bp preview:

boxplot

gg_bp_build <- 
  ggplot_build(gg_bp)

gg_bp_build$data
#> [[1]]
#>   ymin lower middle upper ymax                       outliers notchupper
#> 1   23  24.0   25.0  26.0   26                                  26.41319
#> 2   23  26.0   27.0  29.0   33                 35, 37, 35, 44   27.69140
#> 3   23  26.0   27.0  29.0   32                                  27.74026
#> 4   21  22.0   23.0  24.0   24                             17   23.95278
#> 5   15  16.0   17.0  18.0   20                 12, 12, 12, 22   17.55009
#> 6   20  24.5   26.0  30.5   36                         44, 41   27.60241
#> 7   14  17.0   17.5  19.0   22 12, 12, 25, 24, 27, 25, 26, 23   17.90132
#>   notchlower x PANEL group ymin_final ymax_final  xmin  xmax xid newx new_width
#> 1   23.58681 1     1     1         23         26 0.625 1.375   1    1      0.75
#> 2   26.30860 2     1     2         23         44 1.625 2.375   2    2      0.75
#> 3   26.25974 3     1     3         23         32 2.625 3.375   3    3      0.75
#> 4   22.04722 4     1     4         17         24 3.625 4.375   4    4      0.75
#> 5   16.44991 5     1     5         12         22 4.625 5.375   5    5      0.75
#> 6   24.39759 6     1     6         20         44 5.625 6.375   6    6      0.75
#> 7   17.09868 7     1     7         12         27 6.625 7.375   7    7      0.75
#>   weight colour  fill size alpha shape linetype
#> 1      1 grey20 white  0.5    NA    19    solid
#> 2      1 grey20 white  0.5    NA    19    solid
#> 3      1 grey20 white  0.5    NA    19    solid
#> 4      1 grey20 white  0.5    NA    19    solid
#> 5      1 grey20 white  0.5    NA    19    solid
#> 6      1 grey20 white  0.5    NA    19    solid
#> 7      1 grey20 white  0.5    NA    19    solid