2015-04-02 72 views
2
# 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有没有办法删除geom_errorbar的垂直条?

回答

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

相关问题