Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeat plot command with minor changes in R

Tags:

plot

r

gnuplot

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.

In gnuplot I would use the replot command.

plot ...
set title "The same plot with logarithmic axes"
set logscale
replot

Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.

Thanks for your help,

Sven

like image 921
R_User Avatar asked Jun 17 '11 17:06

R_User


People also ask

What does Coplot do in R?

The coplot() function plots two variables but each plot is conditioned ( | ) by a third variable. This third variable can be either numeric or a factor.

What is Cex in plot R?

cex. A numerical value giving the amount by which plotting text and symbols should be magnified relative to the default.

How do you add points to a graph in R?

To add new points to an existing plot, use the points() function. The points function has many similar arguments to the plot() function, like x (for the x-coordinates), y (for the y-coordinates), and parameters like col (border color), cex (point size), and pch (symbol type).


1 Answers

R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.

Depending on what you are really doing there are two options:

  1. If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
  2. If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.

Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

like image 105
Gavin Simpson Avatar answered Oct 22 '22 13:10

Gavin Simpson