我可以在ggplot2中获得boxplot凹槽吗? [英] Can I get boxplot notches in ggplot2?

查看:130
本文介绍了我可以在ggplot2中获得boxplot凹槽吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我知道它已经出现了,我还发现哈德利在谷歌组织上的答案是,对于 ggplot2 boxplots还没有缺口。所以我的问题是双重的:这是否发生了变化(已经有一个原生的缺口实现),如果没有,那么可以做些什么。



我的意思是我不需要凹口镜片,代表一些阴影区域的置信区域,这个区域适合放置在箱形的另一层上,看起来也不错。

还添加了截图,因为我听说如果没有图形

解决方案

更新
除了下面详述的选项之外, ggplot2 0.9.0版包含 geom_boxplot 中的此功能。检查?geom_boxplot 显示缺口 notchwidth 参数:

  + geom_boxplot(notch = TRUE,notchwidth = 0.5)






不够优雅的图形,但这里有一个例子:

pre > #通过`boxplot.stats`计算出的置信区间
f< - function(x){
ans< - boxplot.stats(x)
data.frame (ymin = ans $ conf [1],ymax = ans $ conf [2])
}

#重叠图(下图上方)
p < - ggplot ,aes(Species,Sepal.Length))+ geom_boxplot()+
stat_summary(fun.data = f,geom =linerange,color =skyblue,size = 5)
p

#基本图形(下面的小图)
boxplot(Sepal.Length〜Species,data = iris,notch = TRUE)

您可以通过调整 stat_summary 的参数来更改CI栏的设置。





crossbar version:

  f < -  function(x){
ans< - boxplot.stats (x)
data.frame(ymin = ans $ conf [1],ymax = ans $ conf [2],y = ans $ stats [3])
}

p <-ggplot(iris,aes(Species,Sepal.Length))+
geom_boxplot(width = 0.8)+
stat_summary(fun.data = f,geom =crossbar,
color = NA,fill =skyblue,width = 0.8,alpha = 0.5)
p


Yes, I know it's been around, I've also found Hadley's answer on google groups that there is no notches yet for ggplot2 boxplots. So my question is twofold: Has this changed (there's a native implementation of notches already) and if not is there something one could do about it.

I mean I do not need the notch optic, representing the confidence bounds by some shaded area that is suitably placed in another layer over the boxplot, would look nice, too.

Also added a screenshot because I heard a graphics question is never complete without the graphic

解决方案

Update In addition to the options detailed below, version 0.9.0 of ggplot2 includes this feature in geom_boxplot. Examining ?geom_boxplot reveals a notch and notchwidth argument:

+ geom_boxplot(notch = TRUE, notchwidth = 0.5)


Not elegant graphics but here is an example:

# confidence interval calculated by `boxplot.stats`
f <- function(x) {
    ans <- boxplot.stats(x)
    data.frame(ymin = ans$conf[1], ymax = ans$conf[2])
}

# overlay plot (upper panel below)
p <- ggplot(iris, aes(Species, Sepal.Length)) + geom_boxplot() +
  stat_summary(fun.data = f, geom = "linerange", colour = "skyblue", size = 5)
p

# base graphics (lower panel below)
boxplot(Sepal.Length ~ Species, data = iris, notch = TRUE)

you can change the apparence of CI bar by tweaking the arguments of stat_summary.

crossbar version:

f <- function(x) {
  ans <- boxplot.stats(x)
  data.frame(ymin = ans$conf[1], ymax = ans$conf[2], y = ans$stats[3])
}

p <- ggplot(iris, aes(Species, Sepal.Length)) + 
  geom_boxplot(width = 0.8) +
  stat_summary(fun.data = f, geom = "crossbar", 
    colour = NA, fill = "skyblue", width = 0.8, alpha = 0.5)
p

这篇关于我可以在ggplot2中获得boxplot凹槽吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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