在仓的中间ggplot误差棒[复制](Error bars in ggplot in middle

2019-10-23 22:16发布

这个问题已经在这里有一个答案:

  • 力误差线是在栏的中间 2回答

我试图在ggplot柱状图与2个值对每个仓(折从RNA-Seq的和qPCR实验变化值):

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

我的RNA-Seq的值没有错误吧。 因此我想绘制一个条形图:

  • 误差线,只有向上延伸
  • 仅适用于Q-PCR酒吧误差线

我不知道在我的代码(或输入格式)是哪里错了:

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)

主要生产:

正如你所看到的,误差条在每个区间的中间,而不是在每个吧。 任何建议/意见将非常感激!

Answer 1:

当您将添加group参数的aes ,你会得到期望的结果:

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)

这给:



文章来源: Error bars in ggplot in middle of bin [duplicate]
标签: r ggplot2