0

I am using ggplot2 to create a heatmap, and am grouping my y variable by a categorical variable using facet_grid(). My y variables are genes of interest, and I am grouping them by their respective Pathways using facet_grid().

The pathway names are so long that I gave most of them acronyms, but want to use a legend to detail exactly what each acronym stands for.

How can I add a separate legend specifically for the variable I am calling in facet_grid()?

So far, everything I have found online tells me how to customize legends pertaining to the x or y variable - but I can't seem to find anything on how to create a legend specifically for the facet_grid() or facet_wrap() variables.

I already have a legend describing the color gradient in the heatmap, and do not want to lose this legend.

I have also included a screenshot of the kind of legend I'd like to make.enter image description here

I have attached a screenshot of one of my heatmaps showing the facet_grid() labels - some of these labels are abbrevations that I want to explain in a legend.

Screenshot of heatmap showing some facet_grid() labels

Linton
  • 45
  • 5
  • No, there is not any built-in way to make a legend for a facet label. But there ways to add a caption, or add annotations, or an inset plot area. Can you draw or describe what you're looking for? – Jon Spring Jun 02 '23 at 19:31
  • Keep in mind you can also add line breaks, either with the text `\n` or with functions like `stringr::str_wrap()`, either to your y axis labels and/or your facet labels. – Jon Spring Jun 02 '23 at 19:36
  • I added an example of what I'd like the legend to be above, but basically I just want to include a key for the pathway acronyms I am using (for example, explain what 3-H BC is). – Linton Jun 02 '23 at 20:42
  • I think these SO questions might help. It sounds like the main difference is where you want the text and how big it is, which might require some tweaking to the theme to leave enough room. https://stackoverflow.com/questions/12409960/ggplot2-annotate-outside-of-plot and https://stackoverflow.com/questions/54776142/add-text-outside-plot-area – Jon Spring Jun 02 '23 at 21:26
  • Another approach would be to create a blank plot that is just your legend and inset that like here: https://patchwork.data-imaginist.com/reference/inset_element.html – Jon Spring Jun 02 '23 at 21:29

0 Answers0