David Z
David Z

Reputation: 7051

Is there a way to remove the vertical bar of geom_errorbar?

# 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)

What I want is to remove the vertical bars like the figure below: enter image description here

Upvotes: 2

Views: 1500

Answers (1)

tonytonov
tonytonov

Reputation: 25638

A bit of extra work is required for that:

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

Upvotes: 3

Related Questions