我可以在ggplot2中获得boxplot凹槽吗?
问题描述:
是的,我知道它已经出现了,我还发现哈雷利在谷歌集团的答案是,没有缺口ggplot2
箱型图。所以我的问题是双重的:这是否发生了变化(已经有一个原生的缺口实现),如果没有,那么可以做些什么。我可以在ggplot2中获得boxplot凹槽吗?
我的意思是我不需要凹口镜片,代表一些阴影区域的置信区域,这个区域适合放置在盒状图上的另一层上,看起来也不错。
同时加入了一个截图,因为我听到一个图形的问题从来都不是不完整的图形
答
更新 除了下文详述的选项中,GGPLOT2 0.9.0版本包含此功能geom_boxplot
。检查?geom_boxplot
透着notch
和notchwidth
说法:
+ geom_boxplot(notch = TRUE, notchwidth = 0.5)
不优雅图形,但这里有一个例子:
# 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)
您可以通过调整的stat_summary
参数改变CI栏apparence。
横梁版本:
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-dev mailing list关于notched box plots后已经形成。
你可以在github找到开发页面。该包可能安装通过:
# install.packages("devtools")
library(devtools)
install_github("ggplot2")
不错!通过扩大天空矩形并增加一点透明度,基本上是完美的。非常感谢你。 –
嗯,我认识到固定大小是缩放情节时的一个问题。我们不能有一个更动态的大小? –
已更新。我把交叉开关版本。 – kohske