如何从包中执行专门绘图功能的图块

时间:2013-10-18 10:33:46

标签: r layout plot tiles par

我想将以下4个图作为par(mfrow=c(2,2))类型排列。

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

但是,基于layoutmfrow的尝试未成功。

我将给予正确答案赏金。

1 个答案:

答案 0 :(得分:1)

正如@plannapus评论的那样,函数plot.modwt已经调用了布局。所以你需要改变原来的功能。

  1. 如果您在R控制台中键入plot.modwt,您将获得完整的定义。
  2. 复制此功能并将其另存为新功能,例如my.plot.modwt
  3. 在此功能中注释掉布局线
  4. 设置新布局。这对我有用:

    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. 调用您的函数(4次)来创建绘图:

    my.plot.modwt(modwt(dat1))
    my.plot.modwt(modwt(dat2))
    my.plot.modwt(modwt(dat3))
    my.plot.modwt(modwt(dat4))
    
  6. 注意,可能需要对布局进行一些其他更改。


    我的代码:

    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>
    }