How to set up notches in ggplot boxplot

I got a question on how to change / adjust the upper and lower limit of the cutout on a box generated by ggplot2. I looked at the stat_boxplot function and found that ggplot calculates the notch limits using the median equation +/- 1.58 * iqr / sqrt (n). However, instead of this equation, I wanted to modify it with my own set of upper and lower notch limits.

My data has 4 factors, and for each factor I calculated the median and bootstraped to get a 95% confidence interval of that median. So in the end, I would like to change each boxplot to have its own unique upper and lower limit.

I'm not sure if this is even possible in ggplot, and was wondering if people have an idea on how to do this?

Thanks again!

0


source to share


1 answer


I guess I figured it out somehow in the end, but already 1 vote ?!

Anyway, I figured out one way to set up notches on a plot using ggplot using the ggplot_build function.

After constructing a rectangle labeled:

p<-ggplot(combined,aes(x=foo,y=bar)) + geom_boxplot(notch=TRUE)

      

not really sure what exactly is going on with ggplot_build, but it looks like it will convert the plot to a data-frame ish structure, so you can manipulate it if you like.

gg<-ggplot_build(p)

      

after



gg$data[[1]]$notchlower
gg$data[[1]]$notchupper

      

contains the cutout limits for your plot, and you can basically change it with something like:

gg$data[[1]]$notchlower<-50
gg$data[[1]]$notchupper<-100

      

And if you had mulitple boxplots and wanted to individually change each boxplot:

gg$data[[1]]$notchlower[1]<-50
gg$data[[1]]$notchlower[2]<-50
....
gg$data[[1]]$notchlower[n]<-50

gg$data[[1]]$notchupper[1]<-100
gg$data[[1]]$notchupper[2]<-100
....
gg$data[[1]]$notchupper[n]<-100

      

In any case, we hope this is the correct method and it will help other people.

+2


source







All Articles