Home > OS >  Formatting legend to fit 45 legend items in R
Formatting legend to fit 45 legend items in R

Time:11-26

I need help formatting a legend in ggplot2. I have approximatley 45 legened items. When I display the legend, my graph shrinks becuase the graph and legend items don't fit. I'm wondering how I can get all my legend items to display, but also have a reasonably sized graph. Is there a way to make my longer legend items go over multiple lines? Or, is there a way to make some legend items occupy more of the white space above/below the page? Any help will be super appreciated! Below is a screenshot of my current plot, along with my code. enter image description here

guild_chart <- 
ggplot(chart, aes(x=factor(Site,level=level_order1), y=`Row 1`, fill=Label))   
  geom_bar(stat="identity")   
  scale_fill_manual(values =colfundose)   
  theme_bw()  ylab("# of reads")   
  xlab("Location")

CodePudding user response:

A frame challenge, if I may:

This is probably a bad way to visualise this data. The groups are impossible to distinguish from one another, and very difficult to compare. What is the purpose of this graph? What information do you wish to convey to the viewer? With that question in mind, think about how you can design the graph in a legible way.

To increase legibility, I would consider combining factors into groups and visualising these instead of the individual levels you are currently displaying.

CodePudding user response:

As others have noted, presenting your data in this stacked bar layout is difficult to interpret. In addition to the challenge in discriminating between different groups, its also tough to estimate the number of reads for any sort of comparison.

As an alternative presentation, would it make sense to visualize these read counts as a heatmap? You could have a column (or row) for each of your seven locations containing 45 squares, colored to indicate # of reads. Now your legend is a color gradient with the range of read counts across the dataset. An advantage here is you can keep your 45 categories, if this is important, but have them right next to their respective rows, minimizing lookups in a legend.

  • Related