画出"错误栏"适用于多个类别

时间:2015-12-03 18:39:47

标签: r plot boxplot

我有一个数据框,其中包含不同类别的(最小,最大)值,例如:

<script>
$('.chatbox-emoji-trigger').click(function() {
  $(this).append(("<%= escape_javascript(render('shared/emoji_modal')) %>"));
});
</script>

我想制作一个图表,其中,对于每个类别,只绘制一个底部和顶部有短水平线段的垂直线段:它看起来像一个误差条,但它没有中心点。但是,如果有必要,我可以接受中心点。必须使用相应的类别名称标记每个段,如框图的框:

    case1     case2
 1 0.8945 0.8943867
 2 0.8950 0.8952849

enter image description here

但是条形图的外观必须是boxplot(df,cex.axis=1.5) plotCI等函数的外观:

segments

enter image description here

这看起来不错,但x轴是数字,而我需要x轴上的类别名称。此外,我不喜欢这些酒吧彼此相距太远,并且非常接近情节边缘。是不是可以把它们放在靠近中心的位置,就像箱子的盒子一样?

1 个答案:

答案 0 :(得分:2)

您至少有两种可能性:

  • 没有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"))
    

enter image description here

  • 使用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()

enter image description here

相关问题