write_plot: Function to save plot figures

View source: R/write_ggplot.R

write_plotR Documentation

Function to save plot figures

Description

One may not always use ggplot2 to draw plot, base plot function for example, this function is particularly useful in that situation.

Usage

write_plot(
  plot_fn = plot,
  number = cctu_env$number,
  width = 29.7 * 0.6,
  height = 21 * 0.6,
  dpi = 300,
  units = "cm",
  clean_up = TRUE,
  directory = file.path("Output", "Figures"),
  format = c("png", "postscript", "jpeg"),
  graphics_args = NULL,
  verbose = options()$verbose,
  footnote = NULL,
  plot_args = NULL
)

Arguments

plot_fn

function to draw a plot.

number

the number used to as a suffix in the output filename, and to link to TableofTables. Default is to use the value in the cctu_env package environment that is set within attach_pop.

width

the width to save as

height

the height to save as

dpi

the resolution setting

units

either "cm" (the default) or "inches"

clean_up

logical to invoke the clean_up function at the end. Defaults to TRUE

directory

where to save the figures within path or current working directory

format

either "jpg", "postscript", or "png" to determine the file type to use

graphics_args

a list of named arguments to supply to graphics function (png, postscript, jpeg)

verbose

logical to print information on changes to the global environment or external files. Defaults to options()$verbose.

footnote

character vector, can be used to add footnotes.

plot_args

A named list arguments for plot_fn.

Details

The plot_fn can be a user defined function to draw plot. All parameters should be passed with names. Checkout the examples below.

Value

writes a copy of a plot to file fig_number.. edits the TableofTables object with the calling programe No return object.

See Also

write_table write_ggplot

Examples

## Not run: 
# Below is a simple example.
write_plot(plot_fn = plot, plot_args = list(x = iris[,1], y = iris[,2]))
# This is equivalent drawing the following plot and save it
plot(x = iris[,1], y = iris[,2])

# Below is user defined function plotting
# One can use this method to draw a complicated plot
new_plot <- function(x, y, h, v) {
   par(pty = "s", cex = 0.7) # adjust plot style
   plot(x, y)
   abline(h = h,v = v, lty=2) # add some lines
}
write_plot(plot_fn = new_plot, plot_args = list(x = iris[,1], y = iris[,2], h = 2.5, v = 6.0))

# To draw a KM-plot from survminer package
library("survival")
library("survminer")
fit<- survfit(Surv(time, status) ~ sex, data = lung)
# Drawing survival curves
p <- ggsurvplot(fit, data = lung)
write_plot(plot_fn = survminer:::print.ggsurvplot, plot_args = list(x = p))
# The code above works because the p is a ggsurvplot object (check it with class(p))
# There's a printing function print.ggsurvplot to handle the printing of the KM-plot.
# But this function is not exported by survminer, so we need to use three colons.

## End(Not run)

shug0131/cctu documentation built on Nov. 10, 2023, 12:03 p.m.