rggplot2geom-ribbon

How to give and display the name (in the legend) to a geom_ribbon in ggplot?


I just added a shadded area to my plot to highlight a given region of the plot using geom_ribbon. However I cannot figure out how to define a name to this ribbon and how to male it display on the legend. In the case below I actually have two ribbons I want to display on my graph.

TM_M_WILD_plot <- ggplot(data = TM_M_WILD, aes(x = Age, y = MeasurementValue, group = Sex, colour = Sex, fill= Sex)) +
  #geom_line(linewidth = 1) +
  #geom_smooth(method=lm, se=FALSE, colour= 'black', linetype="twodash")+
  geom_point(size = 4.5) +
  geom_ribbon(data = TM_M_WILD, aes(ymin = 163, ymax = 386), 
              alpha= 0.6, colour = NA, fill= "royalblue")+
  geom_ribbon(data = TM_M_WILD, aes(ymin = 331, ymax = 979), 
              alpha= 0.6, colour = NA, fill= "orange")+
  scale_color_manual(values = c('black')) +
  scale_fill_manual(values = c("black"))+
  scale_x_continuous(expand=c(0, 0), limits=c(0, 40), breaks = seq(0, 40, 5))+
  scale_y_continuous(expand=c(0, 0), limits=c(0, 1000), breaks = seq(0, 1000, 200), guide=guide_axis(minor.ticks = TRUE))+
  labs(
    x = "Age (years)",
    y = "Body Mass (Kg)",
    title = "Body mass by age")+
  theme_gray(base_size = 55
  )

TM_M_WILD_plot

TM_M_WILD_plot + theme(panel.background = element_rect(fill = "white"),
                       #panel.border = element_rect(fill = "transparent", color = 'black', size = 2),
                       #panel.grid.major = element_blank(),
                       panel.grid.major = element_line(colour = "grey", size=0.5),
                       panel.grid.minor = element_blank(),
                       axis.line = element_line(size = 0.9, linetype = "solid", colour = "black"),
                       #axis.text = element_text(face="bold"),
                       axis.text = element_text(colour="black"),
                       legend.position="none",
                       legend.title = element_blank(),
                       #plot.title = element_text(hjust = 0.5)
                       plot.title = element_blank(),
                      
)

Solution

  • The things you want to appear as legend items need to be declared inside aes(). In your case , you can move fill =, then assign your colours using scale_*_manual().

    There are a few contradictions in your code e.g. assigning colour = sex but then you set scale_color_manual(values = c('black')) and defining titles but then running legend.title = element_blank(). As such, a number of modifications were made to your code. Further, it makes sense to plot your points on top of your ribbons.

    This example uses a a 'toy' dataset as you did not provide one. Please review How to make a great R reproducible example to learn what is expected in a question like this.

    library(ggplot2)
    
    # Example data
    set.seed(42)
    TM_M_WILD <- data.frame(
      Age = sample(5:39, 100, replace = TRUE),
      MeasurementValue = runif(100, 10, 900),
      Sex = sample(c("Female", "Male"), 100, replace = TRUE)
      )
    
    ggplot(
      data = TM_M_WILD,
      aes(Age, MeasurementValue, colour = Sex)) +
      geom_ribbon(aes(ymin = 163, ymax = 386, fill = "Ribbon 1"), 
                  alpha = 0.6,
                  colour = NA) +
      geom_ribbon(aes(ymin = 331, ymax = 979, fill = "Ribbon 2"), 
                  alpha = 0.6,
                  colour = NA) +
      geom_point(size = 4.5) +
      scale_color_manual(values = c("blue", "#F8766D")) +
      scale_fill_manual(name = "Ribbons",
                        values = c("royalblue", "orange")) +
      scale_x_continuous(expand=c(0, 0),
                         limits=c(0, 40),
                         breaks = seq(0, 40, 5)) +
      scale_y_continuous(expand = c(0, 0),
                         limits = c(0, 1000),
                         breaks = seq(0, 1000, 200),
                         guide = guide_axis(minor.ticks = TRUE)) +
      labs(x = "Age (years)",
           y = "Body Mass (Kg)",
           title = "Body mass by age") +
      theme_grey(base_size = 10) +
      theme(panel.background = element_rect(fill = "white"),
            panel.grid.major = element_line(colour = "grey", linewidth = 0.5),
            axis.line = element_line(size = 0.9, linetype = "solid", colour = "black"))
    

    1