Question

I want to plot 4 of the following plots as an par(mfrow=c(2,2)) type arrangement.

install.packages("wavelets")
require(wavelets)
dat <- rnorm(100)
plot.modwt(modwt(dat)) #4 of these in a 2x2 grid is desired

However, layout and mfrow based attempts have not succeeded.

I will be giving the correct answer a bounty.

Was it helpful?

Solution

As @plannapus commented, the function plot.modwt already calls layout. So you will need to alter the original function.

  1. If you type plot.modwt in you R console, you will get the complete definition.
  2. Copy this function and save it as a new function, say, my.plot.modwt.
  3. Comment out the layout line in this function
  4. Set up your new layout. This worked for me:

    nf = layout(matrix(c(3, 1, 4, 2, 7, 5,8, 6), 4, 2, byrow = TRUE), 
         c(2,2), c(2,1, 2, 1), TRUE)
    layout.show(nf)
    
  5. Call your function (4 times )to create plots:

    my.plot.modwt(modwt(dat1))
    my.plot.modwt(modwt(dat2))
    my.plot.modwt(modwt(dat3))
    my.plot.modwt(modwt(dat4))
    

Note, some other alterations to the layout will probably be needed.


My code:

y.plot.modwt = function (x, levels = NULL, draw.boundary = FALSE, type = "stack", 
          col.plot = "black", col.boundary = "red", X.xtick.at = NULL, 
          X.ytick.at = NULL, Stack.xtick.at = NULL, Stack.ytick.at = NULL, 
          X.xlab = "t", y.rlabs = TRUE, plot.X = TRUE, plot.W = TRUE, 
          plot.V = TRUE, ...) 
{
  stackplot.modwt <- function(x, w.range, v.range, col.plot, 
                              col.boundary, draw.boundary, X.xtick.at, X.ytick.at, 
                              Stack.xtick.at, Stack.ytick.at, X.xlab = "t", plot.X = TRUE) {
    innerplot <- function(x, y, type = "l", xtick.at, ytick.at) {
      if (is.null(xtick.at) == FALSE || is.null(ytick.at) == 
            FALSE) {
        plot(x, y, type = "l", axes = FALSE, frame.plot = TRUE)
        <snip>

if (plot.X) {
      #nf <- layout(matrix(c(2, 2, 1, 1), 2, 2, byrow = TRUE), 
      #             c(1, 2), c(2, 1), TRUE)
      par(mai = c(0.6, 0.4, 0.1, 0.6))

<snip>
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top