Learn R Programming

ggsem (version 0.9.9)

save_figure: Save a ggplot Object with Adjusted Dimensions

Description

This function saves a ggplot object (created from 'csv_to_ggplot()' function) to a file with dimensions automatically determined based on the x-axis and y-axis ranges of the plot. The size of the output can be further controlled using addtional arguments.

Usage

save_figure(
  filename,
  plot,
  units = "in",
  dpi = 300,
  aspect_ratio = NULL,
  scale_factor = 0.122,
  zoom_level = 1,
  ...
)

Value

Saves the ggplot object to the specified file and does not return a value.

Arguments

filename

A string. The name of the output file (e.g., "plot.png").

plot

A ggplot object to save.

units

A string. Units for width and height. Default is "in" (inches). Other options include "cm" or "mm".

dpi

Numeric. Resolution of the output file in dots per inch. Default is 300.

aspect_ratio

Numeric or NULL. If provided, fixes the aspect ratio of the plot (e.g., 1 for square). If NULL, uses the natural data aspect ratio. Default is NULL.

scale_factor

Numeric. A scaling factor to control the overall size of the saved plot. Default is 0.122.

zoom_level

Numeric. A zoom factor, value > 1 means zoom out. Default is 1.0.

...

Additional arguments passed to ggsave().

Examples

Run this code
if (FALSE) {
# CSV files from ggsem app
points_data <- data.frame(
x = 20, y = 20, shape = 'rectangle', color = '#D0C5ED', size = 50,
border_color = '#9646D4', border_width = 2, alpha = 1,
width_height_ratio = 1.6, orientation = 45, lavaan = FALSE,
network = FALSE, locked = FALSE, group = 1
)

lines_data <- data.frame(
x_start = 2, y_start = -2, x_end = 10, y_end = -2, ctrl_x = NA, ctrl_y = NA,
ctrl_x2 = NA, ctrl_y2 = NA, curvature_magnitude = NA, rotate_curvature = NA,
curvature_asymmetry = NA, type = 'Straight Line', color = '#000000',
end_color = NA, color_type = 'Single',
gradient_position = NA, width = 1.5, alpha = 1, arrow = FALSE,
arrow_type = NA, arrow_size = NA, two_way = FALSE, lavaan = FALSE,
network = FALSE, line_style = 'solid', locked = FALSE, group = 1
)

p1 <- csv_to_ggplot(graphics_data = list(points_data, lines_data),
              zoom_level = 1.2, # Value from the ggsem app
              horizontal_position = 0, # Value from the ggsem app
              element_order = c('lines', 'points')) # order priority: lines < points

# Save with default scaling
save_figure("p1.png", p1)
}

Run the code above in your browser using DataLab