[R] Add points to subplots
Greg Snow
538280 at gmail.com
Tue Jun 17 18:00:44 CEST 2014
I am not familiar with the pamr.plotcv function, but in general if it
uses par(mfrow=c(2,1)) to set up for the multiple plots then your
problem with going back to the first plot is that you have lost the
other information (such as user coordinates) needed to add to the 1st
plot. You can see that with the following base graphics commands:
par(mfrow=c(2,1))
plot(runif(25),rnorm(25))
tmp <- par(no.readonly=TRUE)
hist(rnorm(1000))
par(mfg=c(1,1))
abline(h=0, col='red')
The horizontal line is at what would be 0 in the lower plot, not the
upper. Since I saved the graphics parameters I can do the correct
thing with a command like:
par(mfg=c(1,1), plt=tmp$plt, usr=tmp$usr)
abline(h=0, col='blue')
You can see the new line is in the correct place.
Looking at the help for pamr.plotcv it does not look like it has any
nice built-in ways to save the plotting parameters (some functions
would let you plot just the top, edit, then plot just the bottom).
Bot there is a hook to the plot.new function that can let us work
around this. Try the following:
mypars <- list()
updatepars <- function() {
n <- length( .GlobalEnv$mypars )
.GlobalEnv$mypars[[ n + 1 ]] <- par(no.readonly=TRUE)
}
setHook('before.plot.new', updatepars)
par(mfrow=c(2,1))
plot(runif(25),rnorm(25))
hist(rnorm(1000))
setHook('before.plot.new',NULL, 'replace' ) ## clean up
par( mfg=c(1,1), plt=mypars[[2]]$plt, usr=mypars[[2]]$usr )
abline( h=0, col='blue' )
This creates a global variable "mypars" that is an empty list (we
should really figure out a way without using the global, but my
current thoughts would make this much more complicated, any
suggestions are welcome). Then it creates a small function that will
add the current results of 'par' to that list. Then this function is
set as a hook to be run before 'plot.new' so that any new plot will
first save the previous parameter settings. Now we run the plotting
commands and use the parameters that were saved into 'mypars'. I
chose to save all the parameters from every old plot in case more than
what is shown is needed, this could be used to go back 3 or 4 plots if
more than just 2 are plotted.
Try this with pamr.plotcv to see if it works (you may need to set some
additional parameters depending on what all pamr.plotcv does).
Whether this is the easy solution or not can be debated. Hope it helps,
On Thu, Jun 12, 2014 at 9:06 AM, Luca Cerone <luca.cerone at gmail.com> wrote:
> Dear all,
> I am running some analysis using the pamr package (available on CRAN).
>
> One of the plots I produce is made using the function "pamr.plotcv".
> This displays two plots in the same figure (using par(mfrow=c(2,1)).
>
> When the figure is created, I would like to be able to add some points
> and lines, to the top plot.
>
> After producing the plot with pamr.cvplot, I have tried to add a line
> doing something like:
>
> par(mfg=c(1,1))
> lines(c(3,3), c(0,1), col = "blue", lty = 3)
>
> However this doesn't work and the line is shown in the bottom plot.
> How can I add points and lines to the top plot?
>
> Thanks a lot in advance for the help,
>
> Cheers,
> Luca
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com
More information about the R-help
mailing list