如何在ggplot boxplot中自定义缺口 [英] How to customize notches in ggplot boxplot

查看:83
本文介绍了如何在ggplot boxplot中自定义缺口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何更改/自定义ggplot2创建的箱形图上的槽口的上限和下限有疑问.我查看了stat_boxplot函数,发现ggplot用公式中位数+/- 1.58 * iqr/sqrt(n)计算陷波极限.但是,我想用我自己的一组上限和下限限制值来代替该方程式.

I had a question on how to change/customize the upper and lower limit of a notch on a boxplot created by ggplot2. I looked through the function stat_boxplot and found that ggplot calculates the notch limits with the equation median +/- 1.58 * iqr / sqrt(n). However instead of that equation I wanted to change it with my own set of upper and lower notch limits.

我的数据有4个因素,我为每个因素计算了中位数,并进行了自举以获取该中位数的95%置信区间.因此,最后,我想将每个箱形图更改为具有自己独特的陷波上限和下限.

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

我不确定ggplot中是否还可以做到这一点,并且想知道人们是否对此有想法吗?

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

再次感谢!

推荐答案

我想我自己终于想通了,但已经有1票否决了!!

I guess I kind of figured it out by myself in the end but 1 down vote already?!!

无论如何,我已经找到了一种使用带有功能ggplot_build的ggplot定制图上凹口的方法.

Anyways I've figured out one way to customize the notches on a plot using ggplot with the function ggplot_build.

在绘制出带有以下内容的箱线图之后:

After plotting a boxplot with say:

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

不确定ggplot_build到底发生了什么,但似乎将图转换为ish-frame结构,因此可以根据需要进行操作.

not really sure what exactly happens with ggplot_build but seems like it converts the plot into a data-frame ish structure so one can manipulate it if wanted.

gg<-ggplot_build(p)

之后:

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

包含绘图的陷波限制,您基本上可以通过以下方式更改它:

contains the notch 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

无论如何,希望这是一种有效的方法,对其他人有帮助.

Anyways hopefully this is a valid method to do and it would be of help for other people.

这篇关于如何在ggplot boxplot中自定义缺口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆