首页 文章

使用R中的lapply在网格上生成多个点阵图

提问于
浏览
0

如何将多个晶格图绘制到单个晶格图上,其中使用lapply函数生成图?

以下是我使用内置的 mtcars 数据集到目前为止所尝试的内容的演示 .

require(lattice)

response <- c("cyl","disp","hp","drat")

par(mfrow=c(2,2))

lapply(response, function(variable) {
  print(xyplot(mtcars$mpg ~ mtcars[variable]))
})

这产生了所需的图 . 然而,似乎忽略了 par(mfrow=c(2,2)) 指令并分别绘制每个图 .

3 回答

  • 1

    如果您确实不想使用晶格的内置构面或视口选项,则可以使用以下内容复制 par(mfrow) 的行为,

    require(lattice)
    
    response <- c("cyl","disp","hp","drat")
    
    # save all plots in a list
    pl <- lapply(response, function(variable) {
      xyplot(mtcars$mpg ~ mtcars[variable])
    })
    
    library(gridExtra)
    # arrange them in a 2x2 grid
    do.call(grid.arrange, c(pl, nrow=2))
    
  • 0

    您的示例不是如何使用 latticegrid 会更合适) .

    这是一个 lattice 解决方案:

    xyplot(mpg ~ cyl+disp+hp+drat,
           data=mtcars,
           groups=cyl+disp+hp+drat,
           scales=list(relation="free"),
           col="blue"
    )
    

    enter image description here

  • 3

    this page上的多重绘图功能是我多次使用以在一个页面上获取多个绘图对象的东西 .

相关问题