user2015601
user2015601

Reputation:

Adding plotting parameter to a temporary variable in R

Is it possible to assign a plotting function with its arguments to a variable and use it as template?

for example, if I have something like

tmp_plot <- plot(my_data, xlabel="x", ylabel="y", ...)

could I add another argument, for example, main="caption" to replot the plot without typing the whole function again?

Upvotes: 1

Views: 120

Answers (2)

IRTFM
IRTFM

Reputation: 263332

I'm wondering if you are looking for the title function? It will add new text to an existing plot that is sitting on the interactive graphics device. There is a dev.copy function that attempts to make a new graph which can further be altered by title or points or any of the other base graphics functions that allow additions. See also a savePlot function for X11() devices. The problem with the code you offer is that most "plot" functions in the base-graphics paradigm will return NULL, so that temp_plot would be ... nothing. The possible exception are S4 plot functions that actually use lattice or ggplot2 (see below).

Contrariwise, if you are trying to save a plot as an R structure then you need to look at the lattice and ggplot2 plotting functions which do that by storing the plot data and structure in a list object.

Upvotes: 3

mnel
mnel

Reputation: 115392

Create a function that is a wrapper for plot with main = 'caption'

mplot <- function(...) plot(..., main = 'caption')
mplot(1:10,1:10)

enter image description here

Upvotes: 3

Related Questions