使用R中的lapply在网格上生成多个晶格图 [英] Generate multiple lattice plots on a grid using lapply in R
本文介绍了使用R中的lapply在网格上生成多个晶格图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将多个晶格图绘制到使用lapply函数生成图的单个晶格图上?
How do plot multiple lattice plots onto a single lattice plot where the plots are generated using an lapply function?
以下是到目前为止我使用内置的mtcars
数据集尝试过的示例.
The following is a demonstration of what I have tried so far using the built in mtcars
dataset.
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))
指令,并分别绘制每个图.
This produces the plots desired. However it seems to be ignoring the par(mfrow=c(2,2))
instruction and plotting each plot separately.
推荐答案
如果您真的不想使用格网的内置构面或视口选项,则可以使用以下命令复制par(mfrow)
的行为,
If you really don't want to use the built-in facetting or viewport options of lattice, you can replicate the behavior of par(mfrow)
with the following,
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))
这篇关于使用R中的lapply在网格上生成多个晶格图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文