Skip to contents

Attempts to save a plot to PDF and optionally PNG. If saving fails due to the "Viewport has zero dimension(s)" error (often caused by too many legend entries), the legend is removed and the plot is saved again.

Usage

save_plot_with_fallback(
  plot,
  pdf_file,
  png_file = NULL,
  width = NULL,
  height = NULL,
  dpi = 300
)

Arguments

plot

A ggplot object to save.

pdf_file

Path to the PDF output file.

png_file

Optional path to the PNG output file.

width

Plot width in inches.

height

Plot height in inches.

dpi

Resolution for PNG output.

Value

The saved plot (possibly modified with legend removed).