Skip to contents

This function takes the output of either `margot_plot()` or `margot_plot_multi_arm()` and saves the plot as a PNG image using `ggsave()`.

This function saves a plot object as a PNG image, supporting multiple plotting systems including ggplot2, base R plots, and other plotting libraries.

Usage

margot_save_png(
  plot_output,
  prefix = NULL,
  base_filename = "plot",
  save_path = here::here("push_mods"),
  width = 12,
  height = 12,
  dpi = 500
)

margot_save_png(
  plot_output,
  prefix = NULL,
  base_filename = "plot",
  save_path = here::here("push_mods"),
  width = 12,
  height = 12,
  dpi = 500
)

Arguments

plot_output

Either a plot object directly, or a list containing a plot element. Supported plot types include ggplot objects, base R plot objects, and others.

prefix

Character string. A prefix to add to the filename. Default is NULL.

base_filename

Character string. The base name for the saved file. Default is "plot".

save_path

Character string. The directory path where the image will be saved. Default is here::here("push_mods").

width

Numeric. The width of the saved image in inches. Default is 16.

height

Numeric. The height of the saved image in inches. Default is 8.

dpi

Numeric. The resolution of the saved image in dots per inch. Default is 500.

Value

Invisibly returns the path of the saved file.

Invisibly returns the path of the saved file.

Details

This function uses `ggsave()` to save the Margot plot as a PNG image. If the save_path directory doesn't exist, it will be created. The final filename will be constructed as: `prefix_base_filename.png`.

This function handles different types of plot objects and saves them as PNG images: - For ggplot2 objects: Uses ggsave() - For other plot types: Uses png() and dev.off() The final filename will be constructed as: `prefix_base_filename.png`.

Examples

if (FALSE) { # \dontrun{
# Assuming you have already run margot_plot() or margot_plot_multi_arm()
plot_result <- margot_plot(your_data, your_options)

# Save the plot as PNG
margot_save_png(
  plot_result,
  prefix = "study1",
  base_filename = "treatment_effects",
  save_path = here::here("output", "plots")
)
} # }

if (FALSE) { # \dontrun{
# For a ggplot object
plot_result <- ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point()
margot_save_png(plot_result, prefix = "study1", base_filename = "scatter")

# For a base R plot
plot_result <- plot(mtcars$mpg, mtcars$wt)
margot_save_png(plot_result, prefix = "study1", base_filename = "scatter")

# For a list containing a plot
plot_list <- list(plot = ggplot(mtcars, aes(x = mpg, y = wt)) + geom_point())
margot_save_png(plot_list, prefix = "study1", base_filename = "scatter")
} # }