【问题标题】:Lines dividing graphs using layout() in R在 R 中使用 layout() 划分图形的线
【发布时间】:2014-07-09 20:01:19
【问题描述】:

我在 R 中使用 layout() 来生成一系列 9 个图形(在 3x3 布局中)

    m <- rbind(c(1,2,3), c(4,5,6), c(7,8,9))
    layout(m)

我试图在三列之间放置 2 条垂直线,以便将各列彼此分开。 box() 不合适,因为我希望将行链接起来,因此不希望有行,我正在努力寻找使用 line() 函数的任何帮助,任何想法都将不胜感激。

谢谢

【问题讨论】:

    标签: r layout plot line margin


    【解决方案1】:

    如果您更改图形参数以将所有绘图剪切到设备区域,然后使用abline() 添加垂直线以进行分隔怎么办?例如:

    m <- rbind(c(1,2,3), c(4,5,6), c(7,8,9))
    
    # Clips drawing to the device region
    # See ?par for more details of the argument
    par(xpd=NA)
    
    layout(m)
    
    # Insert you nine plots here
    for(i in 1:9) {
       plot(1,1)
    }
    
    # Check the correct coordinates with
    # locator(), and the arguments
    # accordingly. These are about right,
    # if the plot region is rectangular.
    abline(v=0.25)
    abline(v=-1.06)
    

    结果图应如下所示。

    【讨论】:

    • 这几乎正是我所需要的,但是,我的每个地块都使用 abline() 将线条添加到各个地块。当我使用这个解决方案时,所有 abline 的范围都是“设备”的全长......另外,我已经为每个单独的图表设置了 par(),并且这个 par(xpd=NA) 覆盖了......否则,你上面画的正是我需要的。
    • 对不起,我才弄明白!!非常感谢您的回复。我只需要在 par() 去哪里玩!!
    【解决方案2】:

    另一种选择是与layout一起玩:

    layout(rbind(c(1,10, 2,11,3),
                 c(4,10, 5,11,6),
                 c(7,10, 8,11,9)),
    widths=c(1, lcm(3), 1,lcm(3),1),
    respect=TRUE)
    
    ## you plot your 9 plots  
    for(i in 1:9) {
      plot(1,1)
    }
    ## you plot the separation 
    plot(1,1,type='n',axes=FALSE, ann=FALSE)
    ## here I use a vertical line but you can plot anything (box, segments,..)
    abline(v=1)
    
    plot(1,1,type='n',axes=FALSE, ann=FALSE)
    abline(v=1)
    

    【讨论】:

      【解决方案3】:

      grconvertX 是@JTT 建议locator 查找行位置的替代方案:

      m <- rbind(c(1,2,3), c(4,5,6), c(7,8,9))
      layout(m)
      for(i in 1:9) {
         plot(1,1)
      }
      abline(v=grconvertX(1:2/3, 'ndc', 'user'))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-07-22
        • 1970-01-01
        • 1970-01-01
        • 2019-08-22
        • 1970-01-01
        • 1970-01-01
        • 2019-08-08
        相关资源
        最近更新 更多