COLOR(red){SIZE(20){グラフィックス参考実例集:ラティスグラフィックス}}

([[グラフィックス参考実例集]]に戻る。[[Rのグラフィックスパラメータ]]を参照する。)~

アドオンパッケージ COLOR(red){lattice} は S 用に開発されたグラフィックスパラダイムの R への移植であり、R の標準的な高水準関数の代替関数が用意されている。より細かい表現が可能になる。library(help=lattice) で関数一覧が得られる。総合的案内は ?Lattice で得られる。なお lattice パッケージは grid パッケージに依存しているので一緒にインストールしておく必要がある。場合によれば library(grid) を同時に実行する。もしかすると R 1.8 以上でないと動かないかも。lattice 関数は lightgrey の背景色等幾つかの固有の既定値を採用しているので、必要に応じ par 関数で変更する。

#contents
~

//*オブジェクト一覧
//
//| オブジェクト名 | 説明                                                                        //|
//| banking           |           Banking                                                        //| 
//| cloud               |       3d Scatter Plot                                                  //| 
//| draw.colorkey    |        Produce a Colorkey for levelplot| 
//| draw.key          |       Produce a Legend or Key| 
//| histogram        |        Histograms and Kernel Density Plots| 
//| latticeParseFormula   |   Parse Trellis formula| 
//| Lattice          |        Lattice Graphics| 
//| levelplot       |         Level Plots| 
//| llines            |       Lattice Replacements of base graphics  //functions| 
//| lset              |       Simple Interfaces to modify Trellis Settings| 
//| oneway             |      Fit One-way Model| 
//| panel.barchart  |         Default Panel Function for barchart| 
//| panel.bwplot      |       Default Panel Function for bwplot| 
//| panel.cloud         |     Default Panel Function for cloud| 
//| panel.densityplot |       Default Panel Function for densityplot| 
//| panel.dotplot     |       Default Panel Function for dotplot| 
//| panel.functions   |       Useful Panel Functions| 
//| panel.histogram  |        Default Panel Function for histogram| 
//| panel.levelplot    |      Default Panel Function for levelplot| 
//| panel.pairs       |       Default Superpanel Function for splom| 
//| panel.parallel   |        Default Panel Function for parallel| 
//| panel.qqmathline  |       Useful panel function with qqmath| 
//| panel.stripplot    |      Default Panel Function for stripplot| 
//| panel.superpose  |        Panel Function for Display Marked by groups| 
//| panel.tmd          |      Panel function for tmd| 
//| panel.xyplot      |       Default Panel Function for xyplot| 
//| prepanel.functions      | Useful Prepanel Function for Lattice| 
//| print.trellis           | Print Trellis Objects| 
//| qqmath                  | Q-Q Plot with Theoretical Distribution| 
//| qq                      | Quantile-Quantile Plots of Two Samples| 
//| rfs                     | Residual and Fit Spread Plots| 
//| Rows                |     Extract 'rows' from a list| 
//| shingles             |    shingles| 
//| simpleKey          |      Function to generate a simple key| 
//|  splom               |     Scatter Plot Matrices| 
//| strip.default      |   Default Trellis strip function| 
//| tmd                    |  Tukey Mean-Difference Plot| 
//| trellis.datasets   |      Data Sets in the Lattice library | 
//| trellis.device     |      Initializing Trellis Displays | 
//| trellis.object      |     Lattice Display Object | 
//| trellis.par.get     |     Graphical Parameters for Trellis Displays |
//| update.trellis    |       Update Trellis Object | 
//| xyplot              |     Common Bivariate Trellis Plots | 

*COLOR(red){wireframe} 関数

 wireframe1 <- function() {
   library(lattice)
   x <- seq(-pi, pi, len = 20)
   y <- seq(-pi, pi, len = 20)
   g <- expand.grid(x = x, y = y)
   g$z <- sin(sqrt(g$x^2 + g$y^2))
   wireframe(z ~ x * y, g, drape = TRUE,
             perspective = FALSE,
             aspect = c(3,1), colorkey = FALSE)
    pp <- recordPlot(); png("lattice.wireframe1.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.wireframe1.png,left)

 wireframe2 <- function() {
   library(lattice)
   g <- expand.grid(x = 1:10, y = 5:15, gr = 1:2)
   g$z <- log((g$x^g$g + g$y^2) * g$gr)
   wireframe(z ~ x * y, data = g, groups = gr,
             scales = list(arrows = FALSE),
             shade = TRUE,
             shade.colors = function(cosangle, height)
             palette.shade(cosangle, height = .15, saturation = .05),
             light.source = c(0, 0, 1),
             screen = list(z = 30, x = -60))
    pp <- recordPlot(); png("lattice.wireframe2.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.wireframe2.png,left)

*COLOR(red){cloud} 関数(三次元散布図)

 cloud1 <- function() {
   library(lattice)
   data(iris)
   cloud(Sepal.Length ~ Petal.Length * Petal.Width, data = iris,
         groups = Species, screen = list(x = -90, y = 70),
         aspect = c(1, 1), distance = .4, zoom = .6,
         key = list(title = "Iris Data", x = .1, y=.9,
           corner = c(0,1),
           border = TRUE,
           points = Rows(trellis.par.get("superpose.symbol"), 1:3),
           text = list(levels(iris$Species))))
    pp <- recordPlot(); png("lattice.cloud1.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.cloud1.png,left)

*COLOR(red){histogram} 関数

 histogram1 <- function() {
   library(lattice)
   data(singer)
   histogram( ~ height | voice.part, data = singer, nint = 17,
             endpoints = c(59.5, 76.5), layout = c(2,4), aspect = 1,
             xlab = "Height (inches)")
    pp <- recordPlot(); png("lattice.histogram1.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.histogram1.png,left)

 ## この例は  S-Plus では表示できないかも知れない
 histogram2 <- function() {
   library(lattice)
   data(singer)
   histogram( ~ height | voice.part, data = singer,
             xlab = "Height (inches)", type = "density",
             panel = function(x, ...) {
             panel.histogram(x, ...)
             panel.mathdensity(dmath = dnorm,
                                 args = list(mean=mean(x),sd=sd(x)))
            } )
   pp <- recordPlot(); png("lattice.histogram2.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.histogram2.png,left)

*COLOR(red){densityplot} 関数
 densityplot3 <- function() {
   library(lattice)
   data(singer)
   densityplot( ~ height | voice.part, data = singer, layout = c(2, 4),
               xlab = "Height (inches)", bw = 5)
   pp <- recordPlot(); png("lattice.densityplot1.png"); replayPlot(pp); dev.off()
 }
#ref(lattice.histogram3.png,left)

*COLOR(red){levelplot} 関数

 levelplot1 <- function() {
   library(lattice)
   x <- seq(pi/4, 5*pi, length = 100)
   y <- seq(pi/4, 5*pi, length = 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))
   levelplot(z~x*y, grid, cuts = 50, xlab="", ylab="",
             main="Weird Function", colorkey = FALSE)
 }
#ref(lattice.levelplot1.png,left)

*COLOR(red){contourplot} 関数

 contourplot1 <- function() { #S+ example
   library(modreg)
   library(lattice)
   data(environmental)
   attach(environmental)
   ozo.m <- loess((ozone^(1/3)) ~ wind * temperature * radiation,
                  parametric = c("radiation", "wind"), span = 1, degree = 2)
   w.marginal <- seq(min(wind), max(wind), length = 50)
   t.marginal <- seq(min(temperature), max(temperature), length = 50)
   r.marginal <- seq(min(radiation), max(radiation), length = 4)
   wtr.marginal <- list(wind = w.marginal, temperature = t.marginal,
                        radiation = r.marginal)
   grid <- expand.grid(wtr.marginal)
   grid[, "fit"] <- c(predict(ozo.m, grid))
   contourplot(fit ~ wind * temperature | radiation, data = grid,
               cuts = 10, region = TRUE,
               xlab = "Wind Speed (mph)",
               ylab = "Temperature (F)",
               main = "Cube Root Ozone (cube root ppb)",
               col.regions = trellis.par.get("regions")$col)
   detach()
 }  
#ref(lattice.contourplot1.png,left)

*グラフの回転  r-help 記事より

 > library(lattice)
 > library(grid)
 > myhist <- histogram(rnorm(50))
 > # Make two square regions side by side
 > push.viewport(viewport(layout=grid.layout(1, 2, respect=TRUE)))
 > # Go to the left region
 > push.viewport(viewport(layout.pos.col=1))
 > # Draw the histogram in normal orientation
 > print(myhist, newpage=FALSE)
 > pop.viewport()
 > # Go to the right region then rotate 90 degrees
 > push.viewport(viewport(layout.pos.col=2), viewport(angle=90))
 > # Draw the histogram (rotated 90 degrees)
 > print(myhist, newpage=FALSE
 > pop.viewport(3) 
#ref(rotate.graph.png, left)

* P. Murrel の gridBase パッケージによるグラフィックス例 (Rnews 3.2 の記事より)
 gridBase.ex.1 <- function() {
 #library(grid)
 library(gridBase)
 #library(lattice)
 midpts <- barplot(1:10, axes=FALSE)
 axis(2)
 axis(1, at=midpts, labels=FALSE)
 vps <- baseViewports()
 par(new=TRUE)
 push.viewport(vps$inner, vps$figure, vps$plot)
 grid.text(c("0ne", "two", "three", "four", "five", "six", 
             "seven", "eight", "nine", "ten"),
           x = unit(midpts, "native"),
           y = unit(-1, "lines"),
           just = "right",
           rot = 60)
 pop.viewport(3)
 }
#ref("gridBase1.png",left)

#ref(gridBase1.png,left)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS