write_plot | R Documentation |
One may not always use ggplot2
to draw plot, base plot
function for example,
this function is particularly useful in that situation.
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(getOption("cctu_output", default = "Output"), "Figures"),
format = c("png", "postscript", "jpeg"),
graphics_args = NULL,
verbose = options()$verbose,
footnote = NULL,
plot_args = NULL
)
... |
Arguments to be passed to the ploting function |
plot_fn |
function to draw a plot. This can be simple |
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 |
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 |
directory |
where to save the figures within path or current working directory. The Output directory can be over-riden with options("cctu_output"). |
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 |
Deprecated, kept here for backcompatibility. A named list
arguments for |
The plot_fn
can be a user defined function to draw plot. Let us
assume the object p
is your plot object. If you can see the plot by
simply typing p
on the console, then this should be print
.
If you can see the plot by simply typing plot(p)
on the console,
then this should be plot
.
All parameters should be passed with names. Checkout the examples below.
writes a copy of a plot to file fig_number.. edits the TableofTables object with the calling programe No return object.
write_table
write_ggplot
## 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(
x = iris[, 1], y = iris[, 2], h = 2.5, v = 6.0,
plot_fn = new_plot
)
####################################################
# 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(p, plot_fn = survminer:::print.ggsurvplot)
# 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.
#####################################
# Draw a consort diagram ===========#
#####################################
library(grid)
# Might want to change some settings
txt0 <- c("Study 1 (n=160)", "Study 2 (n=140)")
txt1 <- "Population (n=300)"
txt1_side <- "Excluded (n=15):\n\u2022 MRI not collected (n=15)"
# supports pipeline operator
g <- add_box(txt = txt0) |>
add_box(txt = txt1) |>
add_side_box(txt = txt1_side) |>
add_box(txt = "Randomized (n=200)")
# Since you can draw the plot g with plot(g), the ploting function is plot
# The plotting function is \code{plot.consort}, so simple plot or plot.consort works
write_plot(g, plot_fn = plot)
# Or just
write_plot(g)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.