Tuesday, February 14, 2017

Overlay a new plot on an existing plot


plot.overlay <- function(..., label="2nd Y", col="blue")
{ 
    ## Purpose: Overlay a new plot on an existing plot. 
    ## Arguments:
    ##   ...: Passed to the "plot" function for the new plot.
    ##   label: The secondary Y-axis label.
    ##   col: The color of the ticks and tick labels for the new plot. 
    ## Return: A modified plot. 
    ## Author: Feiming Chen, Date:  6 Feb 2017, 10:19
    ## ________________________________________________

    par(new=T)
    plot(..., axes=F, xlab="", ylab="", col=col)
    axis(side=4, col.ticks = col, col.axis=col)
    mtext(label, side=4, line=-1, col=col)
}
if (F) {                                # Unit Test
    plot(rnorm(100))
    plot.overlay(rnorm(100), type="l", label="Test")
}


No comments:

Post a Comment