强制错误栏位于栏的中间 [英] Force error bars to be in the middle of bar

查看:107
本文介绍了强制错误栏位于栏的中间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用R和ggplot2来绘制带有误差线的漂亮的基本条形图,但是我遇到了麻烦,迫使它们在我创建的线条的中心。



我的代码如下:

  library(ggplot2)$ (b)b 

类型<-c(A,A,A,B,B,B)
时间< - c (0,24,48,0,24,48)
CellCount <-c(321,213,123,432,234,324)
Error -c(12,32,12,34,23,21)$ b $ (df,aes(x = Time,y = CellCount,fill = Type))+ $ b(df,aes $ b geom_bar(stat =identity,position =dodge)+
xlab(Time(h))+ ylab(Cell count)+
scale_x_continuous(breaks = seq(0 ,48,24))+
scale_fill_discrete(labels = c(Anirridia,Control))+
guides(fill = guide_legend(title = NULL))

p + geom_errorbar(aes(ymin = CellCount - Error,
ymax = CellCount + Error),
width = 0.9,
position =dodge)



但是,这会产生以下图表: $ b



错误栏位于栏的边缘。我认为这是因为他们在0,24和48小时的时间内被绘制出来,但我不知道如何将他们移动到每个栏的中心

请告诉我如何移动错误栏



预先感谢

编辑:我以前看到这个链接的问题是重复的,但是当我添加

  p + geom_errorbar(aes(ymin = CellCount - 错误,
ymax = CellCount + Error),
width = 0.9,
position = position_dodge(.9))

我仍然得到和以前一样的情节



Edit2:我认为Rstudio可能存在问题?或者至少在我的安装中...因为我复制的代码粘贴在重复的问题中,我得到了下图: http://imgur.com/H2DRvqg

解决方案

虽然@ jeremycg的回答是正确的(+ 1为ggplot主题系统的晦涩知识),它更多的是一个副作用比解决方案。



让我们想一下典型的用例情景条形图。通常,我们会在试图呈现数据时使用条形图: b
$ b


  • 连续离散



现在,我们有以下数据:


  • / li>


对于这种数据类型,点图更为合适。但我们可以看到它看起来有点荒谬,因为我们的数据并不是真正连续的。

  ggplot(df,aes(Time, CellCount,color = Type))+ 
geom_point(stat =identity)+
geom_errorbar(aes(
ymin = CellCount - Error,
ymax = CellCount + Error),
width = 0.9)

对于这种性质的表示,应该使用分类变量为它们的x坐标。

这可以通过预先处理变量Time作为一个因子或通过在美学字符串中调用变量as.factor来完成( aes )。

  ggplot(df,aes(
x = as.factor时间),#as.factor()使得它成为离散比例
y = CellCount,
fill = Type))+
geom_bar(stat =identity,position =dodge)+
geom_errorbar(aes(
ymin = CellCount - Error,
ymax = CellCount + Error),
width = 0.9,
position = position_dodge(width = 0.9)) +#现在你可以使用它
labs(x =Time(h),y =Cell count)+
scale_fill_discrete(labels = c(Anirridia,Control))


I'm using R and ggplot2 to plot a pretty basic bar graph with error bars, but I'm having trouble forcing them to be in the center of the bars that I create.

My code is the following:

library(ggplot2)


Type <- c("A", "A", "A", "B", "B", "B")
Time <- c(0, 24, 48, 0, 24, 48)
CellCount <- c(321,213,123,432,234,324)
Error <- c(12,32,12,34,23,21)
df <- data.frame(Type,Time,CellCount,Error)

p <- ggplot(df, aes(x=Time, y = CellCount, fill = Type)) + 
     geom_bar(stat = "identity", position = "dodge") + 
     xlab("Time (h)") + ylab("Cell count") + 
     scale_x_continuous(breaks = seq(0,48,24)) +
     scale_fill_discrete(labels = c("Anirridia", "Control")) + 
     guides(fill = guide_legend(title = NULL))

p + geom_errorbar(aes(ymin = CellCount - Error, 
                      ymax = CellCount + Error), 
                      width = 0.9, 
                      position = "dodge")

But this produces the following plot:

The error bars are at the edges of the bars. I think it´s because they are being plotted at 0, 24 and 48 hours of time, but I don't know how to move them to the center of each bar

Please suggest me how can I "move" the error bars

Thanks in advance

Edit: I previously saw the question that has been linked for this one as duplicate, but when I add

 p + geom_errorbar(aes(ymin = CellCount - Error, 
                  ymax = CellCount + Error), 
                  width = 0.9, 
                  position=position_dodge(.9))

I'm still getting the same plot as before

Edit2: I think there's an issue with Rstudio maybe? Or at least with my installation... Because I´ve copy pasted the code in the "duplicate" question and I get the following graph... http://imgur.com/H2DRvqg

解决方案

While @jeremycg's answer is correct (+1 for obscure knowledge of ggplot theme system), it's more of a side-effect than a solution.

Let's just think for a moment of the typical use case scenario for a bar chart. Generally we would use a bar chart when we are trying to present data that are:

  • Discrete by Continuous

Right now, we have data that are:

  • Continuous by Continuous

A dot plot would be more appropriate for this data type. But we can see that it looks a bit ridiculous because our data are not truly continuous.

ggplot(df, aes(Time,CellCount, color = Type)) + 
  geom_point(stat="identity") + 
  geom_errorbar(aes(
    ymin = CellCount - Error, 
    ymax = CellCount + Error),   
    width = 0.9)

For a presentation of this nature, one ought to use a categorical variable for their x-coordinate.

This can be accomplished by pre-processing your variable, Time, as a factor, or by calling the variable as.factor in the aesthetic string (aes).

ggplot(df, aes(
    x=as.factor(Time), # as.factor() makes this a discrete scale
    y = CellCount, 
    fill = Type)) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_errorbar(aes(
    ymin = CellCount - Error, 
    ymax = CellCount + Error),   
    width = 0.9, 
    position = position_dodge(width = 0.9)) + # now you can use it
  labs(x= "Time (h)", y = "Cell count") +
  scale_fill_discrete(labels = c("Anirridia", "Control")) 

这篇关于强制错误栏位于栏的中间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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