Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Quantile values from geom_boxplot()

Tags:

r

ggplot2

boxplot

I'm using ggplot2 function geom_boxplot() to create boxplots of my data.

Similarly to this question, I'd like to get the quantile values used for the creation of the boxplot, e.g., in a matrix. Is there a way to print out the values used within geom_boxplot()?

like image 302
atreju Avatar asked Jan 24 '16 15:01

atreju


People also ask

What variables does Stat_boxplot () Compute?

stat_boxplot() provides the following variables, some of which depend on the orientation: width. width of boxplot. ymin or xmin.

What do whiskers represent in a box plot Ggplot?

The Whiskers What is this? These whisker lines show the location of the minimum value on one side, and the maximum value on the other. Typically, these minimum and maximum values are calculated according to a formula. Commonly, the minimum is calculated as Q1 – 1.5*IQR and the maximum is calculated as Q3 + 1.5*IQR.

How do you show a mean in a Boxplot in R?

R. Output: In order to show mean values in boxplot using ggplot2, we use the stat_summary() function to compute new summary statistics and add them to the plot. We use stat_summary() function with ggplot() function.

How to create a boxplot ggplot?

In ggplot2, geom_boxplot() is used to create a boxplot. Let us first create a regular boxplot, for that we first have to import all the required libraries and dataset in use. Then simply put all the attributes to plot by in ggplot() function along with geom_boxplot.


1 Answers

Probably the easiest way is to use an outside of ggplot aggregate, but here's how to do it with ggplot, using the function ggplot_build on a created plot:

library(ggplot2)
p <- ggplot(mtcars, aes(x=factor(gear), y=mpg)) + geom_boxplot() 
ggplot_build(p)$data

[[1]]
  ymin lower middle  upper ymax outliers notchupper notchlower x PANEL group weight ymin_final ymax_final
1 10.4  14.5   15.5 18.400 21.5            17.09102   13.90898 1     1     1      1       10.4       21.5
2 17.8  21.0   22.8 28.075 33.9            26.02695   19.57305 2     1     2      1       17.8       33.9
3 15.0  15.8   19.7 26.000 30.4            26.90729   12.49271 3     1     3      1       15.0       30.4
   xmin  xmax
1 0.625 1.375
2 1.625 2.375
3 2.625 3.375
like image 128
jeremycg Avatar answered Oct 24 '22 16:10

jeremycg