position_dodge with geom_errorbar

时间:2013-10-31 12:00:29

标签: r ggplot2

我有以下代码

require(ggplot2)
pd <- position_dodge(0.3)
ggplot(dt, aes(x=Time, y=OR, colour=Group)) + 
    geom_errorbar(aes(ymin=CI_lower, ymax=CI_upper), colour="black", width=.4, position=pd) +
    geom_point(size=2.2, position=pd) +
    geom_hline(aes(yintercept=1), colour="#990000", linetype="dashed")

产生这个

enter image description here

和错误ymax not defined: adjusting position using y instead

数据是:

dt <- structure(list(CI_upper = c(1.93, 1.34, 0.73, 0.69, 0.99, 0.81), CI_lower = c(0.54, 0.66, 0.34, 0.48, 0.34, 0.49), Time = structure(c(1L, 1L, 2L, 2L, 3L, 3L), .Label = c("1", "2", "12"), class = "factor"), 
OR = c(1.02, 0.94, 0.5, 0.58, 0.58, 0.63), Group = c("D", 
"ND", "D", "ND", "D", "ND"
)), .Names = c("CI_upper", "CI_lower", "Time", "OR", "Group"), row.names = c(NA, 6L), class = "data.frame")

有人能告诉我如何将闪避应用到错误栏以及积分吗?

1 个答案:

答案 0 :(得分:3)

它需要一个分组变量:

ggplot(dt, aes(x=Time, y=OR, colour=Group)) + 
  geom_errorbar(aes(ymin=CI_lower, ymax=CI_upper, colour=NULL, group=Group),  
                 position=pd) +
  geom_point(size=2.2, position=pd) +
  geom_hline(aes(yintercept=1), colour="#990000", linetype="dashed")

enter image description here