Search code examples
rggplot2legendfacet-wrap

Rename Legend title on facet_wrap


I have a facet wrap of 8 plots and I want to rename the Legend, it currently defaults to the fill value Depth_bins but I want to be able to change that. I've tried various methods, including the below using scale_color_discrete but nothing changes the title. I'm assuming this is because it's a facet_wrap.

facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
  geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
  scale_x_datetime(date_labels = "%H:%M") +
  labs(x = "Start hour", y = "Density") + 
  facet_wrap(~ID, dir = "v", scales = "free_y") + 
  scale_color_discrete(name = "Depth Bins")
facet_8

any help much appreciated!


Solution

  • I think the labs function should work as mentioned here:

    How to change legend title in ggplot

    So, something like this:

    facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
      geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
      scale_x_datetime(date_labels = "%H:%M") +
      labs(x = "Start hour", y = "Density") + 
      facet_wrap(~ID, dir = "v", scales = "free_y") + 
      labs(fill = "Your Title Here")
    facet_8