geom_errorbar()重叠和定位问题

时间:2014-06-23 19:15:08

标签: r ggplot2

我在使用geom_errorbars时遇到问题,特别是在此脚本中有效利用position_dodge()。

library(ggplot2)
library(plyr)

Dose <- rep(c(3,10,30,100), each = 6)
Visit <- rep(c(1,28), each = 3, times = 4)
Animal <- rep(1:3,  times = 8)
Estimate <- runif(24)

Dose <- factor(Dose)
Visit <- factor(Visit)

df <- data.frame(Animal, Dose, Visit, Estimate)

e <- ddply(df, .(Dose, Visit), summarise, mean = mean(Estimate), sd = sd(Estimate), n = length(Estimate))
e$se = e$sd/sqrt(e$n)

trace.out <- ggplot(data = e, aes(x = Visit, y = mean, colour = Dose))
trace.out <- trace.out + 
  geom_point(data = e, aes(y = mean), size = 3, postion = position_dodge(width = 0.2)) +
  geom_line(data = e, aes(y = mean, group = Dose), position = position_dodge(width = 0.2)) +
  geom_errorbar(aes(ymin= mean - se, ymax = mean + se), postion = position_dodge(0.2), colour='black', width= 0.3) +
  labs(y = 'Estimate') +
  theme_bw()

print(trace.out)

我的输出看起来像: Wonky Lines

我希望点,线和误差线排成一行,并使错误栏不重叠。有办法做到这一点吗?另外我收到错误:

ymax not defined: adjusting position using y instead

这与它有什么关系吗?在此先感谢!

1 个答案:

答案 0 :(得分:2)

也许方面是一个选择:

trace.out <- ggplot(data = e, aes(x = Visit, y = mean, colour = Dose, ymin= mean - se, ymax = mean + se, group = Dose))
trace.out <- trace.out + 
  geom_point(size = 3, postion = position_dodge(width = 0.2)) +
  geom_line(position = position_dodge(width = 0.2), ) +
  geom_errorbar(postion = position_dodge(0.2), colour='black', width= 0.3) +
  labs(y = 'Estimate') +
  theme_bw()
print(trace.out + facet_grid(~Dose) )

enter image description here