0

I'm looping through a spatial feature dataframe and creating hundreds of maps from each column using ggplot2. Depending on the data present in the column, the legend for each map may be a different size, making the plot size different for each map. Although subtle, you can see below that the first map is offset to the left relative to the second map because of the longer label in the legend enter image description hereenter image description here

I'd like to maintain the size of the plot area (in this case, the map area) so that all the saved maps are consistently sized and lined up when flipping through them. I've looked through questions and answers on here, but all I've been able to find are solutions for how to preserve aspect/ratio of different plots on the same image/document as described here. I'd like to be able to save each map as its own file.

Can anyone give me suggestions about how to keep the plot size consistent regardless of legend width when saving these plots as images using ggplot2?

mmoore
  • 174
  • 1
  • 9
  • 2
    Just shooting from a hip, perhaps you could sanitize factors before plotting. Checking length of each factor label and if it exceeds some defined length, insert a `\n` and reflow the label This would only set a max width of the legend, though. – Roman Luštrik Aug 16 '18 at 15:31
  • 1
    `stringr::str_wrap` works well for @RomanLuštrik's suggestion – camille Aug 16 '18 at 15:34
  • 1
    Another option would be to have a consistent legend across all maps that has all possible values. – Jacob F Aug 16 '18 at 15:40
  • 1
    If you include some sample data and the code you use to produce the maps it is more likely that someone can help you. – erc Aug 17 '18 at 11:32

0 Answers0