Learn R Programming

topolow (version 1.0.0)

save_plot: Save Plot to File

Description

Saves a plot (ggplot or rgl scene) to file with specified configuration. Supports multiple output formats and configurable dimensions.

Usage

save_plot(plot, filename, layout_config = new_layout_config(), output_dir)

Value

No return value, called for side effects (saves a plot to a file).

Arguments

plot

ggplot or rgl scene object to save

filename

Output filename (with or without extension)

layout_config

Layout configuration object controlling output parameters

output_dir

Character. Directory for output files. This argument is required.

Details

Supported file formats:

  • PNG: Best for web and general use

  • PDF: Best for publication quality vector graphics

  • SVG: Best for web vector graphics

  • EPS: Best for publication quality vector graphics

The function will:

  1. Auto-detect plot type (ggplot or rgl)

  2. Use appropriate saving method

  3. Apply layout configuration settings

  4. Add file extension if not provided

Examples

Run this code
# The sole purpose of save_plot is to write a file, so its example must demonstrate this. 
# For CRAN tests we wrap the example in \donttest{} to avoid writing files.
# \donttest{
# Create a temporary directory for saving all plots
temp_dir <- tempdir()

# --- Example 1: Basic ggplot save ---
# Create sample data with 3 dimensions to support both 2D and 3D plots
data <- data.frame(
  V1 = rnorm(10), V2 = rnorm(10), V3 = rnorm(10), name=1:10,
  antigen = rep(c(0,1), 5), antiserum = rep(c(1,0), 5),
  year = 2000:2009
)
p <- plot_temporal_mapping(data, ndim=2)
save_plot(p, "temporal_plot.png", output_dir = temp_dir)

# --- Example 2: Save with custom layout ---
layout_config <- new_layout_config(
  width = 12,
  height = 8,
  dpi = 600,
  save_format = "pdf"
)
save_plot(p, "high_res_plot.pdf", layout_config, output_dir = temp_dir)

# --- Verify files and clean up ---
list.files(temp_dir)
unlink(temp_dir, recursive = TRUE)
# }

Run the code above in your browser using DataLab