Error bars in ggplot in middle of bin [duplicate]
Asked Answered
K

1

2

I'm trying to make a bar plot in ggplot with 2 values for each bin (fold change values from RNA-Seq and qPCR experiments):

Gene    FC  expt    se
a   1.02    RNA-Seq 0
b   2.79    RNA-Seq 0
c   1.63    RNA-Seq 0
d   0.84    RNA-Seq 0
e   0.81    RNA-Seq 0
f   1.45    RNA-Seq 0
g   1.27    RNA-Seq 0
h   1.72    RNA-Seq 0
i   2.52    RNA-Seq 0
a   0.84    qPCR    0.16
b   1.92    qPCR    0.15
c   1.14    qPCR    0.78
d   0.47    qPCR    0.76
e   0.95    qPCR    0.26
f   0.32    qPCR    0.51
g   0.92    qPCR    0.39
h   0.97    qPCR    0.61
i   1.73    qPCR    0.77

My RNA-Seq values don't have error bars. As such I want to plot a barchart with:

  • Error bars that only extend up
  • Error bars only for q-PCR bars

I'm not sure where my code (or input format) is going wrong:

df <- read.table("stack.txt", header=TRUE)
limits <- aes(ymax = FC + se, ymin = FC)
dodge <- position_dodge(width = 0.9)

ggplot(data=df, aes(x=Gene, y=FC)) +
geom_errorbar(limits, position = dodge, width = 0.25) +
geom_bar(aes(fill=expt),colour="black", stat="identity", position = dodge)

Which produces:

enter image description here

As you can see, the error bars are in the middle of each bin, rather than being onto of each bar. Any suggestions/comments would be hugely appreciated!

Kourtneykovac answered 16/7, 2015 at 10:55 Comment(2)
position_dodge(width = <...>)Neckerchief
@Neckerchief It's the same question, but the data and answers are different it would seem. At least as far as I can see they don't help me much, whereas the answer provided below doesKourtneykovac
C
1

When you add the group parameter to the aes, you will get the desired result:

ggplot(data=df, aes(x=Gene, y=FC, fill=expt, group=expt)) +
  geom_bar(colour="black", stat="identity", position = position_dodge(width = 0.9)) +
  geom_errorbar(aes(ymax = FC + se, ymin = FC, group=expt),
                position = position_dodge(width = 0.9), width = 0.25)

this gives:

enter image description here

Chelsea answered 16/7, 2015 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.