格子:一个窗口中的多个图?

发布于 2024-08-27 00:02:33 字数 147 浏览 7 评论 0原文

我试图通过设置 par(mfrow=c(2,1)) 使用 levelplot 在一个窗口中放置多个格子图,但它似乎忽略了这一点。

是否有一个特定的函数可以在lattice中设置多个绘图?

I'm trying to put multiple lattice plots in one window using levelplot by setting par(mfrow=c(2,1)) but it seems to be ignoring this.

Is there a particular function for setting multiple plots in lattice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

偏爱你一生 2024-09-03 00:02:33

“lattice”包构建在 grid 包之上,并在加载“lattice”时附加其名称空间。但是,为了使用grid.layout函数,您需要显式load() pkg::grid。另一种选择可能更简单,是 pkg::gridExtra 中的 grid.arrange 函数:

 install.packages("gridExtra")
 require(gridExtra) # also loads grid
 require(lattice)
 x <- seq(pi/4, 5 * pi, length.out = 100)
 y <- seq(pi/4, 5 * pi, length.out = 100)
 r <- as.vector(sqrt(outer(x^2, y^2, "+")))

 grid <- expand.grid(x=x, y=y)
 grid$z <- cos(r^2) * exp(-r/(pi^3))
 plot1 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
           ylab="", main="Weird Function", sub="with log scales",
           colorkey = FALSE, region = TRUE)

 plot2 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
           ylab="", main="Weird Function", sub="with log scales",
           colorkey = FALSE, region = TRUE)
 grid.arrange(plot1,plot2, ncol=2)

在此输入图像描述

The 'lattice' package is built on the grid package and attaches its namespace when 'lattice' loaded. However, in order to use the grid.layout function, you need to explicitly load() pkg::grid. The other alternative, that is probably easier, is the grid.arrange function in pkg::gridExtra:

 install.packages("gridExtra")
 require(gridExtra) # also loads grid
 require(lattice)
 x <- seq(pi/4, 5 * pi, length.out = 100)
 y <- seq(pi/4, 5 * pi, length.out = 100)
 r <- as.vector(sqrt(outer(x^2, y^2, "+")))

 grid <- expand.grid(x=x, y=y)
 grid$z <- cos(r^2) * exp(-r/(pi^3))
 plot1 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
           ylab="", main="Weird Function", sub="with log scales",
           colorkey = FALSE, region = TRUE)

 plot2 <- levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
           ylab="", main="Weird Function", sub="with log scales",
           colorkey = FALSE, region = TRUE)
 grid.arrange(plot1,plot2, ncol=2)

enter image description here

冷了相思 2024-09-03 00:02:33

Lattice 包经常(但并非总是)忽略 par 命令,因此我只是在使用 Lattice 进行绘图时避免使用它。

要在单个页面上放置多个晶格图:

  • 创建(但不绘制)晶格/网格绘图对象,然后

  • 为每个图调用 print 一次

  • 对于每个 print 调用,传入 (i) 的参数; (二)
    更多,设置为TRUE,并且仅在初始调用print时传入,并且(iii ) pos,给出页面上每个图的位置,指定为图左下角和右上角的 xy 坐标对
    分别是角点,即具有四个数字的向量。

展示比讲述容易得多:

data(AirPassengers)     # a dataset supplied with base R
AP = AirPassengers      # re-bind to save some typing

# split the AP data set into two pieces 
# so that we have unique data for each of the two plots
w1 = window(AP, start=c(1949, 1), end=c(1952, 1))
w2 = window(AP, start=c(1952, 1), end=c(1960, 12))

px1 = xyplot(w1)
px2 = xyplot(w2)

# arrange the two plots vertically
print(px1, position=c(0, .6, 1, 1), more=TRUE)
print(px2, position=c(0, 0, 1, .4))

The Lattice Package often (but not always) ignores the par command, so i just avoid using it when plotting w/ Lattice.

To place multiple lattice plots on a single page:

  • create (but don't plot) the lattice/trellis plot objects, then

  • call print once for each plot

  • for each print call, pass in arguments for (i) the plot; (ii)
    more, set to TRUE, and which is only passed in for the initial call to print, and (iii) pos, which gives the position of each plot on the page specified as x-y coordinate pairs for the plot's lower left-hand corner and upper right-hand
    corner, respectively--ie, a vector with four numbers.

much easier to show than to tell:

data(AirPassengers)     # a dataset supplied with base R
AP = AirPassengers      # re-bind to save some typing

# split the AP data set into two pieces 
# so that we have unique data for each of the two plots
w1 = window(AP, start=c(1949, 1), end=c(1952, 1))
w2 = window(AP, start=c(1952, 1), end=c(1960, 12))

px1 = xyplot(w1)
px2 = xyplot(w2)

# arrange the two plots vertically
print(px1, position=c(0, .6, 1, 1), more=TRUE)
print(px2, position=c(0, 0, 1, .4))
海风掠过北极光 2024-09-03 00:02:33

一旦您阅读了?print.trellis,这一切就很简单了。特别令人感兴趣的是 split 参数。乍一看似乎很复杂,但一旦你理解了它的含义,它就非常简单了。从文档中:

split:一个由 4 个整数组成的向量 c(x,y,nx,ny),表示将当前图定位在 nx x ny 图的常规数组中的 x,y 位置。 (注意:原点位于左上角)

您可以在 example(print.trellis) 上看到几个实现,但这是我更喜欢的一个:

library(lattice)

# Data
w <- as.matrix(dist(Loblolly))
x <- as.matrix(dist(HairEyeColor))
y <- as.matrix(dist(rock))
z <- as.matrix(dist(women))

# Plot assignments
pw <- levelplot(w, scales = list(draw = FALSE))  # "scales..." removes axes
px <- levelplot(x, scales = list(draw = FALSE))
py <- levelplot(y, scales = list(draw = FALSE))
pz <- levelplot(z, scales = list(draw = FALSE))

# Plot prints
print(pw, split = c(1, 1, 2, 2), more = TRUE)
print(px, split = c(2, 1, 2, 2), more = TRUE)
print(py, split = c(1, 2, 2, 2), more = TRUE)
print(pz, split = c(2, 2, 2, 2), more = FALSE)  # more = FALSE is redundant

上面的代码为您提供了下图:
levelplots

如您所见,split 采用四个参数。 最后两个指的是框架的大小(类似于mfrow所做的),而前两个参数将您的绘图定位到< code>nx 到 ny 框架。

This is simple to do once you read ?print.trellis. Of particular interest is the split parameter. It may seem complicated at first sight, but it's quite straightforward once you understand what it means. From the documentation:

split: a vector of 4 integers, c(x,y,nx,ny), that says to position the current plot at the x,y position in a regular array of nx by ny plots. (Note: this has origin at top left)

You can see a couple of implementations on example(print.trellis), but here's one that I prefer:

library(lattice)

# Data
w <- as.matrix(dist(Loblolly))
x <- as.matrix(dist(HairEyeColor))
y <- as.matrix(dist(rock))
z <- as.matrix(dist(women))

# Plot assignments
pw <- levelplot(w, scales = list(draw = FALSE))  # "scales..." removes axes
px <- levelplot(x, scales = list(draw = FALSE))
py <- levelplot(y, scales = list(draw = FALSE))
pz <- levelplot(z, scales = list(draw = FALSE))

# Plot prints
print(pw, split = c(1, 1, 2, 2), more = TRUE)
print(px, split = c(2, 1, 2, 2), more = TRUE)
print(py, split = c(1, 2, 2, 2), more = TRUE)
print(pz, split = c(2, 2, 2, 2), more = FALSE)  # more = FALSE is redundant

The code above gives you this figure:
levelplots

As you can see, split takes four parameters. The last two refer to the size of your frame (similar to what mfrow does), whereas the first two parameters position your plot into the nx by ny frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文