1

I have been trying to display error bars on one of my plots and have almost gotten it. I can get the error bars to appear on the plot correctly other than the fact that two of the error bars do not appear. Here is my code:

    adat <- data.frame(1:8)
names(adat)[1] <- "Technology"
adat$Technology <- c("1","1","1","1","2","2","2","2")
adat$Behaviors <- c("Low Moisture","High Moisture","Low Density","High Density","Low Moisture","High Moisture","Low Density","High Density")
adat$Average.duration <- c(374,347,270,313,273,280,242,285)
adat$sd <- c(207,107,120,920,52,61,50,84)

limits <- aes(ymin = adat$Average.duration - adat$sd, ymax = adat$Average.duration + adat$sd)

ggplot(adat, aes(x = Behaviors, y = Average.duration, fill = Technology)) +
  geom_bar(stat = "identity", position = "dodge") + 
  ylim(0,500) +
  geom_errorbar(limits, position = position_dodge(.9), width = .75)

When I run the above code I get the following output: enter image description here

I don't know why two of the error bars do not show up. Any suggestions?

1 Answer 1

1

The issue is that your error bar is extending beyond the limits you set with ylim(). Try plotting using this:

limits <- aes(ymin = Average.duration - sd, ymax = Average.duration + sd)

ggplot(adat, aes(x = Behaviors, y = Average.duration, fill = Technology)) +
  geom_bar(stat = "identity", position = "dodge") + 
  geom_errorbar(limits, position = "dodge")

First Plot

In addition, notice, you do not need to supply adat$... to your limits call since you will "set it" in your call to ggplot().

The takeaways being that ylim() and scale_y_continuous() will remove data points outside their range. If you want to still plot within the 0 to 500 y-range, you will need to use coord_cartesian():

ggplot(adat, aes(x = Behaviors, y = Average.duration, fill = Technology)) +
  geom_bar(stat = "identity", position = "dodge") + 
  geom_errorbar(limits, position = "dodge") +
  coord_cartesian(ylim = c(0, 500))

Plot With Coord_Cartesian

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.