R_Lattice のバックアップソース(No.11) - アールメカブ

アールメカブ


R_Lattice のバックアップソース(No.11)

[[Rの備忘録]]



[[Lattice Multivariate Data Visualization with R:http://www.springer.com/statistics/computational/book/978-0-387-75968-5]] 翻訳メモ

#contents

* クラインの壺 [#y8ec15db]
#ref(lattice113.png);

 kx <- function(u, v)
      cos(u) * (r + cos(u/2) * sin(t*v) - sin(u/2) * sin(2*t*v))
 ky <- function(u, v) 
      sin(u) * (r + cos(u/2) * sin(t*v) - sin(u/2) * sin(2*t*v))
 kz <- function(u, v) 
      sin(u/2) * sin(t*v) + cos(u/2) * sin(t*v)
 n <- 50
 u <- seq(0.3, 1.25, length = n) * 2 * pi
 v <- seq(0, 1, length = n) * 2 * pi
 um <- matrix(u, length(u), length(u))
 vm <- matrix(v, length(v), length(v), byrow = TRUE)
 r <- 2
 t <- 1
 
 wireframe(kz(um, vm) ~ kx(um, vm) + ky(um, vm), 
     shade = TRUE, screen = list(z = 170, x = -60),
      alpha = 0.75, panel.aspect = 0.6, aspect = c(1, 0.4))

* 立体画像? [#t2fc24c4]

ページの向こうに焦点を合わせるようにして,二つの列を重ね合わせると,奥行きが表現された画像を実感することができる?

#ref(lattice97.png);

* cloud() 関数のデフォルト [#s52528df]
の viewpoint. screen 引数にリストで指定する.

 > cloud(iris[,1] ~ iris[,2] * iris[,3])
 > X11()
 > cloud(iris[,1] ~ iris[,2] * iris[,3],
     screen = list(z = 40, y = 0, x = -60),
      )

* hypervariate な平行座標プロット [#qcd62b9a]
#ref(lattice88.png);

ところで,この図を png 化した時に
 > data(gvhd10, package = "latticeExtra")
 >  png(file = "lattice88.png")
 > parallel(~ asinh(gvhd10[c(3, 2, 4, 1, 5)]), data = gvhd10, 
 +            subset = Days == "13", alpha = 0.01, lty = 1)
 >  dev.off()
 X11cairo 
       2 
X11cairo というメッセージを初めてみた.Cairo そのものは,アンチエイリアスを備えた二次元ベクトル・グラフィックスライブラリのことだが.


* バイオリンプロット. [#mc94f3e0]
言い得て妙である.日本風なら,ひょうたんプロットかな? テキスト p.49 

#ref(lattice49.png);

* 掲載の図が R で実現できない理由 [#r5a00e6b]
は,著者が dev.copy2eps() 関数ではなく postscript() 関数を使って作図しているからであった.postscript() 関数での作図パラメータは異なる. [#fa03bf9b]

 > ?trellis.device

- 原書 p.57真ん中 (パラグラフ4.1最後)の以下のコードを使ってウィンドウに表示されるグラフは Figure4.3 ではない.

 > dotplot(VADeaths, type = "o",
        pch =  1:4, col = 1:4, lty = 1:4,
          key = key.list,
          main = "Death Rates in Virginia - 1940",
          xlab = "Rate (per 1000)")

Figure4.3とまったく同じ図を作成するためには次のコードを実行する.

 > key.list <- list( space = "right",
               text =  list(colnames(VADeaths)) ,
               points = list(pch = c(1,3,6,0), col = 1:4),
               lines = list(lty = 1:4, col = 1:4))
 > dotplot(VADeaths,
        panel = function (...){
          panel.xyplot(...,  type = "o",
                   panel.grid = panel.grid(h=-1,v=0),
                   pch =  c(1,3,6,0), col = 1:4, lty = 1:4)
          },
          key = key.list,
          main = "Death Rates in Virginia - 1940",
          xlab = "Rate (per 1000)")



- テキスト p.41
コードをそのまま実行すると次のグラフが作成される.
#ref(lattice43.png);
 data(Chem97, package = "mlmRev")
 qqmath(~ gcsescore | gender, Chem97, 
         groups = score, aspect = "xy",  
         f.value = ppoints(100),
          auto.key = list(space = "right") ,
         xlab = "Standard Normal Quantiles", 
         ylab = "Average GCSE Score")

しかし,掲載されているのは次のような図(ただし白黒)
#ref(lattice43T.png);

 pch.col <- sort(unique(Chem97$score)) + 1 
 
 key.list <- list( space = "right",  
     text =  list(as.character(pch.col-1) ) ,  
      points = list(pch = pch.col, col = 1:6  ) )
 
 qqmath(~ gcsescore | gender, Chem97, 
         groups = score, aspect = "xy",  
         f.value = ppoints(100),  pch = pch.col ,
         col = 1:6,    key = key.list, 
         xlab = "Standard Normal Quantiles", 
         ylab = "Average GCSE Score")

[[Baayen>R_Baayen]] にも同じように,掲載コードとグラフが,R-2.8.1 で再現できない例がある.

 library(languageR)
 affixes.pr = prcomp(affixProductivity[,
    1:(ncol(affixProductivity)-3)])
 library(lattice)
 super.sym = trellis.par.get("superpose.symbol")
 splom(data.frame(affixes.pr$x[,1:3]), 
 groups = affixProductivity$Registers, 
 panel  = panel.superpose,
  key    = list(
  title  = "texts in productivity space",
  text   = list(c("Religious", "Children", 
  "Literary", "Other")),
 points = list(pch = super.sym$pch[1:4],
 col = super.sym$col[1:4])))
 dim(affixes.pr$rotation)
 affixes.pr$rotation[1:10, 1:3]   

/key リスト points をみると,super.sym$pch[1:4] という指定があり,
/これは記号をカテゴリごとに変更するつもりなのだろうが,デフォルトの/[[R_trellis.par.get]] の出力は
/ $pch
/ [1] 1 1 1 1 1 1 1
/なので,記号はすべて 1,つまり○に決まっている.
/ points = list(pch = super.sym$pch[1:4],
/の部分を
/ points = list(pch = 1:4
/とすれば,テキスト記載の図になるが,これは筆者のミスなのか,
/あるいは,Latticeで何か設定に変更が行われた結果なのか?
/latticeの以前の設定は,どうだったのだろうか,と思って,
/R-2.5.1のままになっているLinuxマシンで実行したら,
/結果は上と変わらん.