Home > Software design >  Need help getting summary statistics in a filtered data frame by dplyr
Need help getting summary statistics in a filtered data frame by dplyr

Time:02-17

I`ve reached the boxplot that I need with the code below, but I cannot extract the information that I need in each category ( Min. - 1st Qu. - Median - Mean - 3rd Qu. - Max.). How can I do it?

gapminder %>%
  filter(ciclo_real_reg >= 45) %>%   ##Removing Outliers
  filter(ciclo_real_reg <= 120) %>%  ##Removing Outliers
  ggplot(aes(x=DxP, y = ciclo_real_reg)) geom_boxplot(aes(color = DxP))

CodePudding user response:

We can extract the data that made the plot with ggplot_build. I use mtcars in this answer to create a reproducible example because I do not want to install gapminder.

library(dplyr)
library(ggplot2)
mtcars %>% 
  ggplot(aes(mpg)) 
  geom_boxplot() ->p
  ggplot2::ggplot_build(p)$data[[1]]

Result

xmin xlower xmiddle xupper xmax outliers notchupper
1 10.4 15.425    19.2   22.8 32.4     33.9   21.25989
  notchlower y flipped_aes PANEL group xmin_final xmax_final
1   17.14011 0        TRUE     1    -1       10.4       33.9
    ymin  ymax xid newx new_width weight colour  fill size
1 -0.375 0.375   1    0      0.75      1 grey20 white  0.5
  alpha shape linetype
1    NA    19    solid

Now, what we are interested in is xlower, xmiddle, xupper

  • Related