为多个类别绘制 "error bars"
Draw "error bars" for multiple categories
我有一个数据框,其中包含不同类别的 (min,max) 值,例如:
case1 case2
1 0.8945 0.8943867
2 0.8950 0.8952849
我想绘制一个图,其中对于每个类别,只绘制一个垂直段,底部和顶部有短的水平段:它看起来像一个错误栏,但它没有中心点。不过,如有必要,我可以接受中心点。每个线段都必须标有相应的类别名称,如箱线图的方框:
boxplot(df,cex.axis=1.5)
但是条形图的外观必须是 plotCI
、segments
等函数的外观:
require(plotrix)
x=0:1
plotCI(x,apply(df,2,mean),li=df[1,],ui=df[2,],xlab="categories",ylab="values")
这看起来不错,但 x 轴是数字,而我需要 x 轴上的类别名称。另外,我不喜欢条形图彼此离得太远,而且离图边距太近。是否可以将它们放在靠近中心的位置,就像箱线图的箱子一样?
你至少有两种可能:
没有ggplot2
:
require(plotrix)
plotCI(apply(df,2,mean),li=df[1,],ui=df[2,],xlab="categories",ylab="values")
axis(side = 1, at = c(1,2), labels = c("case1", "case2"))
- 与
ggplot2
:
首先重新定义你的data.frame
:
df2 = data.frame(vmin = c(0.8945, 0.8943867), vmax=c(0.8950, 0.8952849), case=c("case1", "case2"))
然后,使用geom_errorbar
library(ggplot2)
p <- ggplot(df2, aes(ymin = vmin, ymax = vmax, x = case))
p + geom_errorbar()
我有一个数据框,其中包含不同类别的 (min,max) 值,例如:
case1 case2
1 0.8945 0.8943867
2 0.8950 0.8952849
我想绘制一个图,其中对于每个类别,只绘制一个垂直段,底部和顶部有短的水平段:它看起来像一个错误栏,但它没有中心点。不过,如有必要,我可以接受中心点。每个线段都必须标有相应的类别名称,如箱线图的方框:
boxplot(df,cex.axis=1.5)
但是条形图的外观必须是 plotCI
、segments
等函数的外观:
require(plotrix)
x=0:1
plotCI(x,apply(df,2,mean),li=df[1,],ui=df[2,],xlab="categories",ylab="values")
这看起来不错,但 x 轴是数字,而我需要 x 轴上的类别名称。另外,我不喜欢条形图彼此离得太远,而且离图边距太近。是否可以将它们放在靠近中心的位置,就像箱线图的箱子一样?
你至少有两种可能:
没有
ggplot2
:require(plotrix) plotCI(apply(df,2,mean),li=df[1,],ui=df[2,],xlab="categories",ylab="values") axis(side = 1, at = c(1,2), labels = c("case1", "case2"))
- 与
ggplot2
:
首先重新定义你的data.frame
:
df2 = data.frame(vmin = c(0.8945, 0.8943867), vmax=c(0.8950, 0.8952849), case=c("case1", "case2"))
然后,使用geom_errorbar
library(ggplot2)
p <- ggplot(df2, aes(ymin = vmin, ymax = vmax, x = case))
p + geom_errorbar()