Search code examples
rggplot2legend-properties

Legend box out of boundary when saving with ggsave()


I've placed my legend horizontally (5 categories) below my barpchart, within r this works fine, but when I save my plot as a picture some categories from the legend disappear or are out of boundary of the plot/picture. I used ggsave("myPlot.png", p, units = "cm", dpi = 600) in which p represents my plot. Should I fix this within my ggplot call or within ggsave() and how? I want a smaller length of the complete legend box (in the final saved picture). I've included the picture, so you can see how the legend went wrong. Help is appreciated! Thank you.

enter image description here


Solution

  • save_plot() from the cowplot package avoids this

    instead of:

    ggsave("myPlot.png", p)

    try:

    save_plot("myPlot.png", p)