如何基于最小值和最大值创建箱形图 [英] How to create box plot based on min and max alone
本文介绍了如何基于最小值和最大值创建箱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ggplot中,我们可以通过在数据框中指定柱子的高度来创建柱状图
In ggplot, we can create a bar plot by specifying the colum in the data frame that has the height of the bars
library("ggplot2")
library(plyr)
mm <- ddply(mtcars, "cyl", summarise, mmpg = mean(mpg))
ggplot(mm, aes(x = factor(cyl), y = mmpg)) + geom_bar(stat = "identity")
但是,我无法弄清楚如何制作一个类似的图表,指定条形的顶部和底部。
例如使用下面的数据
However, I cannot figure out how to make a similar plot that both top AND bottom of the bars are specified. For instance using the data below
df <- read.table(text = " id min max
Sp1 8.5 13.2
Sp2 11.7 14.5
Sp3 14.7 17.7 ", header=TRUE)
我们会得到一个类似这样的情节:
We would get a plot closely resembling this:
有什么建议?
推荐答案
您可以使用 geom_crossbar
:
ggplot(df) +
geom_crossbar(aes(ymin = min, ymax = max, x = id, y = min),
fill = "blue", fatten = 0)
这篇关于如何基于最小值和最大值创建箱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文