有没有办法删除geom_errorbar的垂直条?

时间:2015-04-02 17:39:08

标签: r ggplot2

# Create a simple example dataset
df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  se = c(0.1, 0.3, 0.3, 0.2)
)
df2 <- df[c(1,3),]

# Define the top and bottom of the errorbars
limits <- aes(ymax = resp + se, ymin=resp - se)

p <- ggplot(df, aes(fill=group, y=resp, x=trt))
p + geom_point() + geom_errorbar(limits, width=0.2)

我想要的是删除垂直条,如下图所示: enter image description here

1 个答案:

答案 0 :(得分:3)

需要做一些额外的工作:

df$trt_low <- as.numeric(df$trt) - 0.1
df$trt_high <- as.numeric(df$trt) + 0.1

limits_low <- aes(y = resp - se, yend = resp - se, x = trt_low, xend = trt_high)
limits_high <- aes(y = resp + se, yend = resp + se, x = trt_low, xend = trt_high)

p + geom_point() + 
  geom_segment(data=df, limits_low, width=0.2) + 
  geom_segment(data=df, limits_high, width=0.2)

enter image description here