42-*_*42- 70
'lattice'包构建在网格包上,并在加载'lattice'时附加其命名空间.但是,为了使用该grid.layout
函数,您需要显式地使用load()
pkg :: grid.另一个可能更容易的选择是grid.arrange
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)
Run Code Online (Sandbox Code Playgroud)
dou*_*oug 42
该格子包往往(但并不总是)忽略相提并论命令,所以我只是避免绘制W /时使用它格.
要在单个页面上放置多个点阵图:
创建(但不积)格/网格情节的对象,然后
调用打印一次为每个情节
对于每个打印调用,传入(i)图的参数 ; (ii) more,设置为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))
Run Code Online (Sandbox Code Playgroud)
Wal*_*cio 10
一旦你阅读,这很容易做到?print.trellis
.特别感兴趣的是split
参数.乍一看似乎很复杂,但一旦你明白它意味着它就会非常简单.从文档:
split:一个4个整数的向量,c(x,y,nx,ny),表示将当前绘图定位在nx图的nx常规数组中的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
Run Code Online (Sandbox Code Playgroud)
如您所见,split
需要四个参数.在最后两个是指你的框架(类似于大小mfrow
一样),而前两个参数位置的积成nx
的ny
框架.