web-dev-qa-db-ja.com

ラティス:1つのウィンドウに複数のプロットがありますか?

par(mfrow=c(2,1))を設定することにより、levelplotを使用して1つのウィンドウに複数の格子プロットを配置しようとしていますが、これを無視しているようです。

latticeに複数のプロットを設定するための特定の機能はありますか?

62
Chris

「lattice」パッケージはグリッドパッケージ上に構築され、「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)
_

enter image description here

68
42-

Latticeパッケージは、多くの場合(常にではないが)parコマンドを無視するため、Latticeをプロットするときは使用しないようにします。

単一のページに複数のラティスプロットを配置するには:

  • 作成(ただし、プロットしない)ラティス/トレリスプロットオブジェクト、その後

  • printを各プロットに対して1回呼び出す

  • printの呼び出しごとに、(i)plot;の引数を渡します。 (ii)more[〜#〜] true [〜#〜]に設定され、渡されるのみprint、および(iii)posの最初の呼び出しでは、ページ上の各プロットの位置を示しますプロットの左下隅と右上隅のxy座標のペアとしてそれぞれ指定されます。つまり、4つの数値を持つベクトルです。

伝えるよりも表示する方がはるかに簡単です:

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))
42
doug

_?print.trellis_を読んだら、これは簡単です。特に興味深いのは、splitパラメーターです。一見複雑に見えるかもしれませんが、その意味を理解すれば非常に簡単です。ドキュメントから:

split:4つの整数のベクトルc(x、y、nx、ny)。現在のプロットをnx x nyプロットの通常の配列のx、y位置に配置します。 (注:左上にOriginがあります)

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は4つのパラメーターを取ります。 最後の2つはフレームのサイズを参照します(mfrowが行うことと同様)、一方最初の2つのパラメータは、プロットをnx by nyフレームに配置します。

10
Waldir Leoncio