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
See Question&Answers more detail:
os