How to eliminate separate legend for geom_ribbon

I have generated a plot showing median expected value over time with a 95% confidence interval ribbon plot. But there are two repeated legends. How do I remove the left hand legend?
Thanks for the help. Larry Hunsicker

ggplot(p1, aes(testyrs, yfit, color = StudyID)) +
  geom_line() + 
  scale_color_discrete(name ="CIT Study") +
  geom_ribbon(aes(testyrs, ymin = pi_lb, ymax = pi_ub, fill = StudyID), 
          alpha = .2) +
  theme(legend.position = "bottom") +
  labs(x = "Years following first PHPI transplant", y = 'HbA1c',
       title = "Projected course of HbA1c following PHPI transplant",
       subtitle = "Median and 95% credible interval, CIT-07 and CIT-06")


You can merge both legends by giving the same name to both aesthetics, since you haven't provided a REPRoducible EXample (reprex) here is an example using the iris built-in dataset.


ggplot(iris, aes(Petal.Length, Petal.Width, color = Species, fill = Species)) +
    geom_line() + 
    geom_ribbon(aes(ymin = Petal.Width -1, ymax = Petal.Width + 1), 
                alpha = .2) +
    theme(legend.position = "bottom") +
    labs(title = "Example",
         color = "Species", 
         fill = "Species")

Created on 2019-09-26 by the reprex package (v0.3.0)

1 Like

Alternatively, geom_ribbon has a show.legend parameter which you could set to FALSE

Thanks, Valeri. That worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.