Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

r - Force error bars to be in the middle of bar

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:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...