Skip to contents

Saves a ggplot object. By default it saves the last plot to be modified or created.

Usage

sbf_save_plot(
  x = ggplot2::last_plot(),
  x_name = substitute(x),
  sub = sbf_get_sub(),
  main = sbf_get_main(),
  caption = "",
  report = TRUE,
  tag = "",
  notes = "",
  units = "in",
  width = NA,
  height = width,
  dpi = 300,
  limitsize = TRUE,
  csv = 1000L,
  drop_uninformative_cols = TRUE,
  proportion = FALSE,
  format_column = list(),
  round_as_estimate = character(0)
)

Arguments

x

The ggplot object to save.

x_name

A string of the name.

sub

A string or character vector specifying the path to the sub folder (by default the current sub folder).

main

A string specifying the path to the main folder (by default the current main folder)

caption

A string of the caption.

report

A flag specifying whether to include in a report.

tag

A string of the tag.

notes

A string of additional notes to save with the metadata.

units

A string of the units. Can be "in" (default) or "mm" or "cm".

width

A number of the plot width in inches.

height

A number of the plot width in inches.

dpi

A number of the resolution in dots per inch.

limitsize

When TRUE (the default), ggsave() will not save images larger than 50x50 inches, to prevent the common error of specifying dimensions in pixels.

csv

A count specifying the maximum number of rows to save as a csv file.

drop_uninformative_cols

A flag indicating whether to drop uninformative columns via tidyplus::drop_uninformative_columns() (TRUE; default) or not (FALSE). Currently soft-deprecated. Will be fully deprecated in future versions so that uninformative columns are always dropped in csv files and always kept in xlsx files.

proportion

A flag specifying whether the columns estimate, lower, upper and sd are proportions, in which case significant digits are counted from the nearer of 0 and 1 in the .md file.

format_column

A list of two-sided formulas of the form column ~ fun(.) specifying how to round and format numeric columns other than estimate, lower, upper and sd in the .md file. The function can return a numeric vector, which is formatted without further rounding, or a character vector, which is used as is. The values in the .rds and .csv files are not altered.

round_as_estimate

A character vector of numeric columns, other than estimate, lower, upper, sd and svalue, to round in the .md file to the same number of decimal places as estimate in the same row (e.g. an observed value compared with the estimate). Rows with a missing estimate are formatted as data. If proportion = TRUE the columns must also be proportions.

Details

The function saves:

  1. A png file of the plot.

  2. A yaml file of the plot metadata.

  3. An rds file of the plot.

  4. A csv of the data passed to ggplot(), provided it has at least one row and no more than csv rows. If x is a patchwork object, only the data for the first patch is saved, to maintain compatibility with previous versions.

  5. An xlsx workbook with a sheet for the data passed to each ggplot() call and a sheet for each geom layer. Sheets are labelled <p>_<l>_<geom>, where <p> is the patch index (1 for a simple ggplot), <l> is the layer index (0 for the ggplot() data), and <geom> is the layer type (e.g. point, line). Layers with more than csv rows are omitted.

  6. An md file of the same data as the csv with the values rounded and formatted for display as described in sbf_save_table(). The values in the plot are not rounded. The md file is not saved if getOption("sbf.save_md") is FALSE.

The csv, xlsx and md files are named using the x_name argument. Not specifying x overwrites existing files that used x_name = "plot".

Examples

if (FALSE) { # \dontrun{
require(ggplot2)
require(patchwork)
ggplot(mtcars) +
  geom_line(aes(mpg, cyl, color = cyl), mtcars) +
  ggtitle("1")
sbf_save_plot()

p_patches <-
  ((ggplot(mtcars) +
      geom_line(aes(mpg, cyl, color = cyl), mtcars) +
      ggtitle("1")) +
     (ggplot() +
        geom_line(aes(Sepal.Length, Petal.Length), iris) +
        ggtitle("2"))) /
  ((ggplot() +
      geom_point(aes(mpg, cyl, color = cyl), mtcars) +
      ggtitle("3")) +
     ((ggplot(iris) +
         geom_point(aes(Sepal.Length, Petal.Length)) +
         ggtitle("4")) +
        (ggplot() +
           geom_point(aes(Sepal.Length, Petal.Length, color = Species),
                      iris) +
           ggtitle("5") +
           theme(legend.position = "none"))))
p_patches
sbf_save_plot(p_patches)
} # }