### R code from vignette source 'import.Rnw' ################################################### ### code chunk number 1: import.Rnw:56-64 ################################################### options(prompt="R> ") options(continue = "+ ") options(width = 60) options(useFancyQuotes = FALSE) strOptions(strict.width = TRUE) library(grid) library(lattice) ################################################### ### code chunk number 2: import.Rnw:83-111 ################################################### chess <- read.table("chessmod.txt", sep = ":", quote = "", col.names = c("player1", "player2", "result", "moves", "year", "place", "openingDetailed")) chess$result <- factor(ifelse(chess$result == "1-1", "draw", ifelse((chess$result == "1-0" & chess$player1 == "La Bourdonnais") | (chess$result == "0-1" & chess$player2 == "La Bourdonnais"), "win", "loss")), levels = c("win", "draw", "loss")) chess$opening <- reorder(factor(gsub("^... |, .+$", "", chess$openingDetailed)), chess$moves, FUN = median) chess$draw <- ifelse(chess$result == "draw", "draw", "result") chess.tab <- xtabs( ~ moves + result, chess) chess.tab.df <- as.data.frame(chess.tab) chess.tab.df$nmoves <- as.numeric(as.character(chess.tab.df$moves)) chess.df <- subset(chess.tab.df, Freq > 0) # Fiddle to force y-scale to include 0 chess.df <- rbind(chess.df, data.frame(moves = NA, result = "win", Freq = 0, nmoves = 1000)) ################################################### ### code chunk number 3: chessplot (eval = FALSE) ################################################### ## xyplot(Freq ~ nmoves | result, data = chess.df, type = "h", ## layout = c(1, 3), xlim = c(0, 100)) ## ################################################### ### code chunk number 4: chess ################################################### print( # One tiny hidden fiddle to control y-axis labels xyplot(Freq ~ nmoves | result, data = chess.df, type = "h", layout = c(1, 3), xlim = c(0, 100), scales = list(y = list(at = seq(0, 6, 2)))) ) ################################################### ### code chunk number 5: chesspiece ################################################### library("grImport") PostScriptTrace("chess_game_01.fromInkscape.eps") chessPicture <- readPicture("chess_game_01.fromInkscape.eps.xml") pawn <- chessPicture[205:206] # grid.newpage() grid.picture(pawn) ################################################### ### code chunk number 6: chesspluspieceplot (eval = FALSE) ################################################### ## xyplot(Freq ~ nmoves | result, data = chess.df, type = "h", ## layout = c(1, 3), xlim = c(0, 100), ## panel = function(...) { ## panel.xyplot(...) ## grid.symbols(pawn, .05, .5, use.gc = FALSE, ## size = unit(.5, "npc"), ## gp = gpar(fill = switch(which.packet(), ## "white", "grey", "black"))) ## }) ## ################################################### ### code chunk number 7: chesspluspiece ################################################### print( # Fiddle to force y-scale to include 0 xyplot(Freq ~ nmoves | result, data = chess.df, type = "h", layout = c(1, 3), xlim = c(0, 100), scales = list(y = list(at = seq(0, 6, 2))), panel = function(...) { panel.xyplot(...) grid.symbols(pawn, .05, .5, use.gc = FALSE, size = unit(.5, "npc"), gp = gpar(fill = switch(which.packet(), "white", "grey", "black"))) }) ) ################################################### ### code chunk number 8: petaltrace ################################################### PostScriptTrace("petal.ps") ################################################### ### code chunk number 9: petal ################################################### petal <- readPicture("petal.ps.xml") grid.newpage() grid.picture(petal) ################################################### ### code chunk number 10: import.Rnw:354-357 ################################################### petalps <- readLines("petal.ps") cat(petalps, sep = "\n") ################################################### ### code chunk number 11: import.Rnw:387-391 ################################################### cat(gsub("\t", " ", gsub("(source)=", "\n \\1=", readLines("petal.ps.xml"))), sep = "\n") ################################################### ### code chunk number 12: import.Rnw:409-410 (eval = FALSE) ################################################### ## PostScriptTrace("petal.ps") ## ################################################### ### code chunk number 13: petaloutline ################################################### pointify <- function(object, ...) { # Thin out the dots for a better diagram n <- length(object@x) subset <- c(1, seq(2, n, 3), n) gTree(children = gList(linesGrob(object@x[subset], object@y[subset], default = "native", gp = gpar(col = "grey"), ...), pointsGrob(object@x[subset], object@y[subset], size = unit(2, "mm"), pch = 16, ...))) } grid.picture(petal, FUN = pointify) ################################################### ### code chunk number 14: flowertrace ################################################### PostScriptTrace("flower.ps") ################################################### ### code chunk number 15: flower ################################################### PSflower <- readPicture("flower.ps.xml") grid.newpage() grid.picture(PSflower) ################################################### ### code chunk number 16: import.Rnw:492-494 ################################################### cat(readLines("flower.ps"), sep = "\n") ################################################### ### code chunk number 17: import.Rnw:560-562 ################################################### cat(readLines("convert.ps"), sep = "\n") ################################################### ### code chunk number 18: import.Rnw:617-618 (eval = FALSE) ################################################### ## PostScriptTrace("flower.ps") ## ################################################### ### code chunk number 19: import.Rnw:623-638 ################################################### flowerLines <- gsub("\t", " ", gsub("(source)=", "\n \\1=", readLines("flower.ps.xml"))) flowerLines <- flowerLines[nchar(flowerLines) > 0] moves <- grep(" 0] petalrgml <- gsub("\t", " ", gsub("(source)=", "\n \\1=", petalrgml)) cat(petalrgml[1:(grep(" 0] hellotextrgml <- gsub("\t", " ", gsub("source=", "\n source=", hellotextrgml)) cat(hellotextrgml, sep = "\n") ################################################### ### code chunk number 48: import.Rnw:1467-1468 ################################################### slotNames(petal) ################################################### ### code chunk number 49: import.Rnw:1495-1496 ################################################### str(petal@paths[[1]]) ################################################### ### code chunk number 50: import.Rnw:1505-1506 ################################################### str(hellotext@paths[[1]]) ################################################### ### code chunk number 51: import.Rnw:1514-1515 ################################################### str(petal@summary) ################################################### ### code chunk number 52: import.Rnw:1560-1562 ################################################### cat(readLines("blueshade.R"), sep = "\n") ################################################### ### code chunk number 53: import.Rnw:1566-1567 ################################################### source("blueshade.R") ################################################### ### code chunk number 54: import.Rnw:1576-1578 ################################################### cat(readLines("blueify.R"), sep = "\n") ################################################### ### code chunk number 55: import.Rnw:1582-1583 ################################################### source("blueify.R") ################################################### ### code chunk number 56: bluetiger (eval = FALSE) ################################################### ## grid.picture(tiger[-1], ## FUN = blueify) ## ################################################### ### code chunk number 57: import.Rnw:1606-1610 ################################################### png("import-bluetiger.png", width=900, height=900) grid.picture(tiger[-1], FUN = blueify) dev.off() ################################################### ### code chunk number 58: gnulogo ################################################### PostScriptTrace("GNU.ps", "GNU.xml") GNU <- readPicture("GNU.xml") grid.picture(GNU) ################################################### ### code chunk number 59: gnulogopaths ################################################### picturePaths(GNU, nr = 1, nc = 2, label = FALSE) ################################################### ### code chunk number 60: brokengnupaths ################################################### brokenGNU <- explodePaths(GNU) picturePaths(brokenGNU, nr = 3, nc = 5, label = FALSE, freeScales = TRUE) ################################################### ### code chunk number 61: logobody (eval = FALSE) ################################################### ## barchart(~ cit, main = "Number of Citations per Year", xlab = "", ## panel = function(...) { ## grid.picture(GNU) ## grid.rect(gp = gpar(fill = rgb(1, 1, 1, .9))) ## panel.barchart(...) ## }) ################################################### ### code chunk number 62: logo ################################################### cit <- c("1998"=4, "1999"=15, "2000"=17, "2001"=39, "2002"=119, "2003"=276, "2004"=523, "2005"=945, "2006"=1475, "2007"=2015) trellis.device("pdf", file = "import-logo.pdf", height = 4, color = TRUE) print({ .Last.value <- barchart(~ cit, main = "Number of Citations per Year", xlab = "", panel = function(...) { grid.picture(GNU) grid.rect(gp = gpar(fill = rgb(1, 1, 1, .9))) panel.barchart(...) }) }); rm(.Last.value) dev.off() ################################################### ### code chunk number 63: import.Rnw:1807-1808 ################################################### PostScriptTrace("page27.ps") ################################################### ### code chunk number 64: import.Rnw:1818-1820 ################################################### page27 <- readPicture("page27.ps.xml") survivalPlot <- page27[c(3:16, 18, 27)] ################################################### ### code chunk number 65: survivalplot ################################################### pushViewport(viewport(gp = gpar(lex = .2))) grid.picture(survivalPlot) popViewport() ################################################### ### code chunk number 66: import.Rnw:1851-1853 ################################################### zeroY <- survivalPlot@paths[[9]]@y[1] zeroY ################################################### ### code chunk number 67: import.Rnw:1859-1861 ################################################### unitY <- (survivalPlot@paths[[14]]@y[1] - zeroY)/100 unitY ################################################### ### code chunk number 68: import.Rnw:1869-1871 ################################################### greenY <- (survivalPlot@paths[[15]]@y - zeroY)/unitY head(round(unname(greenY), 1), n = 20) ################################################### ### code chunk number 69: import.Rnw:1876-1880 ################################################### library("survival") sfit <- survfit(Surv(time, status) ~ trt, data = veteran) originalGreenY <- sfit$surv[1:sfit$strata[1]] head(round(originalGreenY*100, 1), n = 9)