graphics-Ex.Rout.save 90 KB
Newer Older
1

2
R version 3.5.2 RC (2018-12-13 r75856) -- "Eggshell Igloo"
3
Copyright (C) 2018 The R Foundation for Statistical Computing
4
Platform: x86_64-pc-linux-gnu (64-bit)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> pkgname <- "graphics"
> source(file.path(R.home("share"), "R", "examples-header.R"))
> options(warn = 1)
> library('graphics')
> 
25
> base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
26
> base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv')
27 28 29 30 31 32 33 34 35 36 37 38 39
> cleanEx()
> nameEx("abline")
> ### * abline
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: abline
> ### Title: Add Straight Lines to a Plot
> ### Aliases: abline
> ### Keywords: aplot
> 
> ### ** Examples
> 
40 41
> ## Setup up coordinate system (with x == y aspect ratio):
> plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
42
> ## the x- and y-axis, and an integer grid
43
> abline(h = 0, v = 0, col = "gray60")
44
> text(1,0, "abline( h = 0 )", col = "gray60", adj = c(0, -.1))
45 46 47
> abline(h = -1:5, v = -2:3, col = "lightgray", lty = 3)
> abline(a = 1, b = 2, col = 2)
> text(1,3, "abline( 1, 2 )", col = 2, adj = c(-.1, -.1))
48 49 50 51 52
> 
> ## Simple Regression Lines:
> require(stats)
> sale5 <- c(6, 4, 9, 7, 6, 12, 8, 10, 9, 13)
> plot(sale5)
53 54
> abline(lsfit(1:10, sale5))
> abline(lsfit(1:10, sale5, intercept = FALSE), col = 4) # less fitting
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
> 
> z <- lm(dist ~ speed, data = cars)
> plot(cars)
> abline(z) # equivalent to abline(reg = z) or
> abline(coef = coef(z))
> 
> ## trivial intercept model
> abline(mC <- lm(dist ~ 1, data = cars)) ## the same as
> abline(a = coef(mC), b = 0, col = "blue")
> 
> 
> 
> cleanEx()
> nameEx("arrows")
> ### * arrows
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: arrows
> ### Title: Add Arrows to a Plot
> ### Aliases: arrows
> ### Keywords: aplot
> 
> ### ** Examples
> 
> x <- stats::runif(12); y <- stats::rnorm(12)
81 82
> i <- order(x, y); x <- x[i]; y <- y[i]
> plot(x,y, main = "arrows(.) and segments(.)")
83
> ## draw arrows from point to point :
84 85
> s <- seq(length(x)-1)  # one shorter than data
> arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3)
86
> s <- s[-length(s)]
87
> segments(x[s], y[s], x[s+2], y[s+2], col = "pink")
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
> 
> 
> 
> cleanEx()
> nameEx("assocplot")
> ### * assocplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: assocplot
> ### Title: Association Plots
> ### Aliases: assocplot
> ### Keywords: hplot
> 
> ### ** Examples
> 
> ## Aggregate over sex:
> x <- margin.table(HairEyeColor, c(1, 2))
> x
       Eye
Hair    Brown Blue Hazel Green
  Black    68   20    15     5
  Brown   119   84    54    29
  Red      26   17    14    14
  Blond     7   94    10    16
> assocplot(x, main = "Relation between hair and eye color")
> 
> 
> 
> cleanEx()
> nameEx("axTicks")
> ### * axTicks
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: axTicks
> ### Title: Compute Axis Tickmark Locations
> ### Aliases: axTicks
> ### Keywords: dplot
> 
> ### ** Examples
> 
>  plot(1:7, 10*21:27)
>  axTicks(1)
[1] 1 2 3 4 5 6 7
>  axTicks(2)
[1] 210 220 230 240 250 260 270
>  stopifnot(identical(axTicks(1), axTicks(3)),
+            identical(axTicks(2), axTicks(4)))
> 
> ## Show how axTicks() and axis() correspond :
139 140 141 142 143
> op <- par(mfrow = c(3, 1))
> for(x in 9999 * c(1, 2, 8)) {
+     plot(x, 9, log = "x")
+     cat(formatC(par("xaxp"), width = 5),";", T <- axTicks(1),"\n")
+     rug(T, col =  adjustcolor("red", 0.5), lwd = 4)
144 145 146 147 148 149
+ }
 1000 1e+05     3 ; 200 500 1000 2000 5000 10000 20000 50000 1e+05 2e+05 5e+05 
 1000 1e+06     2 ; 500 1000 5000 10000 50000 1e+05 5e+05 1e+06 
 1000 1e+07     1 ; 1000 10000 1e+05 1e+06 1e+07 
> par(op)
> 
150 151 152 153
> x <- 9.9*10^(-3:10)
> plot(x, 1:14, log = "x")
> axTicks(1) # now length 5, in R <= 2.13.x gave the following
[1] 1e-02 1e+01 1e+04 1e+07 1e+10
154
> axTicks(1, nintLog = Inf) # rather too many
155 156 157
 [1] 1e-02 1e-01 1e+00 1e+01 1e+02 1e+03 1e+04 1e+05 1e+06 1e+07 1e+08 1e+09
[13] 1e+10 1e+11
> 
158 159
> ## An example using axTicks() without reference to an existing plot
> ## (copying R's internal procedures for setting axis ranges etc.),
160
> ## You do need to supply _all_ of axp, usr, log, nintLog
161 162 163
> ## standard logarithmic y axis labels
> ylims <- c(0.2, 88)
> get_axp <- function(x) 10^c(ceiling(x[1]), floor(x[2]))
164
> ## mimic par("yaxs") == "i"
165
> usr.i <- log10(ylims)
166 167
> (aT.i <- axTicks(side = 2, usr = usr.i,
+                  axp = c(get_axp(usr.i), n = 3), log = TRUE, nintLog = 5))
168
[1]  0.2  0.5  1.0  2.0  5.0 10.0 20.0 50.0
169
> ## mimic (default) par("yaxs") == "r"
170
> usr.r <- extendrange(r = log10(ylims), f = 0.04)
171 172
> (aT.r <- axTicks(side = 2, usr = usr.r,
+                  axp = c(get_axp(usr.r), 3), log = TRUE, nintLog = 5))
173 174 175
[1]   0.2   0.5   1.0   2.0   5.0  10.0  20.0  50.0 100.0
> 
> ## Prove that we got it right :
176 177
> plot(0:1, ylims, log = "y", yaxs = "i")
> stopifnot(all.equal(aT.i, axTicks(side = 2)))
178
> 
179 180
> plot(0:1, ylims, log = "y", yaxs = "r")
> stopifnot(all.equal(aT.r, axTicks(side = 2)))
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("axis.POSIXct")
> ### * axis.POSIXct
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: axis.POSIXct
> ### Title: Date and Date-time Plotting Functions
> ### Aliases: axis.POSIXct axis.Date
> ### Keywords: utilities chron
> 
> ### ** Examples
> 
> with(beaver1, {
+ time <- strptime(paste(1990, day, time %/% 100, time %% 100),
+                  "%Y %j %H %M")
201
+ plot(time, temp, type = "l") # axis at 4-hour intervals.
202
+ # now label every hour on the time axis
203
+ plot(time, temp, type = "l", xaxt = "n")
204
+ r <- as.POSIXct(round(range(time), "hours"))
205
+ axis.POSIXct(1, at = seq(r[1], r[2], by = "hour"), format = "%H")
206 207
+ })
> 
208 209
> plot(.leap.seconds, seq_along(.leap.seconds), type = "n", yaxt = "n",
+      xlab = "leap seconds", ylab = "", bty = "n")
210 211 212 213 214 215 216 217 218 219 220 221
> rug(.leap.seconds)
> ## or as dates
> lps <- as.Date(.leap.seconds)
> plot(lps, seq_along(.leap.seconds),
+      type = "n", yaxt = "n", xlab = "leap seconds",
+      ylab = "", bty = "n")
> rug(lps)
> 
> ## 100 random dates in a 10-week period
> random.dates <- as.Date("2001/1/1") + 70*sort(stats::runif(100))
> plot(random.dates, 1:100)
> # or for a better axis labelling
222 223 224
> plot(random.dates, 1:100, xaxt = "n")
> axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "weeks"))
> axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "days"),
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
+      labels = FALSE, tcl = -0.2)
> 
> 
> 
> cleanEx()
> nameEx("axis")
> ### * axis
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: axis
> ### Title: Add an Axis to a Plot
> ### Aliases: axis
> ### Keywords: aplot
> 
> ### ** Examples
> 
> require(stats) # for rnorm
> plot(1:4, rnorm(4), axes = FALSE)
> axis(1, 1:4, LETTERS[1:4])
> axis(2)
> box() #- to make it look "as usual"
> 
> plot(1:7, rnorm(7), main = "axis() examples",
+      type = "s", xaxt = "n", frame = FALSE, col = "red")
> axis(1, 1:7, LETTERS[1:7], col.axis = "blue")
> # unusual options:
252
> axis(4, col = "violet", col.axis = "dark violet", lwd = 2)
253 254 255 256
> axis(3, col = "gold", lty = 2, lwd = 0.5)
> 
> # one way to have a custom x axis
> plot(1:10, xaxt = "n")
257
> axis(1, xaxp = c(2, 9, 7))
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
> 
> 
> 
> cleanEx()
> nameEx("barplot")
> ### * barplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: barplot
> ### Title: Bar Plots
> ### Aliases: barplot barplot.default
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(grDevices) # for colours
275 276
> tN <- table(Ni <- stats::rpois(100, lambda = 5))
> r <- barplot(tN, col = rainbow(20))
277
> #- type = "h" plotting *is* 'bar'plot
278
> lines(r, tN, type = "h", col = "red", lwd = 2)
279
> 
280
> barplot(tN, space = 1.5, axisnames = FALSE,
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
+         sub = "barplot(..., space= 1.5, axisnames = FALSE)")
> 
> barplot(VADeaths, plot = FALSE)
[1] 0.7 1.9 3.1 4.3
> barplot(VADeaths, plot = FALSE, beside = TRUE)
     [,1] [,2] [,3] [,4]
[1,]  1.5  7.5 13.5 19.5
[2,]  2.5  8.5 14.5 20.5
[3,]  3.5  9.5 15.5 21.5
[4,]  4.5 10.5 16.5 22.5
[5,]  5.5 11.5 17.5 23.5
> 
> mp <- barplot(VADeaths) # default
> tot <- colMeans(VADeaths)
> text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
> barplot(VADeaths, beside = TRUE,
+         col = c("lightblue", "mistyrose", "lightcyan",
+                 "lavender", "cornsilk"),
+         legend = rownames(VADeaths), ylim = c(0, 100))
> title(main = "Death Rates in Virginia", font.main = 4)
> 
> hh <- t(VADeaths)[, 5:1]
> mybarcol <- "gray20"
> mp <- barplot(hh, beside = TRUE,
+         col = c("lightblue", "mistyrose",
+                 "lightcyan", "lavender"),
307
+         legend = colnames(VADeaths), ylim = c(0,100),
308 309 310 311
+         main = "Death Rates in Virginia", font.main = 4,
+         sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
+         cex.names = 1.5)
> segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
312
> stopifnot(dim(mp) == dim(hh))  # corresponding matrices
313 314 315 316 317 318 319 320 321 322 323 324
> mtext(side = 1, at = colMeans(mp), line = -2,
+       text = paste("Mean", formatC(colMeans(hh))), col = "red")
> 
> # Bar shading example
> barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
+         legend = rownames(VADeaths))
> title(main = list("Death Rates in Virginia", font = 4))
> 
> # border :
> barplot(VADeaths, border = "dark blue") 
> 
> # log scales (not much sense here):
325 326
> barplot(tN, col = heat.colors(12), log = "y")
> barplot(tN, col = gray.colors(20), log = "xy")
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
> 
> # args.legend
> barplot(height = cbind(x = c(465, 91) / 465 * 100,
+                        y = c(840, 200) / 840 * 100,
+                        z = c(37, 17) / 37 * 100),
+         beside = FALSE,
+         width = c(465, 840, 37),
+         col = c(1, 2),
+         legend.text = c("A", "B"),
+         args.legend = list(x = "topleft"))
> 
> 
> 
> cleanEx()
> nameEx("box")
> ### * box
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: box
> ### Title: Draw a Box around a Plot
> ### Aliases: box
> ### Keywords: aplot
> 
> ### ** Examples
> 
353
> plot(1:7, abs(stats::rnorm(7)), type = "h", axes = FALSE)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
> axis(1, at = 1:7, labels = letters[1:7])
> box(lty = '1373', col = 'red')
> 
> 
> 
> cleanEx()
> nameEx("boxplot")
> ### * boxplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: boxplot
> ### Title: Box Plots
> ### Aliases: boxplot boxplot.default boxplot.formula
> ### Keywords: hplot
> 
> ### ** Examples
> 
> ## boxplot on a formula:
> boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
> # *add* notches (somewhat funny here):
> boxplot(count ~ spray, data = InsectSprays,
+         notch = TRUE, add = TRUE, col = "blue")
Warning in bxp(list(stats = c(7, 11, 14, 18.5, 23, 7, 12, 16.5, 18, 21,  :
  some notches went outside hinges ('box'): maybe set notch=FALSE
> 
> boxplot(decrease ~ treatment, data = OrchardSprays,
+         log = "y", col = "bisque")
> 
383
> rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque")
384 385 386 387 388 389 390 391 392 393 394 395
> title("Comparing boxplot()s and non-robust mean +/- SD")
> 
> mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)
> sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)
> xi <- 0.3 + seq(rb$n)
> points(xi, mn.t, col = "orange", pch = 18)
> arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,
+        code = 3, col = "pink", angle = 75, length = .1)
> 
> ## boxplot on a matrix:
> mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
+              `5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
396 397 398 399
> boxplot(mat) # directly, calling boxplot.matrix()
> 
> ## boxplot on a data frame:
> df. <- as.data.frame(mat)
400
> par(las = 1) # all axis labels horizontal
401
> boxplot(df., main = "boxplot(*, horizontal = TRUE)", horizontal = TRUE)
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
> 
> ## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
> boxplot(len ~ dose, data = ToothGrowth,
+         boxwex = 0.25, at = 1:3 - 0.2,
+         subset = supp == "VC", col = "yellow",
+         main = "Guinea Pigs' Tooth Growth",
+         xlab = "Vitamin C dose mg",
+         ylab = "tooth length",
+         xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
> boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
+         boxwex = 0.25, at = 1:3 + 0.2,
+         subset = supp == "OJ", col = "orange")
> legend(2, 9, c("Ascorbic acid", "Orange juice"),
+        fill = c("yellow", "orange"))
> 
417 418 419 420 421 422 423
> ## With less effort (slightly different) using factor *interaction*:
> boxplot(len ~ dose:supp, data = ToothGrowth,
+         boxwex = 0.5, col = c("orange", "yellow"),
+         main = "Guinea Pigs' Tooth Growth",
+         xlab = "Vitamin C dose mg", ylab = "tooth length",
+         sep = ":", lex.order = TRUE, ylim = c(0, 35), yaxs = "i")
> 
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
> ## more examples in  help(bxp)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("boxplot.matrix")
> ### * boxplot.matrix
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: boxplot.matrix
> ### Title: Draw a Boxplot for each Column (Row) of a Matrix
> ### Aliases: boxplot.matrix
> ### Keywords: hplot
> 
> ### ** Examples
> 
> ## Very similar to the example in ?boxplot
> mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
+              T5 = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
> boxplot(mat, main = "boxplot.matrix(...., main = ...)",
+         notch = TRUE, col = 1:4)
> 
> 
> 
> cleanEx()
> nameEx("bxp")
> ### * bxp
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: bxp
> ### Title: Draw Box Plots from Summaries
> ### Aliases: bxp
> ### Keywords: aplot
> 
> ### ** Examples
> 
> require(stats)
> set.seed(753)
465
> (bx.p <- boxplot(split(rt(100, 4), gl(5, 20))))
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
$stats
            [,1]        [,2]        [,3]        [,4]        [,5]
[1,] -1.66391873 -2.02625162 -2.12785004 -2.76510496 -1.70034047
[2,] -0.55308292 -0.65897584 -0.86705616 -1.63431484 -0.81848966
[3,] -0.06763313  0.04887846  0.09674026 -0.06712275 -0.01150075
[4,]  0.68813940  0.91705734  1.05562526  0.56746581  0.49017934
[5,]  1.14222667  3.16270157  2.07574986  2.09523462  1.87734641

$n
[1] 20 20 20 20 20

$conf
           [,1]       [,2]       [,3]       [,4]       [,5]
[1,] -0.5061554 -0.5079321 -0.5825407 -0.8450091 -0.4738519
[2,]  0.3708891  0.6056890  0.7760212  0.7107636  0.4508504

$out
[1]  4.115274  3.224584  3.920438  4.168341 -4.357819  2.498006

$group
[1] 1 1 1 4 5 5

$names
[1] "1" "2" "3" "4" "5"

491
> op <- par(mfrow =  c(2, 2))
492
> bxp(bx.p, xaxt = "n")
493
> bxp(bx.p, notch = TRUE, axes = FALSE, pch = 4, boxfill = 1:5)
494 495
Warning in bxp(bx.p, notch = TRUE, axes = FALSE, pch = 4, boxfill = 1:5) :
  some notches went outside hinges ('box'): maybe set notch=FALSE
496 497
> bxp(bx.p, notch = TRUE, boxfill = "lightblue", frame = FALSE,
+     outl = FALSE, main = "bxp(*, frame= FALSE, outl= FALSE)")
498 499
Warning in bxp(bx.p, notch = TRUE, boxfill = "lightblue", frame = FALSE,  :
  some notches went outside hinges ('box'): maybe set notch=FALSE
500
> bxp(bx.p, notch = TRUE, boxfill = "lightblue", border = 2:6,
501
+     ylim = c(-4,4), pch = 22, bg = "green", log = "x",
502
+     main = "... log = 'x', ylim = *")
503 504 505
Warning in bxp(bx.p, notch = TRUE, boxfill = "lightblue", border = 2:6,  :
  some notches went outside hinges ('box'): maybe set notch=FALSE
> par(op)
506
> op <- par(mfrow = c(1, 2))
507 508
> 
> ## single group -- no label
509
> boxplot (weight ~ group, data = PlantGrowth, subset = group == "ctrl")
510 511
> ## with label
> bx <- boxplot(weight ~ group, data = PlantGrowth,
512 513
+               subset = group == "ctrl", plot = FALSE)
> bxp(bx, show.names=TRUE)
514 515
> par(op)
> 
516 517
> z <- split(rnorm(1000), rpois(1000, 2.2))
> boxplot(z, whisklty = 3, main = "boxplot(z, whisklty = 3)")
518 519
> 
> ## Colour support similar to plot.default:
520 521 522
> op <- par(mfrow = 1:2, bg = "light gray", fg = "midnight blue")
> boxplot(z,   col.axis = "skyblue3", main = "boxplot(*, col.axis=..,main=..)")
> plot(z[[1]], col.axis = "skyblue3", main =    "plot(*, col.axis=..,main=..)")
523 524 525 526 527
> mtext("par(bg=\"light gray\", fg=\"midnight blue\")",
+       outer = TRUE, line = -1.2)
> par(op)
> 
> ## Mimic S-Plus:
528 529 530
> splus <- list(boxwex = 0.4, staplewex = 1, outwex = 1, boxfill = "grey40",
+               medlwd = 3, medcol = "white", whisklty = 3, outlty = 1, outpch = NA)
> boxplot(z, pars = splus)
531
> ## Recycled and "sweeping" parameters
532 533 534
> op <- par(mfrow = c(1,2))
>  boxplot(z, border = 1:5, lty = 3, medlty = 1, medlwd = 2.5)
>  boxplot(z, boxfill = 1:3, pch = 1:5, lwd = 1.5, medcol = "white")
535 536
> par(op)
> ## too many possibilities
537
> boxplot(z, boxfill = "light gray", outpch = 21:25, outlty = 2,
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
+         bg = "pink", lwd = 2,
+         medcol = "dark blue", medcex = 2, medpch = 20)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("cdplot")
> ### * cdplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: cdplot
> ### Title: Conditional Density Plots
> ### Aliases: cdplot cdplot.default cdplot.formula
> ### Keywords: hplot
> 
> ### ** Examples
> 
> ## NASA space shuttle o-ring failures
> fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1,
+                  1, 2, 1, 1, 1, 1, 1),
+                levels = 1:2, labels = c("no", "yes"))
> temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
+                  70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)
> 
> ## CD plot
> cdplot(fail ~ temperature)
> cdplot(fail ~ temperature, bw = 2)
> cdplot(fail ~ temperature, bw = "SJ")
> 
> ## compare with spinogram
> (spineplot(fail ~ temperature, breaks = 3))
           fail
temperature no yes
    [50,60]  0   3
    (60,70]  8   3
    (70,80]  7   1
    (80,90]  1   0
> 
> ## highlighting for failures
> cdplot(fail ~ temperature, ylevels = 2:1)
> 
> ## scatter plot with conditional density
> cdens <- cdplot(fail ~ temperature, plot = FALSE)
> plot(I(as.numeric(fail) - 1) ~ jitter(temperature, factor = 2),
+      xlab = "Temperature", ylab = "Conditional failure probability")
> lines(53:81, 1 - cdens[[1]](53:81), col = 2)
> 
> 
> 
> cleanEx()
> nameEx("clip")
> ### * clip
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: clip
> ### Title: Set Clipping Region
> ### Aliases: clip
> ### Keywords: dplot
> 
> ### ** Examples
> 
> x <- rnorm(1000)
603
> hist(x, xlim = c(-4,4))
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
> usr <- par("usr")
> clip(usr[1], -2, usr[3], usr[4])
> hist(x, col = 'red', add = TRUE)
> clip(2, usr[2], usr[3], usr[4])
> hist(x, col = 'blue', add = TRUE)
> do.call("clip", as.list(usr))  # reset to plot region
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("contour")
> ### * contour
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: contour
> ### Title: Display Contours
> ### Aliases: contour contour.default
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
> require(grDevices) # for colours
> x <- -6:16
> op <- par(mfrow = c(2, 2))
> contour(outer(x, x), method = "edge", vfont = c("sans serif", "plain"))
> z <- outer(x, sqrt(abs(x)), FUN = "/")
> image(x, x, z)
> contour(x, x, z, col = "pink", add = TRUE, method = "edge",
+         vfont = c("sans serif", "plain"))
635 636 637 638
> contour(x, x, z, ylim = c(1, 6), method = "simple", labcex = 1,
+         xlab = quote(x[1]), ylab = quote(x[2]))
> contour(x, x, z, ylim = c(-6, 6), nlev = 20, lty = 2, method = "simple",
+         main = "20 levels; \"simple\" labelling method")
639 640 641 642 643 644 645 646 647 648 649 650
> par(op)
> 
> ## Persian Rug Art:
> x <- y <- seq(-4*pi, 4*pi, len = 27)
> r <- sqrt(outer(x^2, y^2, "+"))
> opar <- par(mfrow = c(2, 2), mar = rep(0, 4))
> for(f in pi^(0:3))
+   contour(cos(r^2)*exp(-r/f),
+           drawlabels = FALSE, axes = FALSE, frame = TRUE)
> 
> rx <- range(x <- 10*1:nrow(volcano))
> ry <- range(y <- 10*1:ncol(volcano))
651
> ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2
652 653
> tcol <- terrain.colors(12)
> par(opar); opar <- par(pty = "s", bg = "lightcyan")
654
> plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
655 656 657 658 659 660 661 662
> u <- par("usr")
> rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
> contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
+         vfont = c("sans serif", "plain"))
> title("A Topographic Map of Maunga Whau", font = 4)
> abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1)
> 
> ## contourLines produces the same contour lines as contour
663
> plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
664 665
> u <- par("usr")
> rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
666 667 668 669
> contour(x, y, volcano, col = tcol[1], lty = "solid", add = TRUE,
+         vfont = c("sans serif", "plain"))
> line.list <- contourLines(x, y, volcano)
> invisible(lapply(line.list, lines, lwd=3, col=adjustcolor(2, .3)))
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
> par(opar)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("convertXY")
> ### * convertXY
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: convertXY
> ### Title: Convert between Graphics Coordinate Systems
> ### Aliases: grconvertX grconvertY
> ### Keywords: dplot
> 
> ### ** Examples
> 
> op <- par(omd=c(0.1, 0.9, 0.1, 0.9), mfrow = c(1, 2))
> plot(1:4)
> for(tp in c("in", "dev", "ndc", "nfc", "npc", "nic"))
+     print(grconvertX(c(1.0, 4.0), "user", tp))
[1] 1.577778 3.022222
693
[1] 113.6 217.6
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
[1] 0.2253968 0.4317460
[1] 0.3134921 0.8293651
[1] 0.03703704 0.96296296
[1] 0.1567460 0.4146825
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("coplot")
> ### * coplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: coplot
> ### Title: Conditioning Plots
> ### Aliases: coplot co.intervals
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
> ## Tonga Trench Earthquakes
> coplot(lat ~ long | depth, data = quakes)
718 719
> given.depth <- co.intervals(quakes$depth, number = 4, overlap = .1)
> coplot(lat ~ long | depth, data = quakes, given.v = given.depth, rows = 1)
720 721 722 723
> 
> ## Conditioning on 2 variables:
> ll.dm <- lat ~ long | depth * mag
> coplot(ll.dm, data = quakes)
724 725 726
> coplot(ll.dm, data = quakes, number = c(4, 7), show.given = c(TRUE, FALSE))
> coplot(ll.dm, data = quakes, number = c(3, 7),
+        overlap = c(-.5, .1)) # negative overlap DROPS values
727 728
> 
> ## given two factors
729
> Index <- seq(length = nrow(warpbreaks)) # to get nicer default labels
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
> coplot(breaks ~ Index | wool * tension, data = warpbreaks,
+        show.given = 0:1)
> coplot(breaks ~ Index | wool * tension, data = warpbreaks,
+        col = "red", bg = "pink", pch = 21,
+        bar.bg = c(fac = "light blue"))
> 
> ## Example with empty panels:
> with(data.frame(state.x77), {
+ coplot(Life.Exp ~ Income | Illiteracy * state.region, number = 3,
+        panel = function(x, y, ...) panel.smooth(x, y, span = .8, ...))
+ ## y ~ factor -- not really sensible, but 'show off':
+ coplot(Life.Exp ~ state.region | Income * state.division,
+        panel = panel.smooth)
+ })
> 
> 
> 
> cleanEx()
> nameEx("curve")
> ### * curve
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: curve
> ### Title: Draw Function Plots
> ### Aliases: curve plot.function
> ### Keywords: hplot
> 
> ### ** Examples
> 
760 761
> plot(qnorm) # default range c(0, 1) is appropriate here,
>             # but end values are -/+Inf and so are omitted.
762
> plot(qlogis, main = "The Inverse Logit : qlogis()")
763
> abline(h = 0, v = 0:2/2, lty = 3, col = "gray")
764
> 
765 766 767
> curve(sin, -2*pi, 2*pi, xname = "t")
> curve(tan, xname = "t", add = NA,
+       main = "curve(tan)  --> same x-scale as previous plot")
768
> 
769 770 771
> op <- par(mfrow = c(2, 2))
> curve(x^3 - 3*x, -2, 2)
> curve(x^2 - 2, add = TRUE, col = "violet")
772
> 
773
> ## simple and advanced versions, quite similar:
774
> plot(cos, -pi,  3*pi)
775
> curve(cos, xlim = c(-pi, 3*pi), n = 1001, col = "blue", add = TRUE)
776 777
> 
> chippy <- function(x) sin(cos(x)*exp(-x/2))
778
> curve(chippy, -8, 7, n = 2001)
779 780
> plot (chippy, -8, -5)
> 
781
> for(ll in c("", "x", "y", "xy"))
782
+    curve(log(1+x), 1, 100, log = ll, sub = paste0("log = '", ll, "'"))
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("dotchart")
> ### * dotchart
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: dotchart
> ### Title: Cleveland's Dot Plots
> ### Aliases: dotchart
> ### Keywords: hplot
> 
> ### ** Examples
> 
> dotchart(VADeaths, main = "Death Rates in Virginia - 1940")
802
> op <- par(xaxs = "i")  # 0 -- 100%
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
> dotchart(t(VADeaths), xlim = c(0,100),
+          main = "Death Rates in Virginia - 1940")
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("filled.contour")
> ### * filled.contour
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: filled.contour
> ### Title: Level (Contour) Plots
818
> ### Aliases: filled.contour .filled.contour
819 820 821 822 823
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
> require(grDevices) # for colours
824
> filled.contour(volcano, color = terrain.colors, asp = 1) # simple
825 826 827 828 829 830 831 832
> 
> x <- 10*1:nrow(volcano)
> y <- 10*1:ncol(volcano)
> filled.contour(x, y, volcano, color = terrain.colors,
+     plot.title = title(main = "The Topography of Maunga Whau",
+     xlab = "Meters North", ylab = "Meters West"),
+     plot.axes = { axis(1, seq(100, 800, by = 100))
+                   axis(2, seq(100, 600, by = 100)) },
833 834
+     key.title = title(main = "Height\n(meters)"),
+     key.axes = axis(4, seq(90, 190, by = 10)))  # maybe also asp = 1
835 836 837 838 839 840 841
> mtext(paste("filled.contour(.) from", R.version.string),
+       side = 1, line = 4, adj = 1, cex = .66)
> 
> # Annotating a filled contour plot
> a <- expand.grid(1:20, 1:20)
> b <- matrix(a[,1] + a[,2], 20)
> filled.contour(x = 1:20, y = 1:20, z = b,
842
+                plot.axes = { axis(1); axis(2); points(10, 10) })
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
> 
> ## Persian Rug Art:
> x <- y <- seq(-4*pi, 4*pi, len = 27)
> r <- sqrt(outer(x^2, y^2, "+"))
> filled.contour(cos(r^2)*exp(-r/(2*pi)), axes = FALSE)
> ## rather, the key *should* be labeled:
> filled.contour(cos(r^2)*exp(-r/(2*pi)), frame.plot = FALSE,
+                plot.axes = {})
> 
> 
> 
> cleanEx()
> nameEx("fourfoldplot")
> ### * fourfoldplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: fourfoldplot
> ### Title: Fourfold Plots
> ### Aliases: fourfoldplot
> ### Keywords: hplot
> 
> ### ** Examples
> 
> ## Use the Berkeley admission data as in Friendly (1995).
> x <- aperm(UCBAdmissions, c(2, 1, 3))
> dimnames(x)[[2]] <- c("Yes", "No")
> names(dimnames(x)) <- c("Sex", "Admit?", "Department")
> stats::ftable(x)
              Department   A   B   C   D   E   F
Sex    Admit?                                   
Male   Yes               512 353 120 138  53  22
       No                313 207 205 279 138 351
Female Yes                89  17 202 131  94  24
       No                 19   8 391 244 299 317
> 
> ## Fourfold display of data aggregated over departments, with
> ## frequencies standardized to equate the margins for admission
> ## and sex.
> ## Figure 1 in Friendly (1994).
> fourfoldplot(margin.table(x, c(1, 2)))
> 
> ## Fourfold display of x, with frequencies in each table
> ## standardized to equate the margins for admission and sex.
> ## Figure 2 in Friendly (1994).
> fourfoldplot(x)
> 
> ## Fourfold display of x, with frequencies in each table
> ## standardized to equate the margins for admission. but not
> ## for sex.
> ## Figure 3 in Friendly (1994).
> fourfoldplot(x, margin = 2)
> 
> 
> 
> cleanEx()
> nameEx("grid")
> ### * grid
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: grid
> ### Title: Add Grid to a Plot
> ### Aliases: grid
> ### Keywords: aplot
> 
> ### ** Examples
> 
> plot(1:3)
> grid(NA, 5, lwd = 2) # grid only in y-direction
> 
914
> ## maybe change the desired number of tick marks:  par(lab = c(mx, my, 7))
915 916 917 918 919 920 921
> op <- par(mfcol = 1:2)
> with(iris,
+      {
+      plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
+           xlim = c(4, 8), ylim = c(2, 4.5), panel.first = grid(),
+           main = "with(iris,  plot(...., panel.first = grid(), ..) )")
+      plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
922 923
+           panel.first = grid(3, lty = 1, lwd = 2),
+           main = "... panel.first = grid(3, lty = 1, lwd = 2), ..")
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
+      }
+     )
> par(op)
> 
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("hist.POSIXt")
> ### * hist.POSIXt
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: hist.POSIXt
> ### Title: Histogram of a Date or Date-Time Object
> ### Aliases: hist.POSIXt hist.Date
> ### Keywords: chron dplot hplot
> 
> ### ** Examples
> 
> hist(.leap.seconds, "years", freq = TRUE)
> hist(.leap.seconds,
947
+      seq(ISOdate(1970, 1, 1), ISOdate(2020, 1, 1), "5 years"))
948
> rug(.leap.seconds, lwd=2)
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
> 
> ## 100 random dates in a 10-week period
> random.dates <- as.Date("2001/1/1") + 70*stats::runif(100)
> hist(random.dates, "weeks", format = "%d %b")
> 
> 
> 
> cleanEx()
> nameEx("hist")
> ### * hist
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: hist
> ### Title: Histograms
> ### Aliases: hist hist.default
> ### Keywords: dplot hplot distribution
> 
> ### ** Examples
> 
969
> op <- par(mfrow = c(2, 2))
970
> hist(islands)
971
> utils::str(hist(islands, col = "gray", labels = TRUE))
972 973 974 975 976 977 978
List of 6
 $ breaks  : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
 $ counts  : int [1:9] 41 2 1 1 1 1 0 0 1
 $ density : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ mids    : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
 $ xname   : chr "islands"
 $ equidist: logi TRUE
979 980
 - attr(*, "class")= chr "histogram"
> 
981
> hist(sqrt(islands), breaks = 12, col = "lightblue", border = "pink")
982 983
> ##-- For non-equidistant breaks, counts should NOT be graphed unscaled:
> r <- hist(sqrt(islands), breaks = c(4*0:5, 10*3:5, 70, 100, 140),
984 985
+           col = "blue1")
> text(r$mids, r$density, r$counts, adj = c(.5, -.5), col = "blue3")
986
> sapply(r[2:3], sum)
987 988
   counts   density 
48.000000  0.215625 
989 990 991 992 993 994
> sum(r$density * diff(r$breaks)) # == 1
[1] 1
> lines(r, lty = 3, border = "purple") # -> lines.histogram(*)
> par(op)
> 
> require(utils) # for str
995
> str(hist(islands, breaks = 12, plot =  FALSE)) #-> 10 (~= 12) breaks
996 997 998 999 1000 1001 1002
List of 6
 $ breaks  : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
 $ counts  : int [1:9] 41 2 1 1 1 1 0 0 1
 $ density : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
 $ mids    : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
 $ xname   : chr "islands"
 $ equidist: logi TRUE
1003
 - attr(*, "class")= chr "histogram"
1004
> str(hist(islands, breaks = c(12,20,36,80,200,1000,17000), plot = FALSE))
1005 1006 1007 1008 1009 1010 1011
List of 6
 $ breaks  : num [1:7] 12 20 36 80 200 1000 17000
 $ counts  : int [1:6] 12 11 8 6 4 7
 $ density : num [1:6] 0.03125 0.014323 0.003788 0.001042 0.000104 ...
 $ mids    : num [1:6] 16 28 58 140 600 9000
 $ xname   : chr "islands"
 $ equidist: logi FALSE
1012 1013
 - attr(*, "class")= chr "histogram"
> 
1014
> hist(islands, breaks = c(12,20,36,80,200,1000,17000), freq = TRUE,
1015 1016
+      main = "WRONG histogram") # and warning
Warning in plot.histogram(r, freq = freq1, col = col, border = border, angle = angle,  :
1017
  the AREAS in the plot are wrong -- rather use 'freq = FALSE'
1018 1019 1020 1021 1022
> require(stats)
> set.seed(14)
> x <- rchisq(100, df = 4)
> ## Don't show: 
> op <- par(mfrow = 2:1, mgp = c(1.5, 0.6, 0), mar = .1 + c(3,3:1))
1023
> ## End(Don't show)
1024
> ## Comparing data with a model distribution should be done with qqplot()!
1025
> qqplot(x, qchisq(ppoints(x), df = 4)); abline(0, 1, col = 2, lty = 2)
1026 1027 1028 1029 1030 1031
> 
> ## if you really insist on using hist() ... :
> hist(x, freq = FALSE, ylim = c(0, 0.2))
> curve(dchisq(x, df = 4), col = 2, lty = 2, lwd = 2, add = TRUE)
> ## Don't show: 
> par(op)
1032
> ## End(Don't show)
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("identify")
> ### * identify
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: identify
> ### Title: Identify Points in a Scatter Plot
> ### Aliases: identify identify.default
> ### Keywords: iplot
> 
> ### ** Examples
> 
> ## A function to use identify to select points, and overplot the
> ## points with another symbol as they are selected
1052
> identifyPch <- function(x, y = NULL, n = length(x), plot = FALSE, pch = 19, ...)
1053 1054
+ {
+     xy <- xy.coords(x, y); x <- xy$x; y <- xy$y
1055
+     sel <- rep(FALSE, length(x))
1056
+     while(sum(sel) < n) {
1057
+         ans <- identify(x[!sel], y[!sel], labels = which(!sel), n = 1, plot = plot, ...)
1058 1059 1060 1061 1062
+         if(!length(ans)) break
+         ans <- which(!sel)[ans]
+         points(x[ans], y[ans], pch = pch)
+         sel[ans] <- TRUE
+     }
1063 1064 1065 1066 1067 1068 1069
+     ## return indices of selected points
+     which(sel)
+ }
> 
> if(dev.interactive()) { ## use it
+   x <- rnorm(50); y <- rnorm(50)
+   plot(x,y); identifyPch(x,y) # how fast to get all?
1070 1071 1072 1073
+ }
> 
> 
> 
1074
> 
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
> cleanEx()
> nameEx("image")
> ### * image
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: image
> ### Title: Display a Color Image
> ### Aliases: image image.default
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
> require(grDevices) # for colours
1089
> x <- y <- seq(-4*pi, 4*pi, len = 27)
1090
> r <- sqrt(outer(x^2, y^2, "+"))
1091
> image(z = z <- cos(r^2)*exp(-r/6), col  = gray((0:32)/32))
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
> image(z, axes = FALSE, main = "Math can be beautiful ...",
+       xlab = expression(cos(r^2) * e^{-r/6}))
> contour(z, add = TRUE, drawlabels = FALSE)
> 
> # Volcano data visualized as matrix. Need to transpose and flip
> # matrix horizontally.
> image(t(volcano)[ncol(volcano):1,])
> 
> # A prettier display of the volcano
> x <- 10*(1:nrow(volcano))
> y <- 10*(1:ncol(volcano))
> image(x, y, volcano, col = terrain.colors(100), axes = FALSE)
> contour(x, y, volcano, levels = seq(90, 200, by = 5),
+         add = TRUE, col = "peru")
> axis(1, at = seq(100, 800, by = 100))
> axis(2, at = seq(100, 600, by = 100))
> box()
> title(main = "Maunga Whau Volcano", font.main = 4)
> 
> 
> 
> cleanEx()
> nameEx("layout")
> ### * layout
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: layout
> ### Title: Specifying Complex Plot Arrangements
> ### Aliases: layout layout.show lcm
> ### Keywords: iplot dplot environment
> 
> ### ** Examples
> 
> def.par <- par(no.readonly = TRUE) # save default, for resetting...
> 
> ## divide the device into two rows and two columns
> ## allocate figure 1 all of row 1
> ## allocate figure 2 the intersection of column 2 and row 2
> layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE))
> ## show the regions that have been allocated to each plot
> layout.show(2)
> 
> ## divide device into two rows and two columns
> ## allocate figure 1 and figure 2 as above
> ## respect relations between widths and heights
1138
> nf <- layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE), respect = TRUE)
1139 1140 1141
> layout.show(nf)
> 
> ## create single figure which is 5cm square
1142
> nf <- layout(matrix(1), widths = lcm(5), heights = lcm(5))
1143 1144 1145 1146 1147 1148 1149
> layout.show(nf)
> 
> 
> ##-- Create a scatterplot with marginal histograms -----
> 
> x <- pmin(3, pmax(-3, stats::rnorm(50)))
> y <- pmin(3, pmax(-3, stats::rnorm(50)))
1150 1151
> xhist <- hist(x, breaks = seq(-3,3,0.5), plot = FALSE)
> yhist <- hist(y, breaks = seq(-3,3,0.5), plot = FALSE)
1152
> top <- max(c(xhist$counts, yhist$counts))
1153 1154 1155
> xrange <- c(-3, 3)
> yrange <- c(-3, 3)
> nf <- layout(matrix(c(2,0,1,3),2,2,byrow = TRUE), c(3,1), c(1,3), TRUE)
1156 1157
> layout.show(nf)
> 
1158 1159 1160 1161 1162 1163
> par(mar = c(3,3,1,1))
> plot(x, y, xlim = xrange, ylim = yrange, xlab = "", ylab = "")
> par(mar = c(0,3,1,1))
> barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0)
> par(mar = c(3,0,1,1))
> barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE)
1164
> 
1165
> par(def.par)  #- reset to default
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("legend")
> ### * legend
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: legend
> ### Title: Add Legends to Plots
> ### Aliases: legend
> ### Keywords: aplot
> 
> ### ** Examples
> 
> ## Run the example in '?matplot' or the following:
> leg.txt <- c("Setosa     Petals", "Setosa     Sepals",
+              "Versicolor Petals", "Versicolor Sepals")
> y.leg <- c(4.5, 3, 2.1, 1.4, .7)
> cexv  <- c(1.2, 1, 4/5, 2/3, 1/2)
1188
> matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width",
1189 1190
+         main = "Petal and Sepal Dimensions in Iris Blossoms")
> for (i in seq(cexv)) {
1191
+   text  (1, y.leg[i] - 0.1, paste("cex=", formatC(cexv[i])), cex = 0.8, adj = 0)
1192 1193
+   legend(3, y.leg[i], leg.txt, pch = "sSvV", col = c(1, 3), cex = cexv[i])
+ }
1194 1195
> ## cex *vector* [in R <= 3.5.1 has 'if(xc < 0)' w/ length(xc) == 2]
> legend(6,1, leg.txt, pch = "sSvV", col = c(1, 3), cex = 1+(-1:2)/8)
1196 1197 1198 1199 1200 1201
> 
> ## 'merge = TRUE' for merging lines & points:
> x <- seq(-pi, pi, len = 65)
> plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
> points(x, cos(x), pch = 3, col = 4)
> lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
1202
> title("legend(..., lty = c(2, -1, 1), pch = c(NA, 3, 4), merge = TRUE)",
1203
+       cex.main = 1.1)
1204
> legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
1205
+        text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
1206
+        merge = TRUE, bg = "gray90")
1207 1208 1209
> 
> ## right-justifying a set of labels: thanks to Uwe Ligges
> x <- 1:5; y1 <- 1/x; y2 <- 2/x
1210 1211
> plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y")
> lines(x, y1); lines(x, y2, lty = 2)
1212 1213 1214 1215 1216
> temp <- legend("topright", legend = c(" ", " "),
+                text.width = strwidth("1,000,000"),
+                lty = 1:2, xjust = 1, yjust = 1,
+                title = "Line Types")
> text(temp$rect$left + temp$rect$w, temp$text$y,
1217
+      c("1,000", "1,000,000"), pos = 2)
1218 1219 1220 1221 1222
> 
> 
> ##--- log scaled Examples ------------------------------
> leg.txt <- c("a one", "a two")
> 
1223
> par(mfrow = c(2, 2))
1224
> for(ll in c("","x","y","xy")) {
1225 1226 1227 1228 1229 1230 1231
+   plot(2:10, log = ll, main = paste0("log = '", ll, "'"))
+   abline(1, 1)
+   lines(2:3, 3:4, col = 2)
+   points(2, 2, col = 3)
+   rect(2, 3, 3, 2, col = 4)
+   text(c(3,3), 2:3, c("rect(2,3,3,2, col=4)",
+                       "text(c(3,3),2:3,\"c(rect(...)\")"), adj = c(0, 0.3))
1232 1233
+   legend(list(x = 2,y = 8), legend = leg.txt, col = 2:3, pch = 1:2,
+          lty = 1, merge = TRUE)   #, trace = TRUE)
1234
+ }
1235
> par(mfrow = c(1,1))
1236 1237 1238
> 
> ##-- Math expressions:  ------------------------------
> x <- seq(-pi, pi, len = 65)
1239
> plot(x, sin(x), type = "l", col = 2, xlab = expression(phi),
1240
+      ylab = expression(f(phi)))
1241
> abline(h = -1:1, v = pi/2*(-6:6), col = "gray90")
1242
> lines(x, cos(x), col = 3, lty = 2)
1243 1244 1245
> ex.cs1 <- expression(plain(sin) * phi,  paste("cos", phi))  # 2 ways
> utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE,
+            adj = c(0, 0.6)))  # adj y !
1246 1247 1248 1249 1250 1251 1252 1253 1254
List of 2
 $ rect:List of 4
  ..$ w   : num 1.2
  ..$ h   : num 0.251
  ..$ left: num -3
  ..$ top : num 0.9
 $ text:List of 2
  ..$ x: num [1:2] -2.29 -2.29
  ..$ y: num [1:2] 0.816 0.733
1255
> legend(-3, 0.9, ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6))
1256 1257 1258 1259
> 
> require(stats)
> x <- rexp(100, rate = .5)
> hist(x, main = "Mean and Median of a Skewed Distribution")
1260 1261 1262 1263 1264
> abline(v = mean(x),   col = 2, lty = 2, lwd = 2)
> abline(v = median(x), col = 3, lty = 3, lwd = 2)
> ex12 <- expression(bar(x) == sum(over(x[i], n), i == 1, n),
+                    hat(x) == median(x[i], i == 1, n))
> utils::str(legend(4.1, 30, ex12, col = 2:3, lty = 2:3, lwd = 2))
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
List of 2
 $ rect:List of 4
  ..$ w   : num 4.27
  ..$ h   : num 6.78
  ..$ left: num 4.1
  ..$ top : num 30
 $ text:List of 2
  ..$ x: num [1:2] 5.22 5.22
  ..$ y: num [1:2] 27.3 24.6
> 
> ## 'Filled' boxes -- for more, see example(plot.factor)
1276
> op <- par(bg = "white") # to get an opaque box for the legend
1277 1278 1279 1280 1281 1282 1283 1284
> plot(cut(weight, 3) ~ group, data = PlantGrowth, col = NULL,
+      density = 16*(1:3))
> par(op)
> 
> ## Using 'ncol' :
> x <- 0:64/64
> matplot(x, outer(x, 1:7, function(x, k) sin(k * pi * x)),
+         type = "o", col = 1:7, ylim = c(-1, 1.5), pch = "*")
1285 1286
> op <- par(bg = "antiquewhite1")
> legend(0, 1.5, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
1287
+        pch = "*", ncol = 4, cex = 0.8)
1288
> legend(.8,1.2, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
1289
+        pch = "*", cex = 0.8)
1290
> legend(0, -.1, paste("sin(", 1:4, "pi * x)"), col = 1:4, lty = 1:4,
1291
+        ncol = 2, cex = 0.8)
1292
> legend(0, -.4, paste("sin(", 5:7, "pi * x)"), col = 4:6,  pch = 24,
1293 1294 1295 1296 1297
+        ncol = 2, cex = 1.5, lwd = 2, pt.bg = "pink", pt.cex = 1:3)
> par(op)
> 
> ## point covering line :
> y <- sin(3*pi*x)
1298
> plot(x, y, type = "l", col = "blue",
1299
+     main = "points with bg & legend(*, pt.bg)")
1300 1301
> points(x, y, pch = 21, bg = "white")
> legend(.4,1, "sin(c x)", pch = 21, pt.bg = "white", lty = 1, col = "blue")
1302 1303
> 
> ## legends with titles at different locations
1304
> plot(x, y, type = "n")
1305 1306 1307 1308 1309 1310 1311 1312 1313
> legend("bottomright", "(x,y)", pch=1, title= "bottomright")
> legend("bottom",      "(x,y)", pch=1, title= "bottom")
> legend("bottomleft",  "(x,y)", pch=1, title= "bottomleft")
> legend("left",        "(x,y)", pch=1, title= "left")
> legend("topleft",     "(x,y)", pch=1, title= "topleft, inset = .05", inset = .05)
> legend("top",         "(x,y)", pch=1, title= "top")
> legend("topright",    "(x,y)", pch=1, title= "topright, inset = .02",inset = .02)
> legend("right",       "(x,y)", pch=1, title= "right")
> legend("center",      "(x,y)", pch=1, title= "center")
1314
> 
1315
> # using text.font (and text.col):
1316
> op <- par(mfrow = c(2, 2), mar = rep(2.1, 4))
1317 1318
> c6 <- terrain.colors(10)[1:6]
> for(i in 1:4) {
1319
+    plot(1, type = "n", axes = FALSE, ann = FALSE); title(paste("text.font =",i))
1320
+    legend("top", legend = LETTERS[1:6], col = c6,
1321
+           ncol = 2, cex = 2, lwd = 3, text.font = i, text.col = c6)
1322 1323 1324
+ }
> par(op)
> 
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("lines")
> ### * lines
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: lines
> ### Title: Add Connected Line Segments to a Plot
> ### Aliases: lines lines.default
> ### Keywords: aplot
> 
> ### ** Examples
> 
> # draw a smooth line through a scatter plot
1342
> plot(cars, main = "Stopping Distance versus Speed")
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
> lines(stats::lowess(cars))
> 
> 
> 
> cleanEx()
> nameEx("matplot")
> ### * matplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: matplot
> ### Title: Plot Columns of Matrices
> ### Aliases: matplot matpoints matlines
> ### Keywords: hplot aplot array
> 
> ### ** Examples
> 
> require(grDevices)
> matplot((-4:5)^2, main = "Quadratic") # almost identical to plot(*)
> sines <- outer(1:20, 1:4, function(x, y) sin(x / 20 * pi * y))
> matplot(sines, pch = 1:4, type = "o", col = rainbow(ncol(sines)))
> matplot(sines, type = "b", pch = 21:23, col = 2:5, bg = 2:5,
+         main = "matplot(...., pch = 21:23, bg = 2:5)")
> 
> x <- 0:50/50
> matplot(x, outer(x, 1:8, function(x, k) sin(k*pi * x)),
+         ylim = c(-2,2), type = "plobcsSh",
+         main= "matplot(,type = \"plobcsSh\" )")
> ## pch & type =  vector of 1-chars :
> matplot(x, outer(x, 1:4, function(x, k) sin(k*pi * x)),
+         pch = letters[1:4], type = c("b","p","o"))
> 
> lends <- c("round","butt","square")
> matplot(matrix(1:12, 4), type="c", lty=1, lwd=10, lend=lends)
> text(cbind(2.5, 2*c(1,3,5)-.4), lends, col= 1:3, cex = 1.5)
> 
> table(iris$Species) # is data.frame with 'Species' factor

    setosa versicolor  virginica 
        50         50         50 
> iS <- iris$Species == "setosa"
> iV <- iris$Species == "versicolor"
> op <- par(bg = "bisque")
1386
> matplot(c(1, 8), c(0, 4.5), type =  "n", xlab = "Length", ylab = "Width",
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
+         main = "Petal and Sepal Dimensions in Iris Blossoms")
> matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = "sS", col = c(2,4))
> matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = "vV", col = c(2,4))
> legend(1, 4, c("    Setosa Petals", "    Setosa Sepals",
+                "Versicolor Petals", "Versicolor Sepals"),
+        pch = "sSvV", col = rep(c(2,4), 2))
> 
> nam.var <- colnames(iris)[-5]
> nam.spec <- as.character(iris[1+50*0:2, "Species"])
> iris.S <- array(NA, dim = c(50,4,3),
+                 dimnames = list(NULL, nam.var, nam.spec))
> for(i in 1:3) iris.S[,,i] <- data.matrix(iris[1:50+50*(i-1), -5])
> 
1400 1401
> matplot(iris.S[, "Petal.Length",], iris.S[, "Petal.Width",], pch = "SCV",
+         col = rainbow(3, start = 0.8, end = 0.1),
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
+         sub = paste(c("S", "C", "V"), dimnames(iris.S)[[3]],
+                     sep = "=", collapse= ",  "),
+         main = "Fisher's Iris Data")
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("mosaicplot")
> ### * mosaicplot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: mosaicplot
> ### Title: Mosaic Plots
> ### Aliases: mosaicplot mosaicplot.default mosaicplot.formula
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(stats)
> mosaicplot(Titanic, main = "Survival on the Titanic", color = TRUE)
> ## Formula interface for tabulated data:
> mosaicplot(~ Sex + Age + Survived, data = Titanic, color = TRUE)
> 
> mosaicplot(HairEyeColor, shade = TRUE)
> ## Independence model of hair and eye color and sex.  Indicates that
1430
> ## there are more blue eyed blonde females than expected in the case
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
> ## of independence and too few brown eyed blonde females.
> ## The corresponding model is:
> fm <- loglin(HairEyeColor, list(1, 2, 3))
2 iterations: deviation 5.684342e-14 
> pchisq(fm$pearson, fm$df, lower.tail = FALSE)
[1] 5.320872e-23
> 
> mosaicplot(HairEyeColor, shade = TRUE, margin = list(1:2, 3))
> ## Model of joint independence of sex from hair and eye color.  Males
> ## are underrepresented among people with brown hair and eyes, and are
> ## overrepresented among people with brown hair and blue eyes.
> ## The corresponding model is:
> fm <- loglin(HairEyeColor, list(1:2, 3))
2 iterations: deviation 5.684342e-14 
> pchisq(fm$pearson, fm$df, lower.tail = FALSE)
[1] 0.1891745
> 
> ## Formula interface for raw data: visualize cross-tabulation of numbers
> ## of gears and carburettors in Motor Trend car data.
> mosaicplot(~ gear + carb, data = mtcars, color = TRUE, las = 1)
> # color recycling
> mosaicplot(~ gear + carb, data = mtcars, color = 2:3, las = 1)
> 
> 
> 
> cleanEx()
> nameEx("mtext")
> ### * mtext
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: mtext
> ### Title: Write Text into the Margins of a Plot
> ### Aliases: mtext
> ### Keywords: aplot
> 
> ### ** Examples
> 
1469
> plot(1:10, (-4:5)^2, main = "Parabola Points", xlab = "xlab")
1470 1471
> mtext("10 of them")
> for(s in 1:4)
1472
+     mtext(paste("mtext(..., line= -1, {side, col, font} = ", s,
1473
+           ", cex = ", (1+s)/2, ")"), line = -1,
1474
+           side = s, col = s, font = s, cex = (1+s)/2)
1475
> mtext("mtext(..., line= -2)", line = -2)
1476
> mtext("mtext(..., line= -2, adj = 0)", line = -2, adj = 0)
1477
> ##--- log axis :
1478 1479
> plot(1:10, exp(1:10), log = "y", main = "log =\"y\"", xlab = "xlab")
> for(s in 1:4) mtext(paste("mtext(...,side=", s ,")"), side = s)
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498
> 
> 
> 
> cleanEx()
> nameEx("pairs")
> ### * pairs
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: pairs
> ### Title: Scatterplot Matrices
> ### Aliases: pairs pairs.default pairs.formula
> ### Keywords: hplot
> 
> ### ** Examples
> 
> pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",
+       pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])
> 
1499 1500
> ## formula method, "graph" layout (row 1 at bottom):
> pairs(~ Fertility + Education + Catholic, data = swiss, row1attop=FALSE,
1501 1502
+       subset = Education < 20, main = "Swiss data, Education < 20")
> 
1503
> pairs(USJudgeRatings, gap=1/10) # (gap: not wasting plotting area)
1504 1505
> ## show only lower triangle (and suppress labeling for whatever reason):
> pairs(USJudgeRatings, text.panel = NULL, upper.panel = NULL)
1506 1507 1508 1509 1510 1511 1512 1513 1514
> 
> ## put histograms on the diagonal
> panel.hist <- function(x, ...)
+ {
+     usr <- par("usr"); on.exit(par(usr))
+     par(usr = c(usr[1:2], 0, 1.5) )
+     h <- hist(x, plot = FALSE)
+     breaks <- h$breaks; nB <- length(breaks)
+     y <- h$counts; y <- y/max(y)
1515
+     rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
1516
+ }
1517 1518 1519
> pairs(USJudgeRatings[1:5], panel = panel.smooth,
+       cex = 1.5, pch = 24, bg = "light blue",
+       diag.panel = panel.hist, cex.labels = 2, font.labels = 2)
1520 1521 1522
> 
> ## put (absolute) correlations on the upper panels,
> ## with size proportional to the correlations.
1523
> panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
1524 1525 1526 1527
+ {
+     usr <- par("usr"); on.exit(par(usr))
+     par(usr = c(0, 1, 0, 1))
+     r <- abs(cor(x, y))
1528
+     txt <- format(c(r, 0.123456789), digits = digits)[1]
1529
+     txt <- paste0(prefix, txt)
1530 1531 1532
+     if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
+     text(0.5, 0.5, txt, cex = cex.cor * r)
+ }
1533 1534
> pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor,
+       gap=0, row1attop=FALSE)
1535
> 
1536
> pairs(iris[-5], log = "xy") # plot all variables on log scale
1537 1538
> pairs(iris, log = 1:4, # log the first four
+       main = "Lengths and Widths in [log]", line.main=1.5, oma=c(2,2,3,2))
1539
> 
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("panel.smooth")
> ### * panel.smooth
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: panel.smooth
> ### Title: Simple Panel Plot
> ### Aliases: panel.smooth
> ### Keywords: hplot dplot
> 
> ### ** Examples
> 
1556 1557
> pairs(swiss, panel = panel.smooth, pch = ".")  # emphasize the smooths
> pairs(swiss, panel = panel.smooth, lwd = 2, cex = 1.5, col = "blue")  # hmm...
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
> 
> 
> 
> cleanEx()
> nameEx("par")
> ### * par
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: par
> ### Title: Set or Query Graphical Parameters
1569
> ### Aliases: par .Pars 'graphical parameter' 'graphical parameters'
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
> ### Keywords: iplot dplot environment
> 
> ### ** Examples
> 
> op <- par(mfrow = c(2, 2), # 2 x 2 pictures on one plot
+           pty = "s")       # square plotting region,
>                            # independent of device size
> 
> ## At end of plotting, reset to previous settings:
> par(op)
> 
> ## Alternatively,
> op <- par(no.readonly = TRUE) # the whole list of settable par's.
> ## do lots of plotting and par(.) calls, then reset:
> par(op)
> ## Note this is not in general good practice
> 
> par("ylog") # FALSE
[1] FALSE
> plot(1 : 12, log = "y")
> par("ylog") # TRUE
[1] TRUE
> 
> plot(1:2, xaxs = "i") # 'inner axis' w/o extra space
> par(c("usr", "xaxp"))
$usr
[1] 1.00 2.00 0.96 2.04

$xaxp
[1] 1 2 5

> 
> ( nr.prof <-
1603 1604 1605
+ c(prof.pilots = 16, lawyers = 11, farmers = 10, salesmen = 9, physicians = 9,
+   mechanics = 6, policemen = 6, managers = 6, engineers = 5, teachers = 4,
+   housewives = 3, students = 3, armed.forces = 1))
1606 1607 1608 1609 1610 1611 1612 1613
 prof.pilots      lawyers      farmers     salesmen   physicians    mechanics 
          16           11           10            9            9            6 
   policemen     managers    engineers     teachers   housewives     students 
           6            6            5            4            3            3 
armed.forces 
           1 
> par(las = 3)
> barplot(rbind(nr.prof)) # R 0.63.2: shows alignment problem
1614
> par(las = 0)  # reset to default
1615 1616 1617
> 
> require(grDevices) # for gray
> ## 'fg' use:
1618 1619
> plot(1:12, type = "b", main = "'fg' : axes, ticks and box in gray",
+      fg = gray(0.7), bty = "7" , sub = R.version.string)
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
> 
> ex <- function() {
+    old.par <- par(no.readonly = TRUE) # all par settings which
+                                       # could be changed.
+    on.exit(par(old.par))
+    ## ...
+    ## ... do lots of par() settings and plots
+    ## ...
+    invisible() #-- now,  par(old.par)  will be executed
+ }
> ex()
> 
1632 1633 1634 1635 1636 1637 1638
> ## Line types
> showLty <- function(ltys, xoff = 0, ...) {
+    stopifnot((n <- length(ltys)) >= 1)
+    op <- par(mar = rep(.5,4)); on.exit(par(op))
+    plot(0:1, 0:1, type = "n", axes = FALSE, ann = FALSE)
+    y <- (n:1)/(n+1)
+    clty <- as.character(ltys)
1639 1640
+    mytext <- function(x, y, txt)
+       text(x, y, txt, adj = c(0, -.3), cex = 0.8, ...)
1641 1642
+    abline(h = y, lty = ltys, ...); mytext(xoff, y, clty)
+    y <- y - 1/(3*(n+1))
1643 1644
+    abline(h = y, lty = ltys, lwd = 2, ...)
+    mytext(1/8+xoff, y, paste(clty," lwd = 2"))
1645 1646 1647 1648 1649 1650
+ }
> showLty(c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"))
> par(new = TRUE)  # the same:
> showLty(c("solid", "44", "13", "1343", "73", "2262"), xoff = .2, col = 2)
> showLty(c("11", "22", "33", "44",   "12", "13", "14",   "21", "31"))
> 
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("persp")
> ### * persp
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: persp
> ### Title: Perspective Plots
> ### Aliases: persp persp.default
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
> require(grDevices) # for trans3d
> ## More examples in  demo(persp) !!
> ##                   -----------
> 
> # (1) The Obligatory Mathematical surface.
> #     Rotated sinc function.
> 
> x <- seq(-10, 10, length= 30)
> y <- x
1676
> f <- function(x, y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
> z <- outer(x, y, f)
> z[is.na(z)] <- 1
> op <- par(bg = "white")
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
+       ltheta = 120, shade = 0.75, ticktype = "detailed",
+       xlab = "X", ylab = "Y", zlab = "Sinc( r )"
+ ) -> res
> round(res, 3)
      [,1]   [,2]   [,3]   [,4]
[1,] 0.087 -0.025  0.043 -0.043
[2,] 0.050  0.043 -0.075  0.075
[3,] 0.000  0.074  0.042 -0.042
[4,] 0.000 -0.273 -2.890  3.890
> 
> # (2) Add to existing persp plot - using trans3d() :
> 
> xE <- c(-10,10); xy <- expand.grid(xE, xE)
1695 1696
> points(trans3d(xy[,1], xy[,2], 6, pmat = res), col = 2, pch = 16)
> lines (trans3d(x, y = 10, z = 6 + sin(x), pmat = res), col = 3)
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
> 
> phi <- seq(0, 2*pi, len = 201)
> r1 <- 7.725 # radius of 2nd maximum
> xr <- r1 * cos(phi)
> yr <- r1 * sin(phi)
> lines(trans3d(xr,yr, f(xr,yr), res), col = "pink", lwd = 2)
> ## (no hidden lines)
> 
> # (3) Visualizing a simple DEM model
> 
> z <- 2 * volcano        # Exaggerate the relief
> x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
> y <- 10 * (1:ncol(z))   # 10 meter spacing (E to W)
> ## Don't draw the grid lines :  border = NA
> par(bg = "slategray")
> persp(x, y, z, theta = 135, phi = 30, col = "green3", scale = FALSE,
+       ltheta = -120, shade = 0.75, border = NA, box = FALSE)
> 
> # (4) Surface colours corresponding to z-values
> 
> par(bg = "white")
> x <- seq(-1.95, 1.95, length = 30)
> y <- seq(-1.95, 1.95, length = 35)
1720
> z <- outer(x, y, function(a, b) a*b^2)
1721 1722 1723
> nrz <- nrow(z)
> ncz <- ncol(z)
> # Create a function interpolating colors in the range of specified colors
1724
> jet.colors <- colorRampPalette( c("blue", "green") )
1725 1726 1727 1728 1729 1730 1731
> # Generate the desired number of colors from this palette
> nbcol <- 100
> color <- jet.colors(nbcol)
> # Compute the z-value at the facet centres
> zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
> # Recode facet z-values into color indices
> facetcol <- cut(zfacet, nbcol)
1732
> persp(x, y, z, col = color[facetcol], phi = 30, theta = -30)
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758
> 
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("pie")
> ### * pie
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: pie
> ### Title: Pie Charts
> ### Aliases: pie
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(grDevices)
> pie(rep(1, 24), col = rainbow(24), radius = 0.9)
> 
> pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
> names(pie.sales) <- c("Blueberry", "Cherry",
+     "Apple", "Boston Cream", "Other", "Vanilla Cream")
> pie(pie.sales) # default colours
1759 1760
> pie(pie.sales, col = c("purple", "violetred1", "green3",
+                        "cornsilk", "cyan", "white"))
1761
> pie(pie.sales, col = gray(seq(0.4, 1.0, length = 6)))
1762
> pie(pie.sales, density = 10, angle = 15 + 10 * 1:6)
1763 1764 1765
> pie(pie.sales, clockwise = TRUE, main = "pie(*, clockwise = TRUE)")
> segments(0, 0, 0, 1, col = "red", lwd = 2)
> text(0, 1, "init.angle = 90", col = "red")
1766 1767
> 
> n <- 200
1768
> pie(rep(1, n), labels = "", col = rainbow(n), border = NA,
1769 1770
+     main = "pie(*, labels=\"\", col=rainbow(n), border=NA,..")
> 
1771 1772
> ## Another case showing pie() is rather fun than science:
> ## (original by FinalBackwardsGlance on http://imgur.com/gallery/wWrpU4X)
1773 1774 1775
> pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5),
+     init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE)
> 
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
> 
> 
> cleanEx()
> nameEx("plot")
> ### * plot
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot
> ### Title: Generic X-Y Plotting
> ### Aliases: plot
> ### Keywords: hplot
> 
> ### ** Examples
> 
1791
> require(stats) # for lowess, rpois, rnorm
1792 1793 1794
> plot(cars)
> lines(lowess(cars))
> 
1795
> plot(sin, -pi, 2*pi) # see ?plot.function
1796 1797
> 
> ## Discrete Distribution Plot:
1798 1799
> plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
+      main = "rpois(100, lambda = 5)")
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819
> 
> ## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
> plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
> points(x, cex = .5, col = "dark red")
> 
> 
> 
> cleanEx()
> nameEx("plot.dataframe")
> ### * plot.dataframe
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.data.frame
> ### Title: Plot Method for Data Frames
> ### Aliases: plot.data.frame
> ### Keywords: hplot methods
> 
> ### ** Examples
> 
1820
> plot(OrchardSprays[1], method = "jitter")
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844
> plot(OrchardSprays[c(4,1)])
> plot(OrchardSprays)
> 
> plot(iris)
> plot(iris[5:4])
> plot(women)
> 
> 
> 
> cleanEx()
> nameEx("plot.default")
> ### * plot.default
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.default
> ### Title: The Default Scatterplot Function
> ### Aliases: plot.default
> ### Keywords: hplot
> 
> ### ** Examples
> 
> Speed <- cars$speed
> Distance <- cars$dist
1845
> plot(Speed, Distance, panel.first = grid(8, 8),
1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
+      pch = 0, cex = 1.2, col = "blue")
> plot(Speed, Distance,
+      panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
+      pch = 0, cex = 1.2, col = "blue")
> 
> ## Show the different plot types
> x <- 0:12
> y <- sin(pi/5 * x)
> op <- par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))
> for (tp in c("p","l","b",  "c","o","h",  "s","S","n")) {
1856
+    plot(y ~ x, type = tp, main = paste0("plot(*, type = \"", tp, "\")"))
1857
+    if(tp == "S") {
1858
+       lines(x, y, type = "s", col = "red", lty = 2)
1859
+       mtext("lines(*, type = \"s\", ...)", col = "red", cex = 0.8)
1860 1861 1862 1863 1864
+    }
+ }
> par(op)
> 
> ##--- Log-Log Plot  with  custom axes
1865
> lx <- seq(1, 5, length = 41)
1866 1867
> yl <- expression(e^{-frac(1,2) * {log[10](x)}^2})
> y <- exp(-.5*lx^2)
1868
> op <- par(mfrow = c(2,1), mar = par("mar")-c(1,0,2,0), mgp = c(2, .7, 0))
1869 1870 1871 1872
> plot(10^lx, y, log = "xy", type = "l", col = "purple",
+      main = "Log-Log plot", ylab = yl, xlab = "x")
> plot(10^lx, y, log = "xy", type = "o", pch = ".", col = "forestgreen",
+      main = "Log-Log plot with custom axes", ylab = yl, xlab = "x",
1873 1874
+      axes = FALSE, frame.plot = TRUE)
> my.at <- 10^(1:5)
1875
> axis(1, at = my.at, labels = formatC(my.at, format = "fg"))
1876 1877 1878
> e.y <- -5:-1 ; at.y <- 10^e.y
> axis(2, at = at.y, col.axis = "red", las = 1,
+      labels = as.expression(lapply(e.y, function(E) bquote(10^.(E)))))
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("plot.design")
> ### * plot.design
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.design
> ### Title: Plot Univariate Effects of a Design or Model
> ### Aliases: plot.design
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(stats)
1898
> plot.design(warpbreaks)  # automatic for data frame with one numeric var.
1899 1900 1901
> 
> Form <- breaks ~ wool + tension
> summary(fm1 <- aov(Form, data = warpbreaks))
1902 1903 1904 1905
            Df Sum Sq Mean Sq F value  Pr(>F)   
wool         1    451   450.7   3.339 0.07361 . 
tension      2   2034  1017.1   7.537 0.00138 **
Residuals   50   6748   135.0                   
1906
---
1907
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
1908
> plot.design(       Form, data = warpbreaks, col = 2)  # same as above
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
> 
> ## More than one y :
> utils::str(esoph)
'data.frame':	88 obs. of  5 variables:
 $ agegp    : Ord.factor w/ 6 levels "25-34"<"35-44"<..: 1 1 1 1 1 1 1 1 1 1 ...
 $ alcgp    : Ord.factor w/ 4 levels "0-39g/day"<"40-79"<..: 1 1 1 1 2 2 2 2 3 3 ...
 $ tobgp    : Ord.factor w/ 4 levels "0-9g/day"<"10-19"<..: 1 2 3 4 1 2 3 4 1 2 ...
 $ ncases   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ ncontrols: num  40 10 6 5 27 7 4 7 2 1 ...
> plot.design(esoph) ## two plots; if interactive you are "ask"ed
> 
> ## or rather, compare mean and median:
> op <- par(mfcol = 1:2)
> plot.design(ncases/ncontrols ~ ., data = esoph, ylim = c(0, 0.8))
> plot.design(ncases/ncontrols ~ ., data = esoph, ylim = c(0, 0.8),
+             fun = median)
> par(op)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("plot.factor")
> ### * plot.factor
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.factor
> ### Title: Plotting Factor Variables
> ### Aliases: plot.factor
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(grDevices)
1944 1945
> plot(weight ~ group, data = PlantGrowth)           # numeric vector ~ factor
> plot(cut(weight, 2) ~ group, data = PlantGrowth)   # factor ~ factor
1946 1947 1948 1949
> ## passing "..." to spineplot() eventually:
> plot(cut(weight, 3) ~ group, data = PlantGrowth,
+      col = hcl(c(0, 120, 240), 50, 70))
> 
1950
> plot(PlantGrowth$group, axes = FALSE, main = "no axes")  # extremely silly
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
> 
> 
> 
> cleanEx()
> nameEx("plot.formula")
> ### * plot.formula
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.formula
> ### Title: Formula Notation for Scatterplots
> ### Aliases: plot.formula lines.formula points.formula text.formula
> ### Keywords: hplot aplot
> 
> ### ** Examples
> 
1967 1968 1969
> op <- par(mfrow = c(2,1))
> plot(Ozone ~ Wind, data = airquality, pch = as.character(Month))
> plot(Ozone ~ Wind, data = airquality, pch = as.character(Month),
1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
+      subset = Month != 7)
> par(op)
> 
> ## text.formula() can be very natural:
> wb <- within(warpbreaks, {
+     time <- seq_along(breaks); W.T <- wool:tension })
> plot(breaks ~ time, data = wb, type = "b")
> text(breaks ~ time, data = wb, label = W.T, col = 1+as.integer(wool))
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
> nameEx("plot.raster")
> ### * plot.raster
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.raster
> ### Title: Plotting Raster Images
> ### Aliases: plot.raster
> ### Keywords: hplot
> 
> ### ** Examples
> 
> require(grDevices)
> r <- as.raster(c(0.5, 1, 0.5))
> plot(r)
> # additional arguments to rasterImage()
> plot(r, interpolate=FALSE)
> # distort
> plot(r, asp=NA)
> # fill page
> op <- par(mar=rep(0, 4))
> plot(r, asp=NA)
> par(op)
> # normal annotations work
> plot(r, asp=NA)
> box()
> title(main="This is my raster")
> # add to existing plot
> plot(1)
> plot(r, add=TRUE)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062
> nameEx("plot.table")
> ### * plot.table
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.table
> ### Title: Plot Methods for 'table' Objects
> ### Aliases: plot.table lines.table points.table
> ### Keywords: hplot category
> 
> ### ** Examples
> 
> ## 1-d tables
> (Poiss.tab <- table(N = stats::rpois(200, lambda = 5)))
N
 1  2  3  4  5  6  7  8  9 10 11 
 4 14 25 38 40 33 21 16  4  2  3 
> plot(Poiss.tab, main = "plot(table(rpois(200, lambda = 5)))")
> 
> plot(table(state.division))
> 
> ## 4-D :
> plot(Titanic, main ="plot(Titanic, main= *)")
> 
> 
> 
> 
> cleanEx()
> nameEx("plot.window")
> ### * plot.window
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.window
> ### Title: Set up World Coordinates for Graphics Window
> ### Aliases: plot.window xlim ylim asp
> ### Keywords: aplot
> 
> ### ** Examples
> 
> ##--- An example for the use of 'asp' :
> require(stats)  # normally loaded
> loc <- cmdscale(eurodist)
> rx <- range(x <- loc[,1])
> ry <- range(y <- -loc[,2])
2063
> plot(x, y, type = "n", asp = 1, xlab = "", ylab = "")
2064
> abline(h = pretty(rx, 10), v = pretty(ry, 10), col = "lightgray")
2065
> text(x, y, labels(eurodist), cex = 0.8)
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084
> 
> 
> 
> cleanEx()
> nameEx("plot.xy")
> ### * plot.xy
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.xy
> ### Title: Basic Internal Plot Function
> ### Aliases: plot.xy
> ### Keywords: aplot
> 
> ### ** Examples
> 
> points.default # to see how it calls "plot.xy(xy.coords(x, y), ...)"
function (x, y = NULL, type = "p", ...) 
plot.xy(xy.coords(x, y), type = type, ...)
2085
<bytecode: 0x1a0dd78>
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148
<environment: namespace:graphics>
> 
> 
> 
> cleanEx()
> nameEx("plothistogram")
> ### * plothistogram
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: plot.histogram
> ### Title: Plot Histograms
> ### Aliases: plot.histogram lines.histogram
> ### Keywords: hplot iplot
> 
> ### ** Examples
> 
> (wwt <- hist(women$weight, nclass = 7, plot = FALSE))
$breaks
 [1] 115 120 125 130 135 140 145 150 155 160 165

$counts
 [1] 3 1 2 2 1 1 2 1 1 1

$density
 [1] 0.04000000 0.01333333 0.02666667 0.02666667 0.01333333 0.01333333
 [7] 0.02666667 0.01333333 0.01333333 0.01333333

$mids
 [1] 117.5 122.5 127.5 132.5 137.5 142.5 147.5 152.5 157.5 162.5

$xname
[1] "women$weight"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
> plot(wwt, labels = TRUE) # default main & xlab using wwt$xname
> plot(wwt, border = "dark blue", col = "light blue",
+      main = "Histogram of 15 women's weights", xlab = "weight [pounds]")
> 
> ## Fake "lines" example, using non-default labels:
> w2 <- wwt; w2$counts <- w2$counts - 1
> lines(w2, col = "Midnight Blue", labels = ifelse(w2$counts, "> 1", "1"))
> 
> 
> 
> cleanEx()
> nameEx("points")
> ### * points
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: points
> ### Title: Add Points to a Plot
> ### Aliases: points points.default pch
> ### Keywords: aplot
> 
> ### ** Examples
> 
> require(stats) # for rnorm
2149
> plot(-4:4, -4:4, type = "n")  # setting up coord. system
2150 2151 2152 2153
> points(rnorm(200), rnorm(200), col = "red")
> points(rnorm(100)/2, rnorm(100)/2, col = "blue", cex = 1.5)
> 
> op <- par(bg = "light blue")
2154
> x <- seq(0, 2*pi, len = 51)
2155
> ## something "between type='b' and type='o'":
2156 2157
> plot(x, sin(x), type = "o", pch = 21, bg = par("bg"), col = "blue", cex = .6,
+  main = 'plot(..., type="o", pch=21, bg=par("bg"))')
2158 2159
> par(op)
> 
2160 2161
> ## Not run: 
> ##D ## The figure was produced by calls like
2162
> ##D png("pch.png", height = 0.7, width = 7, res = 100, units = "in")
2163
> ##D par(mar = rep(0,4))
2164
> ##D plot(c(-1, 26), 0:1, type = "n", axes = FALSE)
2165 2166 2167 2168
> ##D text(0:25, 0.6, 0:25, cex = 0.5)
> ##D points(0:25, rep(0.3, 26), pch = 0:25, bg = "grey")
> ## End(Not run)
> 
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
> ##-------- Showing all the extra & some char graphics symbols ---------
> pchShow <-
+   function(extras = c("*",".", "o","O","0","+","-","|","%","#"),
+            cex = 3, ## good for both .Device=="postscript" and "x11"
+            col = "red3", bg = "gold", coltext = "brown", cextext = 1.2,
+            main = paste("plot symbols :  points (...  pch = *, cex =",
+                         cex,")"))
+   {
+     nex <- length(extras)
+     np  <- 26 + nex
+     ipch <- 0:(np-1)
+     k <- floor(sqrt(np))
+     dd <- c(-1,1)/2
+     rx <- dd + range(ix <- ipch %/% k)
+     ry <- dd + range(iy <- 3 + (k-1)- ipch %% k)
+     pch <- as.list(ipch) # list with integers & strings
+     if(nex > 0) pch[26+ 1:nex] <- as.list(extras)
2186
+     plot(rx, ry, type = "n", axes  =  FALSE, xlab = "", ylab = "", main = main)
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
+     abline(v = ix, h = iy, col = "lightgray", lty = "dotted")
+     for(i in 1:np) {
+       pc <- pch[[i]]
+       ## 'col' symbols with a 'bg'-colored interior (where available) :
+       points(ix[i], iy[i], pch = pc, col = col, bg = bg, cex = cex)
+       if(cextext > 0)
+           text(ix[i] - 0.3, iy[i], pc, col = coltext, cex = cextext)
+     }
+   }
> 
> pchShow()
> pchShow(c("o","O","0"), cex = 2.5)
> pchShow(NULL, cex = 4, cextext = 0, main = NULL)
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("polygon")
> ### * polygon
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: polygon
> ### Title: Polygon Drawing
> ### Aliases: polygon
> ### Keywords: aplot
> 
> ### ** Examples
> 
2217 2218 2219 2220
> x <- c(1:9, 8:1)
> y <- c(1, 2*(5:3), 2, -1, 17, 9, 8, 2:9)
> op <- par(mfcol = c(3, 1))
> for(xpd in c(FALSE, TRUE, NA)) {
2221
+   plot(1:10, main = paste("xpd =", xpd))
2222 2223
+   box("figure", col = "pink", lwd = 3)
+   polygon(x, y, xpd = xpd, col = "orange", lty = 2, lwd = 2, border = "red")
2224 2225 2226 2227 2228
+ }
> par(op)
> 
> n <- 100
> xx <- c(0:n, n:0)
2229 2230 2231
> yy <- c(c(0, cumsum(stats::rnorm(n))), rev(c(0, cumsum(stats::rnorm(n)))))
> plot   (xx, yy, type = "n", xlab = "Time", ylab = "Distance")
> polygon(xx, yy, col = "gray", border = "red")
2232 2233 2234 2235
> title("Distance Between Brownian Motions")
> 
> # Multiple polygons from NA values
> # and recycling of col, border, and lty
2236 2237
> op <- par(mfrow = c(2, 1))
> plot(c(1, 9), 1:2, type = "n")
2238
> polygon(1:9, c(2,1,2,1,1,2,1,2,1),
2239 2240 2241 2242
+         col = c("red", "blue"),
+         border = c("green", "yellow"),
+         lwd = 3, lty = c("dashed", "solid"))
> plot(c(1, 9), 1:2, type = "n")
2243
> polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
2244 2245 2246
+         col = c("red", "blue"),
+         border = c("green", "yellow"),
+         lwd = 3, lty = c("dashed", "solid"))
2247 2248 2249
> par(op)
> 
> # Line-shaded polygons
2250
> plot(c(1, 9), 1:2, type = "n")
2251
> polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
2252
+         density = c(10, 20), angle = c(-45, 45))
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
> 
> 
> 
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
> cleanEx()
> nameEx("polypath")
> ### * polypath
> 
> flush(stderr()); flush(stdout())
> 
> ### Name: polypath
> ### Title: Path Drawing
> ### Aliases: polypath
> ### Keywords: aplot
> 
> ### ** Examples
> 
2270
> plotPath <- function(x, y, col = "grey", rule = "winding") {
2271
+     plot.new()
2272 2273
+     plot.window(range(x, na.rm = TRUE), range(y, na.rm = TRUE))
+     polypath(x, y, col = col, rule = rule)
2274
+     if (!is.na(col))
2275
+         mtext(paste("Rule:", rule), side = 1, line = 0)
2276 2277 2278 2279
+ }
> 
> plotRules <- function(x, y, title) {
+     plotPath(x, y)
2280 2281 2282
+     plotPath(x, y, rule = "evenodd")
+     mtext(title, side = 3, line = 0)
+     plotPath(x, y, col = NA)
2283 2284
+ }
> 
2285
> op <- par(mfrow = c(5, 3), mar = c(2, 1, 1, 1))
2286 2287 2288
> 
> plotRules(c(.1, .1, .9, .9, NA, .2, .2, .8, .8),
+           c(.1, .9, .9, .1, NA, .2, .8, .8, .2),
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298