The R Project SVN R

Rev

Rev 77426 | Rev 77514 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56353 ripley 1
 
77431 ripley 2
R Under development (unstable) (2019-11-17 r77430) -- "Unsuffered Consequences"
75937 ripley 3
Copyright (C) 2019 The R Foundation for Statistical Computing
68809 ripley 4
Platform: x86_64-pc-linux-gnu (64-bit)
56353 ripley 5
 
6
R is free software and comes with ABSOLUTELY NO WARRANTY.
7
You are welcome to redistribute it under certain conditions.
8
Type 'license()' or 'licence()' for distribution details.
9
 
10
  Natural language support but running in an English locale
11
 
12
R is a collaborative project with many contributors.
13
Type 'contributors()' for more information and
14
'citation()' on how to cite R or R packages in publications.
15
 
16
Type 'demo()' for some demos, 'help()' for on-line help, or
17
'help.start()' for an HTML browser interface to help.
18
Type 'q()' to quit R.
19
 
20
> pkgname <- "graphics"
21
> source(file.path(R.home("share"), "R", "examples-header.R"))
22
> options(warn = 1)
23
> library('graphics')
77426 ripley 24
> 
61787 ripley 25
> base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
73819 hornik 26
> base::assign(".old_wd", base::getwd(), pos = 'CheckExEnv')
56353 ripley 27
> cleanEx()
28
> nameEx("abline")
29
> ### * abline
77426 ripley 30
> 
56353 ripley 31
> flush(stderr()); flush(stdout())
77426 ripley 32
> 
56353 ripley 33
> ### Name: abline
34
> ### Title: Add Straight Lines to a Plot
35
> ### Aliases: abline
36
> ### Keywords: aplot
77426 ripley 37
> 
56353 ripley 38
> ### ** Examples
77426 ripley 39
> 
61157 ripley 40
> ## Setup up coordinate system (with x == y aspect ratio):
41
> plot(c(-2,3), c(-1,5), type = "n", xlab = "x", ylab = "y", asp = 1)
56353 ripley 42
> ## the x- and y-axis, and an integer grid
61157 ripley 43
> abline(h = 0, v = 0, col = "gray60")
56353 ripley 44
> text(1,0, "abline( h = 0 )", col = "gray60", adj = c(0, -.1))
61157 ripley 45
> abline(h = -1:5, v = -2:3, col = "lightgray", lty = 3)
46
> abline(a = 1, b = 2, col = 2)
61169 ripley 47
> text(1,3, "abline( 1, 2 )", col = 2, adj = c(-.1, -.1))
77426 ripley 48
> 
56353 ripley 49
> ## Simple Regression Lines:
50
> require(stats)
51
> sale5 <- c(6, 4, 9, 7, 6, 12, 8, 10, 9, 13)
52
> plot(sale5)
61169 ripley 53
> abline(lsfit(1:10, sale5))
54
> abline(lsfit(1:10, sale5, intercept = FALSE), col = 4) # less fitting
77426 ripley 55
> 
56353 ripley 56
> z <- lm(dist ~ speed, data = cars)
57
> plot(cars)
58
> abline(z) # equivalent to abline(reg = z) or
59
> abline(coef = coef(z))
77426 ripley 60
> 
56353 ripley 61
> ## trivial intercept model
62
> abline(mC <- lm(dist ~ 1, data = cars)) ## the same as
63
> abline(a = coef(mC), b = 0, col = "blue")
77426 ripley 64
> 
65
> 
66
> 
56353 ripley 67
> cleanEx()
68
> nameEx("arrows")
69
> ### * arrows
77426 ripley 70
> 
56353 ripley 71
> flush(stderr()); flush(stdout())
77426 ripley 72
> 
56353 ripley 73
> ### Name: arrows
74
> ### Title: Add Arrows to a Plot
75
> ### Aliases: arrows
76
> ### Keywords: aplot
77426 ripley 77
> 
56353 ripley 78
> ### ** Examples
77426 ripley 79
> 
56353 ripley 80
> x <- stats::runif(12); y <- stats::rnorm(12)
61169 ripley 81
> i <- order(x, y); x <- x[i]; y <- y[i]
61157 ripley 82
> plot(x,y, main = "arrows(.) and segments(.)")
56353 ripley 83
> ## draw arrows from point to point :
61169 ripley 84
> s <- seq(length(x)-1)  # one shorter than data
61157 ripley 85
> arrows(x[s], y[s], x[s+1], y[s+1], col = 1:3)
56353 ripley 86
> s <- s[-length(s)]
61169 ripley 87
> segments(x[s], y[s], x[s+2], y[s+2], col = "pink")
77426 ripley 88
> 
89
> 
90
> 
56353 ripley 91
> cleanEx()
92
> nameEx("assocplot")
93
> ### * assocplot
77426 ripley 94
> 
56353 ripley 95
> flush(stderr()); flush(stdout())
77426 ripley 96
> 
56353 ripley 97
> ### Name: assocplot
98
> ### Title: Association Plots
99
> ### Aliases: assocplot
100
> ### Keywords: hplot
77426 ripley 101
> 
56353 ripley 102
> ### ** Examples
77426 ripley 103
> 
56353 ripley 104
> ## Aggregate over sex:
105
> x <- margin.table(HairEyeColor, c(1, 2))
106
> x
107
       Eye
108
Hair    Brown Blue Hazel Green
109
  Black    68   20    15     5
110
  Brown   119   84    54    29
111
  Red      26   17    14    14
112
  Blond     7   94    10    16
113
> assocplot(x, main = "Relation between hair and eye color")
77426 ripley 114
> 
115
> 
116
> 
56353 ripley 117
> cleanEx()
118
> nameEx("axTicks")
119
> ### * axTicks
77426 ripley 120
> 
56353 ripley 121
> flush(stderr()); flush(stdout())
77426 ripley 122
> 
56353 ripley 123
> ### Name: axTicks
124
> ### Title: Compute Axis Tickmark Locations
125
> ### Aliases: axTicks
126
> ### Keywords: dplot
77426 ripley 127
> 
56353 ripley 128
> ### ** Examples
77426 ripley 129
> 
56353 ripley 130
>  plot(1:7, 10*21:27)
131
>  axTicks(1)
132
[1] 1 2 3 4 5 6 7
133
>  axTicks(2)
134
[1] 210 220 230 240 250 260 270
135
>  stopifnot(identical(axTicks(1), axTicks(3)),
136
+            identical(axTicks(2), axTicks(4)))
77426 ripley 137
> 
56353 ripley 138
> ## Show how axTicks() and axis() correspond :
61169 ripley 139
> op <- par(mfrow = c(3, 1))
140
> for(x in 9999 * c(1, 2, 8)) {
141
+     plot(x, 9, log = "x")
61157 ripley 142
+     cat(formatC(par("xaxp"), width = 5),";", T <- axTicks(1),"\n")
143
+     rug(T, col =  adjustcolor("red", 0.5), lwd = 4)
56353 ripley 144
+ }
77426 ripley 145
 1000 1e+05     3 ; 200 500 1000 2000 5000 10000 20000 50000 1e+05 2e+05 5e+05 
146
 1000 1e+06     2 ; 500 1000 5000 10000 50000 1e+05 5e+05 1e+06 
147
 1000 1e+07     1 ; 1000 10000 1e+05 1e+06 1e+07 
56353 ripley 148
> par(op)
77426 ripley 149
> 
57122 maechler 150
> x <- 9.9*10^(-3:10)
151
> plot(x, 1:14, log = "x")
152
> axTicks(1) # now length 5, in R <= 2.13.x gave the following
153
[1] 1e-02 1e+01 1e+04 1e+07 1e+10
61157 ripley 154
> axTicks(1, nintLog = Inf) # rather too many
57122 maechler 155
 [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
156
[13] 1e+10 1e+11
77426 ripley 157
> 
56353 ripley 158
> ## An example using axTicks() without reference to an existing plot
159
> ## (copying R's internal procedures for setting axis ranges etc.),
57360 ripley 160
> ## You do need to supply _all_ of axp, usr, log, nintLog
56353 ripley 161
> ## standard logarithmic y axis labels
162
> ylims <- c(0.2, 88)
163
> get_axp <- function(x) 10^c(ceiling(x[1]), floor(x[2]))
61157 ripley 164
> ## mimic par("yaxs") == "i"
56353 ripley 165
> usr.i <- log10(ylims)
61157 ripley 166
> (aT.i <- axTicks(side = 2, usr = usr.i,
167
+                  axp = c(get_axp(usr.i), n = 3), log = TRUE, nintLog = 5))
57362 ripley 168
[1]  0.2  0.5  1.0  2.0  5.0 10.0 20.0 50.0
61157 ripley 169
> ## mimic (default) par("yaxs") == "r"
56353 ripley 170
> usr.r <- extendrange(r = log10(ylims), f = 0.04)
61157 ripley 171
> (aT.r <- axTicks(side = 2, usr = usr.r,
172
+                  axp = c(get_axp(usr.r), 3), log = TRUE, nintLog = 5))
56353 ripley 173
[1]   0.2   0.5   1.0   2.0   5.0  10.0  20.0  50.0 100.0
77426 ripley 174
> 
56353 ripley 175
> ## Prove that we got it right :
61157 ripley 176
> plot(0:1, ylims, log = "y", yaxs = "i")
177
> stopifnot(all.equal(aT.i, axTicks(side = 2)))
77426 ripley 178
> 
61169 ripley 179
> plot(0:1, ylims, log = "y", yaxs = "r")
61157 ripley 180
> stopifnot(all.equal(aT.r, axTicks(side = 2)))
77426 ripley 181
> 
182
> 
183
> 
56353 ripley 184
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
185
> cleanEx()
186
> nameEx("axis.POSIXct")
187
> ### * axis.POSIXct
77426 ripley 188
> 
56353 ripley 189
> flush(stderr()); flush(stdout())
77426 ripley 190
> 
56353 ripley 191
> ### Name: axis.POSIXct
192
> ### Title: Date and Date-time Plotting Functions
193
> ### Aliases: axis.POSIXct axis.Date
194
> ### Keywords: utilities chron
77426 ripley 195
> 
56353 ripley 196
> ### ** Examples
77426 ripley 197
> 
56353 ripley 198
> with(beaver1, {
199
+ time <- strptime(paste(1990, day, time %/% 100, time %% 100),
200
+                  "%Y %j %H %M")
61157 ripley 201
+ plot(time, temp, type = "l") # axis at 4-hour intervals.
56353 ripley 202
+ # now label every hour on the time axis
61157 ripley 203
+ plot(time, temp, type = "l", xaxt = "n")
56353 ripley 204
+ r <- as.POSIXct(round(range(time), "hours"))
61157 ripley 205
+ axis.POSIXct(1, at = seq(r[1], r[2], by = "hour"), format = "%H")
56353 ripley 206
+ })
77426 ripley 207
> 
61157 ripley 208
> plot(.leap.seconds, seq_along(.leap.seconds), type = "n", yaxt = "n",
209
+      xlab = "leap seconds", ylab = "", bty = "n")
56353 ripley 210
> rug(.leap.seconds)
211
> ## or as dates
212
> lps <- as.Date(.leap.seconds)
213
> plot(lps, seq_along(.leap.seconds),
214
+      type = "n", yaxt = "n", xlab = "leap seconds",
215
+      ylab = "", bty = "n")
216
> rug(lps)
77426 ripley 217
> 
56353 ripley 218
> ## 100 random dates in a 10-week period
219
> random.dates <- as.Date("2001/1/1") + 70*sort(stats::runif(100))
220
> plot(random.dates, 1:100)
221
> # or for a better axis labelling
61157 ripley 222
> plot(random.dates, 1:100, xaxt = "n")
223
> axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "weeks"))
224
> axis.Date(1, at = seq(as.Date("2001/1/1"), max(random.dates)+6, "days"),
56353 ripley 225
+      labels = FALSE, tcl = -0.2)
77426 ripley 226
> 
227
> 
228
> 
56353 ripley 229
> cleanEx()
230
> nameEx("axis")
231
> ### * axis
77426 ripley 232
> 
56353 ripley 233
> flush(stderr()); flush(stdout())
77426 ripley 234
> 
56353 ripley 235
> ### Name: axis
236
> ### Title: Add an Axis to a Plot
237
> ### Aliases: axis
238
> ### Keywords: aplot
77426 ripley 239
> 
56353 ripley 240
> ### ** Examples
77426 ripley 241
> 
56353 ripley 242
> require(stats) # for rnorm
243
> plot(1:4, rnorm(4), axes = FALSE)
244
> axis(1, 1:4, LETTERS[1:4])
245
> axis(2)
246
> box() #- to make it look "as usual"
77426 ripley 247
> 
56353 ripley 248
> plot(1:7, rnorm(7), main = "axis() examples",
249
+      type = "s", xaxt = "n", frame = FALSE, col = "red")
250
> axis(1, 1:7, LETTERS[1:7], col.axis = "blue")
251
> # unusual options:
61157 ripley 252
> axis(4, col = "violet", col.axis = "dark violet", lwd = 2)
56353 ripley 253
> axis(3, col = "gold", lty = 2, lwd = 0.5)
77426 ripley 254
> 
56353 ripley 255
> # one way to have a custom x axis
256
> plot(1:10, xaxt = "n")
61157 ripley 257
> axis(1, xaxp = c(2, 9, 7))
77426 ripley 258
> 
74613 maechler 259
> ## Changing default gap between labels:
260
> plot(0:100, type="n", axes=FALSE, ann=FALSE)
261
> title(quote("axis(1, .., gap.axis = f)," ~~ f >= 0))
262
> axis(2, at = 5*(0:20), las = 1, gap.axis = 1/4)
263
> gaps <- c(4, 2, 1, 1/2, 1/4, 0.1, 0)
264
> chG <- paste0(ifelse(gaps == 1, "default:  ", ""),
265
+               "gap.axis=", formatC(gaps))
266
> jj <- seq_along(gaps)
267
> linG <- -2.5*(jj-1)
268
> for(j in jj) {
269
+     isD <- gaps[j] == 1 # is default
270
+     axis (1, at=5*(0:20), gap.axis = gaps[j], padj=-1, line = linG[j],
271
+           col.axis = if(isD) "forest green" else 1, font.axis= 1+isD)
272
+ }
273
> mtext(chG, side=1, padj=-1, line = linG -1/2, cex=3/4,
274
+       col = ifelse(gaps == 1, "forest green", "blue3"))
275
> ## now shrink the window (in x- and y-direction) and observe the axis labels drawn
77426 ripley 276
> 
277
> 
278
> 
56353 ripley 279
> cleanEx()
280
> nameEx("barplot")
281
> ### * barplot
77426 ripley 282
> 
56353 ripley 283
> flush(stderr()); flush(stdout())
77426 ripley 284
> 
56353 ripley 285
> ### Name: barplot
286
> ### Title: Bar Plots
76269 ripley 287
> ### Aliases: barplot barplot.default barplot.formula
56353 ripley 288
> ### Keywords: hplot
77426 ripley 289
> 
56353 ripley 290
> ### ** Examples
77426 ripley 291
> 
76269 ripley 292
> # Formula method
293
> barplot(GNP ~ Year, data = longley)
294
> barplot(cbind(Employed, Unemployed) ~ Year, data = longley)
77426 ripley 295
> 
76269 ripley 296
> ## 3rd form of formula - 2 categories :
297
> op <- par(mfrow = 2:1, mgp = c(3,1,0)/2, mar = .1+c(3,3:1))
298
> summary(d.Titanic <- as.data.frame(Titanic))
77426 ripley 299
  Class       Sex        Age     Survived      Freq       
300
 1st :8   Male  :16   Child:16   No :16   Min.   :  0.00  
301
 2nd :8   Female:16   Adult:16   Yes:16   1st Qu.:  0.75  
302
 3rd :8                                   Median : 13.50  
303
 Crew:8                                   Mean   : 68.78  
304
                                          3rd Qu.: 77.00  
305
                                          Max.   :670.00  
76269 ripley 306
> barplot(Freq ~ Class + Survived, data = d.Titanic,
307
+         subset = Age == "Adult" & Sex == "Male",
308
+         main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend = TRUE)
309
> # Corresponding table :
310
> (xt <- xtabs(Freq ~ Survived + Class + Sex, d.Titanic, subset = Age=="Adult"))
311
, , Sex = Male
312
 
313
        Class
314
Survived 1st 2nd 3rd Crew
315
     No  118 154 387  670
316
     Yes  57  14  75  192
317
 
318
, , Sex = Female
319
 
320
        Class
321
Survived 1st 2nd 3rd Crew
322
     No    4  13  89    3
323
     Yes 140  80  76   20
324
 
325
> # Alternatively, a mosaic plot :
326
> mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE)
327
> par(op)
77426 ripley 328
> 
329
> 
76269 ripley 330
> # Default method
56353 ripley 331
> require(grDevices) # for colours
61157 ripley 332
> tN <- table(Ni <- stats::rpois(100, lambda = 5))
333
> r <- barplot(tN, col = rainbow(20))
56353 ripley 334
> #- type = "h" plotting *is* 'bar'plot
61169 ripley 335
> lines(r, tN, type = "h", col = "red", lwd = 2)
77426 ripley 336
> 
61157 ripley 337
> barplot(tN, space = 1.5, axisnames = FALSE,
56353 ripley 338
+         sub = "barplot(..., space= 1.5, axisnames = FALSE)")
77426 ripley 339
> 
56353 ripley 340
> barplot(VADeaths, plot = FALSE)
341
[1] 0.7 1.9 3.1 4.3
342
> barplot(VADeaths, plot = FALSE, beside = TRUE)
343
     [,1] [,2] [,3] [,4]
344
[1,]  1.5  7.5 13.5 19.5
345
[2,]  2.5  8.5 14.5 20.5
346
[3,]  3.5  9.5 15.5 21.5
347
[4,]  4.5 10.5 16.5 22.5
348
[5,]  5.5 11.5 17.5 23.5
77426 ripley 349
> 
56353 ripley 350
> mp <- barplot(VADeaths) # default
351
> tot <- colMeans(VADeaths)
352
> text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
353
> barplot(VADeaths, beside = TRUE,
354
+         col = c("lightblue", "mistyrose", "lightcyan",
355
+                 "lavender", "cornsilk"),
356
+         legend = rownames(VADeaths), ylim = c(0, 100))
357
> title(main = "Death Rates in Virginia", font.main = 4)
77426 ripley 358
> 
56353 ripley 359
> hh <- t(VADeaths)[, 5:1]
360
> mybarcol <- "gray20"
361
> mp <- barplot(hh, beside = TRUE,
362
+         col = c("lightblue", "mistyrose",
363
+                 "lightcyan", "lavender"),
61157 ripley 364
+         legend = colnames(VADeaths), ylim = c(0,100),
56353 ripley 365
+         main = "Death Rates in Virginia", font.main = 4,
366
+         sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
367
+         cex.names = 1.5)
368
> segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
61169 ripley 369
> stopifnot(dim(mp) == dim(hh))  # corresponding matrices
56353 ripley 370
> mtext(side = 1, at = colMeans(mp), line = -2,
371
+       text = paste("Mean", formatC(colMeans(hh))), col = "red")
77426 ripley 372
> 
56353 ripley 373
> # Bar shading example
374
> barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
375
+         legend = rownames(VADeaths))
376
> title(main = list("Death Rates in Virginia", font = 4))
77426 ripley 377
> 
76269 ripley 378
> # Border color
77426 ripley 379
> barplot(VADeaths, border = "dark blue") 
380
> 
76269 ripley 381
> # Log scales (not much sense here)
61157 ripley 382
> barplot(tN, col = heat.colors(12), log = "y")
383
> barplot(tN, col = gray.colors(20), log = "xy")
77426 ripley 384
> 
76269 ripley 385
> # Legend location
56353 ripley 386
> barplot(height = cbind(x = c(465, 91) / 465 * 100,
387
+                        y = c(840, 200) / 840 * 100,
388
+                        z = c(37, 17) / 37 * 100),
389
+         beside = FALSE,
390
+         width = c(465, 840, 37),
391
+         col = c(1, 2),
392
+         legend.text = c("A", "B"),
393
+         args.legend = list(x = "topleft"))
77426 ripley 394
> 
395
> 
396
> 
76269 ripley 397
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
56353 ripley 398
> cleanEx()
399
> nameEx("box")
400
> ### * box
77426 ripley 401
> 
56353 ripley 402
> flush(stderr()); flush(stdout())
77426 ripley 403
> 
56353 ripley 404
> ### Name: box
405
> ### Title: Draw a Box around a Plot
406
> ### Aliases: box
407
> ### Keywords: aplot
77426 ripley 408
> 
56353 ripley 409
> ### ** Examples
77426 ripley 410
> 
61169 ripley 411
> plot(1:7, abs(stats::rnorm(7)), type = "h", axes = FALSE)
56353 ripley 412
> axis(1, at = 1:7, labels = letters[1:7])
413
> box(lty = '1373', col = 'red')
77426 ripley 414
> 
415
> 
416
> 
56353 ripley 417
> cleanEx()
418
> nameEx("boxplot")
419
> ### * boxplot
77426 ripley 420
> 
56353 ripley 421
> flush(stderr()); flush(stdout())
77426 ripley 422
> 
56353 ripley 423
> ### Name: boxplot
424
> ### Title: Box Plots
425
> ### Aliases: boxplot boxplot.default boxplot.formula
426
> ### Keywords: hplot
77426 ripley 427
> 
56353 ripley 428
> ### ** Examples
77426 ripley 429
> 
56353 ripley 430
> ## boxplot on a formula:
431
> boxplot(count ~ spray, data = InsectSprays, col = "lightgray")
76503 maechler 432
> # *add* notches (somewhat funny here <--> warning "notches .. outside hinges"):
56353 ripley 433
> boxplot(count ~ spray, data = InsectSprays,
434
+         notch = TRUE, add = TRUE, col = "blue")
73746 maechler 435
Warning in bxp(list(stats = c(7, 11, 14, 18.5, 23, 7, 12, 16.5, 18, 21,  :
56353 ripley 436
  some notches went outside hinges ('box'): maybe set notch=FALSE
77426 ripley 437
> 
76503 maechler 438
> boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
439
+         log = "y")
440
> ## horizontal=TRUE, switching  y <--> x :
441
> boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque",
442
+         log = "x", horizontal=TRUE)
77426 ripley 443
> 
61157 ripley 444
> rb <- boxplot(decrease ~ treatment, data = OrchardSprays, col = "bisque")
56353 ripley 445
> title("Comparing boxplot()s and non-robust mean +/- SD")
446
> mn.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, mean)
447
> sd.t <- tapply(OrchardSprays$decrease, OrchardSprays$treatment, sd)
448
> xi <- 0.3 + seq(rb$n)
449
> points(xi, mn.t, col = "orange", pch = 18)
450
> arrows(xi, mn.t - sd.t, xi, mn.t + sd.t,
451
+        code = 3, col = "pink", angle = 75, length = .1)
77426 ripley 452
> 
56353 ripley 453
> ## boxplot on a matrix:
454
> mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
455
+              `5T` = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
71718 maechler 456
> boxplot(mat) # directly, calling boxplot.matrix()
77426 ripley 457
> 
71718 maechler 458
> ## boxplot on a data frame:
459
> df. <- as.data.frame(mat)
61157 ripley 460
> par(las = 1) # all axis labels horizontal
71718 maechler 461
> boxplot(df., main = "boxplot(*, horizontal = TRUE)", horizontal = TRUE)
77426 ripley 462
> 
56353 ripley 463
> ## Using 'at = ' and adding boxplots -- example idea by Roger Bivand :
464
> boxplot(len ~ dose, data = ToothGrowth,
465
+         boxwex = 0.25, at = 1:3 - 0.2,
466
+         subset = supp == "VC", col = "yellow",
467
+         main = "Guinea Pigs' Tooth Growth",
468
+         xlab = "Vitamin C dose mg",
469
+         ylab = "tooth length",
470
+         xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
471
> boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
472
+         boxwex = 0.25, at = 1:3 + 0.2,
473
+         subset = supp == "OJ", col = "orange")
474
> legend(2, 9, c("Ascorbic acid", "Orange juice"),
475
+        fill = c("yellow", "orange"))
77426 ripley 476
> 
70587 maechler 477
> ## With less effort (slightly different) using factor *interaction*:
478
> boxplot(len ~ dose:supp, data = ToothGrowth,
479
+         boxwex = 0.5, col = c("orange", "yellow"),
480
+         main = "Guinea Pigs' Tooth Growth",
481
+         xlab = "Vitamin C dose mg", ylab = "tooth length",
482
+         sep = ":", lex.order = TRUE, ylim = c(0, 35), yaxs = "i")
77426 ripley 483
> 
56353 ripley 484
> ## more examples in  help(bxp)
77426 ripley 485
> 
486
> 
487
> 
56353 ripley 488
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
489
> cleanEx()
490
> nameEx("boxplot.matrix")
491
> ### * boxplot.matrix
77426 ripley 492
> 
56353 ripley 493
> flush(stderr()); flush(stdout())
77426 ripley 494
> 
56353 ripley 495
> ### Name: boxplot.matrix
496
> ### Title: Draw a Boxplot for each Column (Row) of a Matrix
497
> ### Aliases: boxplot.matrix
498
> ### Keywords: hplot
77426 ripley 499
> 
56353 ripley 500
> ### ** Examples
77426 ripley 501
> 
56353 ripley 502
> ## Very similar to the example in ?boxplot
503
> mat <- cbind(Uni05 = (1:100)/21, Norm = rnorm(100),
504
+              T5 = rt(100, df = 5), Gam2 = rgamma(100, shape = 2))
505
> boxplot(mat, main = "boxplot.matrix(...., main = ...)",
506
+         notch = TRUE, col = 1:4)
77426 ripley 507
> 
508
> 
509
> 
56353 ripley 510
> cleanEx()
511
> nameEx("bxp")
512
> ### * bxp
77426 ripley 513
> 
56353 ripley 514
> flush(stderr()); flush(stdout())
77426 ripley 515
> 
56353 ripley 516
> ### Name: bxp
517
> ### Title: Draw Box Plots from Summaries
518
> ### Aliases: bxp
519
> ### Keywords: aplot
77426 ripley 520
> 
56353 ripley 521
> ### ** Examples
77426 ripley 522
> 
56353 ripley 523
> require(stats)
524
> set.seed(753)
61169 ripley 525
> (bx.p <- boxplot(split(rt(100, 4), gl(5, 20))))
56353 ripley 526
$stats
527
            [,1]        [,2]        [,3]        [,4]        [,5]
528
[1,] -1.66391873 -2.02625162 -2.12785004 -2.76510496 -1.70034047
529
[2,] -0.55308292 -0.65897584 -0.86705616 -1.63431484 -0.81848966
530
[3,] -0.06763313  0.04887846  0.09674026 -0.06712275 -0.01150075
531
[4,]  0.68813940  0.91705734  1.05562526  0.56746581  0.49017934
532
[5,]  1.14222667  3.16270157  2.07574986  2.09523462  1.87734641
533
 
534
$n
535
[1] 20 20 20 20 20
536
 
537
$conf
538
           [,1]       [,2]       [,3]       [,4]       [,5]
539
[1,] -0.5061554 -0.5079321 -0.5825407 -0.8450091 -0.4738519
540
[2,]  0.3708891  0.6056890  0.7760212  0.7107636  0.4508504
541
 
542
$out
543
[1]  4.115274  3.224584  3.920438  4.168341 -4.357819  2.498006
544
 
545
$group
546
[1] 1 1 1 4 5 5
547
 
548
$names
549
[1] "1" "2" "3" "4" "5"
550
 
61169 ripley 551
> op <- par(mfrow =  c(2, 2))
56353 ripley 552
> bxp(bx.p, xaxt = "n")
61157 ripley 553
> bxp(bx.p, notch = TRUE, axes = FALSE, pch = 4, boxfill = 1:5)
56353 ripley 554
Warning in bxp(bx.p, notch = TRUE, axes = FALSE, pch = 4, boxfill = 1:5) :
555
  some notches went outside hinges ('box'): maybe set notch=FALSE
61157 ripley 556
> bxp(bx.p, notch = TRUE, boxfill = "lightblue", frame = FALSE,
557
+     outl = FALSE, main = "bxp(*, frame= FALSE, outl= FALSE)")
56353 ripley 558
Warning in bxp(bx.p, notch = TRUE, boxfill = "lightblue", frame = FALSE,  :
559
  some notches went outside hinges ('box'): maybe set notch=FALSE
61157 ripley 560
> bxp(bx.p, notch = TRUE, boxfill = "lightblue", border = 2:6,
56353 ripley 561
+     ylim = c(-4,4), pch = 22, bg = "green", log = "x",
61169 ripley 562
+     main = "... log = 'x', ylim = *")
56353 ripley 563
Warning in bxp(bx.p, notch = TRUE, boxfill = "lightblue", border = 2:6,  :
564
  some notches went outside hinges ('box'): maybe set notch=FALSE
565
> par(op)
61169 ripley 566
> op <- par(mfrow = c(1, 2))
77426 ripley 567
> 
56353 ripley 568
> ## single group -- no label
61157 ripley 569
> boxplot (weight ~ group, data = PlantGrowth, subset = group == "ctrl")
56353 ripley 570
> ## with label
571
> bx <- boxplot(weight ~ group, data = PlantGrowth,
61157 ripley 572
+               subset = group == "ctrl", plot = FALSE)
61169 ripley 573
> bxp(bx, show.names=TRUE)
56353 ripley 574
> par(op)
77426 ripley 575
> 
61157 ripley 576
> z <- split(rnorm(1000), rpois(1000, 2.2))
577
> boxplot(z, whisklty = 3, main = "boxplot(z, whisklty = 3)")
77426 ripley 578
> 
56353 ripley 579
> ## Colour support similar to plot.default:
61157 ripley 580
> op <- par(mfrow = 1:2, bg = "light gray", fg = "midnight blue")
581
> boxplot(z,   col.axis = "skyblue3", main = "boxplot(*, col.axis=..,main=..)")
582
> plot(z[[1]], col.axis = "skyblue3", main =    "plot(*, col.axis=..,main=..)")
56353 ripley 583
> mtext("par(bg=\"light gray\", fg=\"midnight blue\")",
584
+       outer = TRUE, line = -1.2)
585
> par(op)
77426 ripley 586
> 
56353 ripley 587
> ## Mimic S-Plus:
61157 ripley 588
> splus <- list(boxwex = 0.4, staplewex = 1, outwex = 1, boxfill = "grey40",
589
+               medlwd = 3, medcol = "white", whisklty = 3, outlty = 1, outpch = NA)
590
> boxplot(z, pars = splus)
56353 ripley 591
> ## Recycled and "sweeping" parameters
61157 ripley 592
> op <- par(mfrow = c(1,2))
593
>  boxplot(z, border = 1:5, lty = 3, medlty = 1, medlwd = 2.5)
594
>  boxplot(z, boxfill = 1:3, pch = 1:5, lwd = 1.5, medcol = "white")
56353 ripley 595
> par(op)
596
> ## too many possibilities
61157 ripley 597
> boxplot(z, boxfill = "light gray", outpch = 21:25, outlty = 2,
56353 ripley 598
+         bg = "pink", lwd = 2,
599
+         medcol = "dark blue", medcex = 2, medpch = 20)
77426 ripley 600
> 
601
> 
602
> 
56353 ripley 603
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
604
> cleanEx()
605
> nameEx("cdplot")
606
> ### * cdplot
77426 ripley 607
> 
56353 ripley 608
> flush(stderr()); flush(stdout())
77426 ripley 609
> 
56353 ripley 610
> ### Name: cdplot
611
> ### Title: Conditional Density Plots
612
> ### Aliases: cdplot cdplot.default cdplot.formula
613
> ### Keywords: hplot
77426 ripley 614
> 
56353 ripley 615
> ### ** Examples
77426 ripley 616
> 
56353 ripley 617
> ## NASA space shuttle o-ring failures
618
> fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1,
619
+                  1, 2, 1, 1, 1, 1, 1),
620
+                levels = 1:2, labels = c("no", "yes"))
621
> temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
622
+                  70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)
77426 ripley 623
> 
56353 ripley 624
> ## CD plot
625
> cdplot(fail ~ temperature)
626
> cdplot(fail ~ temperature, bw = 2)
627
> cdplot(fail ~ temperature, bw = "SJ")
77426 ripley 628
> 
56353 ripley 629
> ## compare with spinogram
630
> (spineplot(fail ~ temperature, breaks = 3))
631
           fail
632
temperature no yes
633
    [50,60]  0   3
634
    (60,70]  8   3
635
    (70,80]  7   1
636
    (80,90]  1   0
77426 ripley 637
> 
56353 ripley 638
> ## highlighting for failures
639
> cdplot(fail ~ temperature, ylevels = 2:1)
77426 ripley 640
> 
56353 ripley 641
> ## scatter plot with conditional density
642
> cdens <- cdplot(fail ~ temperature, plot = FALSE)
643
> plot(I(as.numeric(fail) - 1) ~ jitter(temperature, factor = 2),
644
+      xlab = "Temperature", ylab = "Conditional failure probability")
645
> lines(53:81, 1 - cdens[[1]](53:81), col = 2)
77426 ripley 646
> 
647
> 
648
> 
56353 ripley 649
> cleanEx()
650
> nameEx("clip")
651
> ### * clip
77426 ripley 652
> 
56353 ripley 653
> flush(stderr()); flush(stdout())
77426 ripley 654
> 
56353 ripley 655
> ### Name: clip
656
> ### Title: Set Clipping Region
657
> ### Aliases: clip
658
> ### Keywords: dplot
77426 ripley 659
> 
56353 ripley 660
> ### ** Examples
77426 ripley 661
> 
56353 ripley 662
> x <- rnorm(1000)
61157 ripley 663
> hist(x, xlim = c(-4,4))
56353 ripley 664
> usr <- par("usr")
665
> clip(usr[1], -2, usr[3], usr[4])
666
> hist(x, col = 'red', add = TRUE)
667
> clip(2, usr[2], usr[3], usr[4])
668
> hist(x, col = 'blue', add = TRUE)
669
> do.call("clip", as.list(usr))  # reset to plot region
77426 ripley 670
> 
671
> 
672
> 
56353 ripley 673
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
674
> cleanEx()
675
> nameEx("contour")
676
> ### * contour
77426 ripley 677
> 
56353 ripley 678
> flush(stderr()); flush(stdout())
77426 ripley 679
> 
56353 ripley 680
> ### Name: contour
681
> ### Title: Display Contours
682
> ### Aliases: contour contour.default
683
> ### Keywords: hplot aplot
77426 ripley 684
> 
56353 ripley 685
> ### ** Examples
77426 ripley 686
> 
56353 ripley 687
> require(grDevices) # for colours
688
> x <- -6:16
689
> op <- par(mfrow = c(2, 2))
690
> contour(outer(x, x), method = "edge", vfont = c("sans serif", "plain"))
691
> z <- outer(x, sqrt(abs(x)), FUN = "/")
692
> image(x, x, z)
693
> contour(x, x, z, col = "pink", add = TRUE, method = "edge",
694
+         vfont = c("sans serif", "plain"))
65704 maechler 695
> contour(x, x, z, ylim = c(1, 6), method = "simple", labcex = 1,
696
+         xlab = quote(x[1]), ylab = quote(x[2]))
697
> contour(x, x, z, ylim = c(-6, 6), nlev = 20, lty = 2, method = "simple",
698
+         main = "20 levels; \"simple\" labelling method")
56353 ripley 699
> par(op)
77356 maechler 700
> 
701
> ## Passing multiple colours / lty / lwd :
77346 maechler 702
> op <- par(mfrow = c(1, 2))
703
> z <- outer(-9:25, -9:25)
704
> ## Using default levels <- pretty(range(z, finite = TRUE), 10),
705
> ##  the first and last of which typically are *not* drawn:
706
> (levs <- pretty(z, n=10)) # -300 -200 ... 600 700
707
 [1] -300 -200 -100    0  100  200  300  400  500  600  700
708
> contour(z, col = 1:4)
77356 maechler 709
> ## Set levels explicitly; show that 'lwd' and 'lty' are recycled as well:
77346 maechler 710
> contour(z, levels=levs[-c(1,length(levs))], col = 1:5, lwd = 1:3 *1.5, lty = 1:3)
711
> par(op)
77426 ripley 712
> 
56353 ripley 713
> ## Persian Rug Art:
714
> x <- y <- seq(-4*pi, 4*pi, len = 27)
715
> r <- sqrt(outer(x^2, y^2, "+"))
716
> opar <- par(mfrow = c(2, 2), mar = rep(0, 4))
717
> for(f in pi^(0:3))
718
+   contour(cos(r^2)*exp(-r/f),
719
+           drawlabels = FALSE, axes = FALSE, frame = TRUE)
77426 ripley 720
> 
56353 ripley 721
> rx <- range(x <- 10*1:nrow(volcano))
722
> ry <- range(y <- 10*1:ncol(volcano))
61169 ripley 723
> ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2
56353 ripley 724
> tcol <- terrain.colors(12)
725
> par(opar); opar <- par(pty = "s", bg = "lightcyan")
61169 ripley 726
> plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
56353 ripley 727
> u <- par("usr")
728
> rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
729
> contour(x, y, volcano, col = tcol[2], lty = "solid", add = TRUE,
730
+         vfont = c("sans serif", "plain"))
731
> title("A Topographic Map of Maunga Whau", font = 4)
732
> abline(h = 200*0:4, v = 200*0:4, col = "lightgray", lty = 2, lwd = 0.1)
77426 ripley 733
> 
56353 ripley 734
> ## contourLines produces the same contour lines as contour
61169 ripley 735
> plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "")
56353 ripley 736
> u <- par("usr")
737
> rect(u[1], u[3], u[2], u[4], col = tcol[8], border = "red")
65704 maechler 738
> contour(x, y, volcano, col = tcol[1], lty = "solid", add = TRUE,
739
+         vfont = c("sans serif", "plain"))
740
> line.list <- contourLines(x, y, volcano)
741
> invisible(lapply(line.list, lines, lwd=3, col=adjustcolor(2, .3)))
56353 ripley 742
> par(opar)
77426 ripley 743
> 
744
> 
745
> 
56353 ripley 746
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
747
> cleanEx()
748
> nameEx("convertXY")
749
> ### * convertXY
77426 ripley 750
> 
56353 ripley 751
> flush(stderr()); flush(stdout())
77426 ripley 752
> 
56353 ripley 753
> ### Name: convertXY
754
> ### Title: Convert between Graphics Coordinate Systems
755
> ### Aliases: grconvertX grconvertY
756
> ### Keywords: dplot
77426 ripley 757
> 
56353 ripley 758
> ### ** Examples
77426 ripley 759
> 
56353 ripley 760
> op <- par(omd=c(0.1, 0.9, 0.1, 0.9), mfrow = c(1, 2))
761
> plot(1:4)
75295 murrell 762
> for(tp in c("in", "dev", "ndc", "nfc", "npc", "nic", "lines", "chars"))
56353 ripley 763
+     print(grconvertX(c(1.0, 4.0), "user", tp))
764
[1] 1.577778 3.022222
56942 ripley 765
[1] 113.6 217.6
56353 ripley 766
[1] 0.2253968 0.4317460
767
[1] 0.3134921 0.8293651
768
[1] 0.03703704 0.96296296
769
[1] 0.1567460 0.4146825
75295 murrell 770
[1]  7.888889 15.111111
771
[1]  7.888889 15.111111
56353 ripley 772
> par(op)
77426 ripley 773
> 
774
> 
775
> 
56353 ripley 776
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
777
> cleanEx()
778
> nameEx("coplot")
779
> ### * coplot
77426 ripley 780
> 
56353 ripley 781
> flush(stderr()); flush(stdout())
77426 ripley 782
> 
56353 ripley 783
> ### Name: coplot
784
> ### Title: Conditioning Plots
785
> ### Aliases: coplot co.intervals
786
> ### Keywords: hplot aplot
77426 ripley 787
> 
56353 ripley 788
> ### ** Examples
77426 ripley 789
> 
56353 ripley 790
> ## Tonga Trench Earthquakes
791
> coplot(lat ~ long | depth, data = quakes)
61157 ripley 792
> given.depth <- co.intervals(quakes$depth, number = 4, overlap = .1)
793
> coplot(lat ~ long | depth, data = quakes, given.v = given.depth, rows = 1)
77426 ripley 794
> 
56353 ripley 795
> ## Conditioning on 2 variables:
796
> ll.dm <- lat ~ long | depth * mag
797
> coplot(ll.dm, data = quakes)
61169 ripley 798
> coplot(ll.dm, data = quakes, number = c(4, 7), show.given = c(TRUE, FALSE))
799
> coplot(ll.dm, data = quakes, number = c(3, 7),
800
+        overlap = c(-.5, .1)) # negative overlap DROPS values
77426 ripley 801
> 
56353 ripley 802
> ## given two factors
61157 ripley 803
> Index <- seq(length = nrow(warpbreaks)) # to get nicer default labels
56353 ripley 804
> coplot(breaks ~ Index | wool * tension, data = warpbreaks,
805
+        show.given = 0:1)
806
> coplot(breaks ~ Index | wool * tension, data = warpbreaks,
807
+        col = "red", bg = "pink", pch = 21,
808
+        bar.bg = c(fac = "light blue"))
77426 ripley 809
> 
56353 ripley 810
> ## Example with empty panels:
811
> with(data.frame(state.x77), {
812
+ coplot(Life.Exp ~ Income | Illiteracy * state.region, number = 3,
813
+        panel = function(x, y, ...) panel.smooth(x, y, span = .8, ...))
814
+ ## y ~ factor -- not really sensible, but 'show off':
815
+ coplot(Life.Exp ~ state.region | Income * state.division,
816
+        panel = panel.smooth)
817
+ })
77426 ripley 818
> 
819
> 
820
> 
56353 ripley 821
> cleanEx()
822
> nameEx("curve")
823
> ### * curve
77426 ripley 824
> 
56353 ripley 825
> flush(stderr()); flush(stdout())
77426 ripley 826
> 
56353 ripley 827
> ### Name: curve
828
> ### Title: Draw Function Plots
829
> ### Aliases: curve plot.function
830
> ### Keywords: hplot
77426 ripley 831
> 
56353 ripley 832
> ### ** Examples
77426 ripley 833
> 
56353 ripley 834
> plot(qnorm) # default range c(0, 1) is appropriate here,
835
>             # but end values are -/+Inf and so are omitted.
836
> plot(qlogis, main = "The Inverse Logit : qlogis()")
837
> abline(h = 0, v = 0:2/2, lty = 3, col = "gray")
77426 ripley 838
> 
56353 ripley 839
> curve(sin, -2*pi, 2*pi, xname = "t")
840
> curve(tan, xname = "t", add = NA,
841
+       main = "curve(tan)  --> same x-scale as previous plot")
77426 ripley 842
> 
56353 ripley 843
> op <- par(mfrow = c(2, 2))
844
> curve(x^3 - 3*x, -2, 2)
845
> curve(x^2 - 2, add = TRUE, col = "violet")
77426 ripley 846
> 
56353 ripley 847
> ## simple and advanced versions, quite similar:
848
> plot(cos, -pi,  3*pi)
849
> curve(cos, xlim = c(-pi, 3*pi), n = 1001, col = "blue", add = TRUE)
77426 ripley 850
> 
56353 ripley 851
> chippy <- function(x) sin(cos(x)*exp(-x/2))
852
> curve(chippy, -8, 7, n = 2001)
853
> plot (chippy, -8, -5)
77426 ripley 854
> 
56353 ripley 855
> for(ll in c("", "x", "y", "xy"))
61157 ripley 856
+    curve(log(1+x), 1, 100, log = ll, sub = paste0("log = '", ll, "'"))
56353 ripley 857
> par(op)
77426 ripley 858
> 
859
> 
860
> 
56353 ripley 861
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
862
> cleanEx()
863
> nameEx("dotchart")
864
> ### * dotchart
77426 ripley 865
> 
56353 ripley 866
> flush(stderr()); flush(stdout())
77426 ripley 867
> 
56353 ripley 868
> ### Name: dotchart
869
> ### Title: Cleveland's Dot Plots
870
> ### Aliases: dotchart
871
> ### Keywords: hplot
77426 ripley 872
> 
56353 ripley 873
> ### ** Examples
77426 ripley 874
> 
56353 ripley 875
> dotchart(VADeaths, main = "Death Rates in Virginia - 1940")
61169 ripley 876
> op <- par(xaxs = "i")  # 0 -- 100%
77314 maechler 877
> dotchart(t(VADeaths), xlim = c(0,100), bg = "skyblue",
56353 ripley 878
+          main = "Death Rates in Virginia - 1940")
879
> par(op)
77426 ripley 880
> 
881
> 
882
> 
56353 ripley 883
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
884
> cleanEx()
885
> nameEx("filled.contour")
886
> ### * filled.contour
77426 ripley 887
> 
56353 ripley 888
> flush(stderr()); flush(stdout())
77426 ripley 889
> 
56353 ripley 890
> ### Name: filled.contour
891
> ### Title: Level (Contour) Plots
58477 ripley 892
> ### Aliases: filled.contour .filled.contour
56353 ripley 893
> ### Keywords: hplot aplot
77426 ripley 894
> 
56353 ripley 895
> ### ** Examples
77426 ripley 896
> 
76277 murrell 897
> require("grDevices") # for colours
898
> filled.contour(volcano, asp = 1) # simple
77426 ripley 899
> 
56353 ripley 900
> x <- 10*1:nrow(volcano)
901
> y <- 10*1:ncol(volcano)
76277 murrell 902
> filled.contour(x, y, volcano, color = function(n) hcl.colors(n, "terrain"),
56353 ripley 903
+     plot.title = title(main = "The Topography of Maunga Whau",
904
+     xlab = "Meters North", ylab = "Meters West"),
905
+     plot.axes = { axis(1, seq(100, 800, by = 100))
906
+                   axis(2, seq(100, 600, by = 100)) },
61157 ripley 907
+     key.title = title(main = "Height\n(meters)"),
61169 ripley 908
+     key.axes = axis(4, seq(90, 190, by = 10)))  # maybe also asp = 1
56353 ripley 909
> mtext(paste("filled.contour(.) from", R.version.string),
910
+       side = 1, line = 4, adj = 1, cex = .66)
77426 ripley 911
> 
56353 ripley 912
> # Annotating a filled contour plot
913
> a <- expand.grid(1:20, 1:20)
914
> b <- matrix(a[,1] + a[,2], 20)
915
> filled.contour(x = 1:20, y = 1:20, z = b,
61169 ripley 916
+                plot.axes = { axis(1); axis(2); points(10, 10) })
77426 ripley 917
> 
56353 ripley 918
> ## Persian Rug Art:
919
> x <- y <- seq(-4*pi, 4*pi, len = 27)
920
> r <- sqrt(outer(x^2, y^2, "+"))
921
> filled.contour(cos(r^2)*exp(-r/(2*pi)), axes = FALSE)
922
> ## rather, the key *should* be labeled:
923
> filled.contour(cos(r^2)*exp(-r/(2*pi)), frame.plot = FALSE,
924
+                plot.axes = {})
77426 ripley 925
> 
926
> 
927
> 
56353 ripley 928
> cleanEx()
929
> nameEx("fourfoldplot")
930
> ### * fourfoldplot
77426 ripley 931
> 
56353 ripley 932
> flush(stderr()); flush(stdout())
77426 ripley 933
> 
56353 ripley 934
> ### Name: fourfoldplot
935
> ### Title: Fourfold Plots
936
> ### Aliases: fourfoldplot
937
> ### Keywords: hplot
77426 ripley 938
> 
56353 ripley 939
> ### ** Examples
77426 ripley 940
> 
56353 ripley 941
> ## Use the Berkeley admission data as in Friendly (1995).
942
> x <- aperm(UCBAdmissions, c(2, 1, 3))
943
> dimnames(x)[[2]] <- c("Yes", "No")
944
> names(dimnames(x)) <- c("Sex", "Admit?", "Department")
945
> stats::ftable(x)
946
              Department   A   B   C   D   E   F
77426 ripley 947
Sex    Admit?                                   
56353 ripley 948
Male   Yes               512 353 120 138  53  22
949
       No                313 207 205 279 138 351
950
Female Yes                89  17 202 131  94  24
951
       No                 19   8 391 244 299 317
77426 ripley 952
> 
56353 ripley 953
> ## Fourfold display of data aggregated over departments, with
954
> ## frequencies standardized to equate the margins for admission
955
> ## and sex.
956
> ## Figure 1 in Friendly (1994).
957
> fourfoldplot(margin.table(x, c(1, 2)))
77426 ripley 958
> 
56353 ripley 959
> ## Fourfold display of x, with frequencies in each table
960
> ## standardized to equate the margins for admission and sex.
961
> ## Figure 2 in Friendly (1994).
962
> fourfoldplot(x)
77426 ripley 963
> 
56353 ripley 964
> ## Fourfold display of x, with frequencies in each table
965
> ## standardized to equate the margins for admission. but not
966
> ## for sex.
967
> ## Figure 3 in Friendly (1994).
968
> fourfoldplot(x, margin = 2)
77426 ripley 969
> 
970
> 
971
> 
56353 ripley 972
> cleanEx()
973
> nameEx("grid")
974
> ### * grid
77426 ripley 975
> 
56353 ripley 976
> flush(stderr()); flush(stdout())
77426 ripley 977
> 
56353 ripley 978
> ### Name: grid
979
> ### Title: Add Grid to a Plot
980
> ### Aliases: grid
981
> ### Keywords: aplot
77426 ripley 982
> 
56353 ripley 983
> ### ** Examples
77426 ripley 984
> 
56353 ripley 985
> plot(1:3)
986
> grid(NA, 5, lwd = 2) # grid only in y-direction
77426 ripley 987
> 
61169 ripley 988
> ## maybe change the desired number of tick marks:  par(lab = c(mx, my, 7))
56353 ripley 989
> op <- par(mfcol = 1:2)
990
> with(iris,
991
+      {
992
+      plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
993
+           xlim = c(4, 8), ylim = c(2, 4.5), panel.first = grid(),
994
+           main = "with(iris,  plot(...., panel.first = grid(), ..) )")
995
+      plot(Sepal.Length, Sepal.Width, col = as.integer(Species),
61169 ripley 996
+           panel.first = grid(3, lty = 1, lwd = 2),
997
+           main = "... panel.first = grid(3, lty = 1, lwd = 2), ..")
56353 ripley 998
+      }
999
+     )
1000
> par(op)
77426 ripley 1001
> 
1002
> 
1003
> 
1004
> 
56353 ripley 1005
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1006
> cleanEx()
1007
> nameEx("hist.POSIXt")
1008
> ### * hist.POSIXt
77426 ripley 1009
> 
56353 ripley 1010
> flush(stderr()); flush(stdout())
77426 ripley 1011
> 
56353 ripley 1012
> ### Name: hist.POSIXt
1013
> ### Title: Histogram of a Date or Date-Time Object
1014
> ### Aliases: hist.POSIXt hist.Date
1015
> ### Keywords: chron dplot hplot
77426 ripley 1016
> 
56353 ripley 1017
> ### ** Examples
77426 ripley 1018
> 
56353 ripley 1019
> hist(.leap.seconds, "years", freq = TRUE)
1020
> hist(.leap.seconds,
68616 ripley 1021
+      seq(ISOdate(1970, 1, 1), ISOdate(2020, 1, 1), "5 years"))
71816 ripley 1022
> rug(.leap.seconds, lwd=2)
77426 ripley 1023
> 
56353 ripley 1024
> ## 100 random dates in a 10-week period
1025
> random.dates <- as.Date("2001/1/1") + 70*stats::runif(100)
1026
> hist(random.dates, "weeks", format = "%d %b")
77426 ripley 1027
> 
1028
> 
1029
> 
56353 ripley 1030
> cleanEx()
1031
> nameEx("hist")
1032
> ### * hist
77426 ripley 1033
> 
56353 ripley 1034
> flush(stderr()); flush(stdout())
77426 ripley 1035
> 
56353 ripley 1036
> ### Name: hist
1037
> ### Title: Histograms
1038
> ### Aliases: hist hist.default
1039
> ### Keywords: dplot hplot distribution
77426 ripley 1040
> 
56353 ripley 1041
> ### ** Examples
77426 ripley 1042
> 
61157 ripley 1043
> op <- par(mfrow = c(2, 2))
56353 ripley 1044
> hist(islands)
61157 ripley 1045
> utils::str(hist(islands, col = "gray", labels = TRUE))
61587 ripley 1046
List of 6
1047
 $ breaks  : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
1048
 $ counts  : int [1:9] 41 2 1 1 1 1 0 0 1
1049
 $ density : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
1050
 $ mids    : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
1051
 $ xname   : chr "islands"
1052
 $ equidist: logi TRUE
56353 ripley 1053
 - attr(*, "class")= chr "histogram"
77426 ripley 1054
> 
61157 ripley 1055
> hist(sqrt(islands), breaks = 12, col = "lightblue", border = "pink")
56353 ripley 1056
> ##-- For non-equidistant breaks, counts should NOT be graphed unscaled:
1057
> r <- hist(sqrt(islands), breaks = c(4*0:5, 10*3:5, 70, 100, 140),
61169 ripley 1058
+           col = "blue1")
1059
> text(r$mids, r$density, r$counts, adj = c(.5, -.5), col = "blue3")
56353 ripley 1060
> sapply(r[2:3], sum)
77426 ripley 1061
   counts   density 
1062
48.000000  0.215625 
56353 ripley 1063
> sum(r$density * diff(r$breaks)) # == 1
1064
[1] 1
1065
> lines(r, lty = 3, border = "purple") # -> lines.histogram(*)
1066
> par(op)
77426 ripley 1067
> 
56353 ripley 1068
> require(utils) # for str
61157 ripley 1069
> str(hist(islands, breaks = 12, plot =  FALSE)) #-> 10 (~= 12) breaks
61587 ripley 1070
List of 6
1071
 $ breaks  : num [1:10] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000
1072
 $ counts  : int [1:9] 41 2 1 1 1 1 0 0 1
1073
 $ density : num [1:9] 4.27e-04 2.08e-05 1.04e-05 1.04e-05 1.04e-05 ...
1074
 $ mids    : num [1:9] 1000 3000 5000 7000 9000 11000 13000 15000 17000
1075
 $ xname   : chr "islands"
1076
 $ equidist: logi TRUE
56353 ripley 1077
 - attr(*, "class")= chr "histogram"
61157 ripley 1078
> str(hist(islands, breaks = c(12,20,36,80,200,1000,17000), plot = FALSE))
61587 ripley 1079
List of 6
1080
 $ breaks  : num [1:7] 12 20 36 80 200 1000 17000
1081
 $ counts  : int [1:6] 12 11 8 6 4 7
1082
 $ density : num [1:6] 0.03125 0.014323 0.003788 0.001042 0.000104 ...
1083
 $ mids    : num [1:6] 16 28 58 140 600 9000
1084
 $ xname   : chr "islands"
1085
 $ equidist: logi FALSE
56353 ripley 1086
 - attr(*, "class")= chr "histogram"
77426 ripley 1087
> 
61157 ripley 1088
> hist(islands, breaks = c(12,20,36,80,200,1000,17000), freq = TRUE,
56353 ripley 1089
+      main = "WRONG histogram") # and warning
1090
Warning in plot.histogram(r, freq = freq1, col = col, border = border, angle = angle,  :
60841 ripley 1091
  the AREAS in the plot are wrong -- rather use 'freq = FALSE'
56353 ripley 1092
> require(stats)
1093
> set.seed(14)
1094
> x <- rchisq(100, df = 4)
77426 ripley 1095
> ## Don't show: 
56353 ripley 1096
> op <- par(mfrow = 2:1, mgp = c(1.5, 0.6, 0), mar = .1 + c(3,3:1))
66943 maechler 1097
> ## End(Don't show)
56353 ripley 1098
> ## Comparing data with a model distribution should be done with qqplot()!
61157 ripley 1099
> qqplot(x, qchisq(ppoints(x), df = 4)); abline(0, 1, col = 2, lty = 2)
77426 ripley 1100
> 
56353 ripley 1101
> ## if you really insist on using hist() ... :
1102
> hist(x, freq = FALSE, ylim = c(0, 0.2))
1103
> curve(dchisq(x, df = 4), col = 2, lty = 2, lwd = 2, add = TRUE)
77426 ripley 1104
> ## Don't show: 
56353 ripley 1105
> par(op)
66943 maechler 1106
> ## End(Don't show)
77426 ripley 1107
> 
1108
> 
1109
> 
56353 ripley 1110
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1111
> cleanEx()
1112
> nameEx("identify")
1113
> ### * identify
77426 ripley 1114
> 
56353 ripley 1115
> flush(stderr()); flush(stdout())
77426 ripley 1116
> 
56353 ripley 1117
> ### Name: identify
1118
> ### Title: Identify Points in a Scatter Plot
1119
> ### Aliases: identify identify.default
1120
> ### Keywords: iplot
77426 ripley 1121
> 
56353 ripley 1122
> ### ** Examples
77426 ripley 1123
> 
56353 ripley 1124
> ## A function to use identify to select points, and overplot the
1125
> ## points with another symbol as they are selected
72569 ripley 1126
> identifyPch <- function(x, y = NULL, n = length(x), plot = FALSE, pch = 19, ...)
56353 ripley 1127
+ {
1128
+     xy <- xy.coords(x, y); x <- xy$x; y <- xy$y
72569 ripley 1129
+     sel <- rep(FALSE, length(x))
56353 ripley 1130
+     while(sum(sel) < n) {
72569 ripley 1131
+         ans <- identify(x[!sel], y[!sel], labels = which(!sel), n = 1, plot = plot, ...)
56353 ripley 1132
+         if(!length(ans)) break
1133
+         ans <- which(!sel)[ans]
1134
+         points(x[ans], y[ans], pch = pch)
1135
+         sel[ans] <- TRUE
1136
+     }
72569 ripley 1137
+     ## return indices of selected points
1138
+     which(sel)
56353 ripley 1139
+ }
77426 ripley 1140
> 
72569 ripley 1141
> if(dev.interactive()) { ## use it
1142
+   x <- rnorm(50); y <- rnorm(50)
1143
+   plot(x,y); identifyPch(x,y) # how fast to get all?
1144
+ }
77426 ripley 1145
> 
1146
> 
1147
> 
1148
> 
56353 ripley 1149
> cleanEx()
1150
> nameEx("image")
1151
> ### * image
77426 ripley 1152
> 
56353 ripley 1153
> flush(stderr()); flush(stdout())
77426 ripley 1154
> 
56353 ripley 1155
> ### Name: image
1156
> ### Title: Display a Color Image
1157
> ### Aliases: image image.default
1158
> ### Keywords: hplot aplot
77426 ripley 1159
> 
56353 ripley 1160
> ### ** Examples
77426 ripley 1161
> 
76277 murrell 1162
> require("grDevices") # for colours
1163
> x <- y <- seq(-4*pi, 4*pi, length.out = 27)
56353 ripley 1164
> r <- sqrt(outer(x^2, y^2, "+"))
76277 murrell 1165
> image(z = z <- cos(r^2)*exp(-r/6), col = gray.colors(33))
56353 ripley 1166
> image(z, axes = FALSE, main = "Math can be beautiful ...",
1167
+       xlab = expression(cos(r^2) * e^{-r/6}))
1168
> contour(z, add = TRUE, drawlabels = FALSE)
77426 ripley 1169
> 
56353 ripley 1170
> # Volcano data visualized as matrix. Need to transpose and flip
1171
> # matrix horizontally.
1172
> image(t(volcano)[ncol(volcano):1,])
77426 ripley 1173
> 
56353 ripley 1174
> # A prettier display of the volcano
1175
> x <- 10*(1:nrow(volcano))
1176
> y <- 10*(1:ncol(volcano))
76277 murrell 1177
> image(x, y, volcano, col = hcl.colors(100, "terrain"), axes = FALSE)
56353 ripley 1178
> contour(x, y, volcano, levels = seq(90, 200, by = 5),
76277 murrell 1179
+         add = TRUE, col = "brown")
56353 ripley 1180
> axis(1, at = seq(100, 800, by = 100))
1181
> axis(2, at = seq(100, 600, by = 100))
1182
> box()
1183
> title(main = "Maunga Whau Volcano", font.main = 4)
77426 ripley 1184
> 
1185
> 
1186
> 
56353 ripley 1187
> cleanEx()
1188
> nameEx("layout")
1189
> ### * layout
77426 ripley 1190
> 
56353 ripley 1191
> flush(stderr()); flush(stdout())
77426 ripley 1192
> 
56353 ripley 1193
> ### Name: layout
1194
> ### Title: Specifying Complex Plot Arrangements
1195
> ### Aliases: layout layout.show lcm
1196
> ### Keywords: iplot dplot environment
77426 ripley 1197
> 
56353 ripley 1198
> ### ** Examples
77426 ripley 1199
> 
56353 ripley 1200
> def.par <- par(no.readonly = TRUE) # save default, for resetting...
77426 ripley 1201
> 
56353 ripley 1202
> ## divide the device into two rows and two columns
1203
> ## allocate figure 1 all of row 1
1204
> ## allocate figure 2 the intersection of column 2 and row 2
1205
> layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE))
1206
> ## show the regions that have been allocated to each plot
1207
> layout.show(2)
77426 ripley 1208
> 
56353 ripley 1209
> ## divide device into two rows and two columns
1210
> ## allocate figure 1 and figure 2 as above
1211
> ## respect relations between widths and heights
61157 ripley 1212
> nf <- layout(matrix(c(1,1,0,2), 2, 2, byrow = TRUE), respect = TRUE)
56353 ripley 1213
> layout.show(nf)
77426 ripley 1214
> 
56353 ripley 1215
> ## create single figure which is 5cm square
61157 ripley 1216
> nf <- layout(matrix(1), widths = lcm(5), heights = lcm(5))
56353 ripley 1217
> layout.show(nf)
77426 ripley 1218
> 
1219
> 
56353 ripley 1220
> ##-- Create a scatterplot with marginal histograms -----
77426 ripley 1221
> 
56353 ripley 1222
> x <- pmin(3, pmax(-3, stats::rnorm(50)))
1223
> y <- pmin(3, pmax(-3, stats::rnorm(50)))
61157 ripley 1224
> xhist <- hist(x, breaks = seq(-3,3,0.5), plot = FALSE)
1225
> yhist <- hist(y, breaks = seq(-3,3,0.5), plot = FALSE)
56353 ripley 1226
> top <- max(c(xhist$counts, yhist$counts))
61169 ripley 1227
> xrange <- c(-3, 3)
1228
> yrange <- c(-3, 3)
61157 ripley 1229
> nf <- layout(matrix(c(2,0,1,3),2,2,byrow = TRUE), c(3,1), c(1,3), TRUE)
56353 ripley 1230
> layout.show(nf)
77426 ripley 1231
> 
61157 ripley 1232
> par(mar = c(3,3,1,1))
1233
> plot(x, y, xlim = xrange, ylim = yrange, xlab = "", ylab = "")
1234
> par(mar = c(0,3,1,1))
1235
> barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0)
1236
> par(mar = c(3,0,1,1))
1237
> barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE)
77426 ripley 1238
> 
61169 ripley 1239
> par(def.par)  #- reset to default
77426 ripley 1240
> 
1241
> 
1242
> 
56353 ripley 1243
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1244
> cleanEx()
1245
> nameEx("legend")
1246
> ### * legend
77426 ripley 1247
> 
56353 ripley 1248
> flush(stderr()); flush(stdout())
77426 ripley 1249
> 
56353 ripley 1250
> ### Name: legend
1251
> ### Title: Add Legends to Plots
1252
> ### Aliases: legend
1253
> ### Keywords: aplot
77426 ripley 1254
> 
56353 ripley 1255
> ### ** Examples
77426 ripley 1256
> 
56353 ripley 1257
> ## Run the example in '?matplot' or the following:
1258
> leg.txt <- c("Setosa     Petals", "Setosa     Sepals",
1259
+              "Versicolor Petals", "Versicolor Sepals")
1260
> y.leg <- c(4.5, 3, 2.1, 1.4, .7)
1261
> cexv  <- c(1.2, 1, 4/5, 2/3, 1/2)
61255 ripley 1262
> matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width",
56353 ripley 1263
+         main = "Petal and Sepal Dimensions in Iris Blossoms")
1264
> for (i in seq(cexv)) {
61255 ripley 1265
+   text  (1, y.leg[i] - 0.1, paste("cex=", formatC(cexv[i])), cex = 0.8, adj = 0)
56353 ripley 1266
+   legend(3, y.leg[i], leg.txt, pch = "sSvV", col = c(1, 3), cex = cexv[i])
1267
+ }
75448 maechler 1268
> ## cex *vector* [in R <= 3.5.1 has 'if(xc < 0)' w/ length(xc) == 2]
1269
> legend(6,1, leg.txt, pch = "sSvV", col = c(1, 3), cex = 1+(-1:2)/8)
77426 ripley 1270
> 
56353 ripley 1271
> ## 'merge = TRUE' for merging lines & points:
1272
> x <- seq(-pi, pi, len = 65)
1273
> plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2)
1274
> points(x, cos(x), pch = 3, col = 4)
1275
> lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
61258 ripley 1276
> title("legend(..., lty = c(2, -1, 1), pch = c(NA, 3, 4), merge = TRUE)",
56353 ripley 1277
+       cex.main = 1.1)
61255 ripley 1278
> legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
61258 ripley 1279
+        text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
61169 ripley 1280
+        merge = TRUE, bg = "gray90")
77426 ripley 1281
> 
56353 ripley 1282
> ## right-justifying a set of labels: thanks to Uwe Ligges
1283
> x <- 1:5; y1 <- 1/x; y2 <- 2/x
61157 ripley 1284
> plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y")
1285
> lines(x, y1); lines(x, y2, lty = 2)
56353 ripley 1286
> temp <- legend("topright", legend = c(" ", " "),
1287
+                text.width = strwidth("1,000,000"),
1288
+                lty = 1:2, xjust = 1, yjust = 1,
1289
+                title = "Line Types")
1290
> text(temp$rect$left + temp$rect$w, temp$text$y,
61157 ripley 1291
+      c("1,000", "1,000,000"), pos = 2)
77426 ripley 1292
> 
1293
> 
56353 ripley 1294
> ##--- log scaled Examples ------------------------------
1295
> leg.txt <- c("a one", "a two")
77426 ripley 1296
> 
61255 ripley 1297
> par(mfrow = c(2, 2))
56353 ripley 1298
> for(ll in c("","x","y","xy")) {
61157 ripley 1299
+   plot(2:10, log = ll, main = paste0("log = '", ll, "'"))
1300
+   abline(1, 1)
61255 ripley 1301
+   lines(2:3, 3:4, col = 2)
1302
+   points(2, 2, col = 3)
61157 ripley 1303
+   rect(2, 3, 3, 2, col = 4)
1304
+   text(c(3,3), 2:3, c("rect(2,3,3,2, col=4)",
61255 ripley 1305
+                       "text(c(3,3),2:3,\"c(rect(...)\")"), adj = c(0, 0.3))
61157 ripley 1306
+   legend(list(x = 2,y = 8), legend = leg.txt, col = 2:3, pch = 1:2,
61169 ripley 1307
+          lty = 1, merge = TRUE)   #, trace = TRUE)
56353 ripley 1308
+ }
61157 ripley 1309
> par(mfrow = c(1,1))
77426 ripley 1310
> 
56353 ripley 1311
> ##-- Math expressions:  ------------------------------
1312
> x <- seq(-pi, pi, len = 65)
61157 ripley 1313
> plot(x, sin(x), type = "l", col = 2, xlab = expression(phi),
56353 ripley 1314
+      ylab = expression(f(phi)))
61157 ripley 1315
> abline(h = -1:1, v = pi/2*(-6:6), col = "gray90")
56353 ripley 1316
> lines(x, cos(x), col = 3, lty = 2)
61169 ripley 1317
> ex.cs1 <- expression(plain(sin) * phi,  paste("cos", phi))  # 2 ways
61157 ripley 1318
> utils::str(legend(-3, .9, ex.cs1, lty = 1:2, plot = FALSE,
61255 ripley 1319
+            adj = c(0, 0.6)))  # adj y !
56353 ripley 1320
List of 2
1321
 $ rect:List of 4
1322
  ..$ w   : num 1.2
1323
  ..$ h   : num 0.251
1324
  ..$ left: num -3
1325
  ..$ top : num 0.9
1326
 $ text:List of 2
1327
  ..$ x: num [1:2] -2.29 -2.29
1328
  ..$ y: num [1:2] 0.816 0.733
61255 ripley 1329
> legend(-3, 0.9, ex.cs1, lty = 1:2, col = 2:3,  adj = c(0, 0.6))
77426 ripley 1330
> 
56353 ripley 1331
> require(stats)
1332
> x <- rexp(100, rate = .5)
1333
> hist(x, main = "Mean and Median of a Skewed Distribution")
61157 ripley 1334
> abline(v = mean(x),   col = 2, lty = 2, lwd = 2)
1335
> abline(v = median(x), col = 3, lty = 3, lwd = 2)
1336
> ex12 <- expression(bar(x) == sum(over(x[i], n), i == 1, n),
1337
+                    hat(x) == median(x[i], i == 1, n))
61255 ripley 1338
> utils::str(legend(4.1, 30, ex12, col = 2:3, lty = 2:3, lwd = 2))
56353 ripley 1339
List of 2
1340
 $ rect:List of 4
1341
  ..$ w   : num 4.27
1342
  ..$ h   : num 6.78
1343
  ..$ left: num 4.1
1344
  ..$ top : num 30
1345
 $ text:List of 2
1346
  ..$ x: num [1:2] 5.22 5.22
1347
  ..$ y: num [1:2] 27.3 24.6
77426 ripley 1348
> 
56353 ripley 1349
> ## 'Filled' boxes -- for more, see example(plot.factor)
61157 ripley 1350
> op <- par(bg = "white") # to get an opaque box for the legend
56353 ripley 1351
> plot(cut(weight, 3) ~ group, data = PlantGrowth, col = NULL,
1352
+      density = 16*(1:3))
1353
> par(op)
77426 ripley 1354
> 
56353 ripley 1355
> ## Using 'ncol' :
1356
> x <- 0:64/64
1357
> matplot(x, outer(x, 1:7, function(x, k) sin(k * pi * x)),
1358
+         type = "o", col = 1:7, ylim = c(-1, 1.5), pch = "*")
61157 ripley 1359
> op <- par(bg = "antiquewhite1")
1360
> legend(0, 1.5, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
56353 ripley 1361
+        pch = "*", ncol = 4, cex = 0.8)
61157 ripley 1362
> legend(.8,1.2, paste("sin(", 1:7, "pi * x)"), col = 1:7, lty = 1:7,
56353 ripley 1363
+        pch = "*", cex = 0.8)
61157 ripley 1364
> legend(0, -.1, paste("sin(", 1:4, "pi * x)"), col = 1:4, lty = 1:4,
56353 ripley 1365
+        ncol = 2, cex = 0.8)
61157 ripley 1366
> legend(0, -.4, paste("sin(", 5:7, "pi * x)"), col = 4:6,  pch = 24,
56353 ripley 1367
+        ncol = 2, cex = 1.5, lwd = 2, pt.bg = "pink", pt.cex = 1:3)
1368
> par(op)
77426 ripley 1369
> 
56353 ripley 1370
> ## point covering line :
1371
> y <- sin(3*pi*x)
61157 ripley 1372
> plot(x, y, type = "l", col = "blue",
56353 ripley 1373
+     main = "points with bg & legend(*, pt.bg)")
61157 ripley 1374
> points(x, y, pch = 21, bg = "white")
1375
> legend(.4,1, "sin(c x)", pch = 21, pt.bg = "white", lty = 1, col = "blue")
77426 ripley 1376
> 
56353 ripley 1377
> ## legends with titles at different locations
61169 ripley 1378
> plot(x, y, type = "n")
75448 maechler 1379
> legend("bottomright", "(x,y)", pch=1, title= "bottomright")
1380
> legend("bottom",      "(x,y)", pch=1, title= "bottom")
1381
> legend("bottomleft",  "(x,y)", pch=1, title= "bottomleft")
1382
> legend("left",        "(x,y)", pch=1, title= "left")
1383
> legend("topleft",     "(x,y)", pch=1, title= "topleft, inset = .05", inset = .05)
1384
> legend("top",         "(x,y)", pch=1, title= "top")
1385
> legend("topright",    "(x,y)", pch=1, title= "topright, inset = .02",inset = .02)
1386
> legend("right",       "(x,y)", pch=1, title= "right")
1387
> legend("center",      "(x,y)", pch=1, title= "center")
77426 ripley 1388
> 
57622 maechler 1389
> # using text.font (and text.col):
61255 ripley 1390
> op <- par(mfrow = c(2, 2), mar = rep(2.1, 4))
57622 maechler 1391
> c6 <- terrain.colors(10)[1:6]
1392
> for(i in 1:4) {
61157 ripley 1393
+    plot(1, type = "n", axes = FALSE, ann = FALSE); title(paste("text.font =",i))
57622 maechler 1394
+    legend("top", legend = LETTERS[1:6], col = c6,
61157 ripley 1395
+           ncol = 2, cex = 2, lwd = 3, text.font = i, text.col = c6)
57622 maechler 1396
+ }
1397
> par(op)
77426 ripley 1398
> 
1399
> 
1400
> 
56353 ripley 1401
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1402
> cleanEx()
1403
> nameEx("lines")
1404
> ### * lines
77426 ripley 1405
> 
56353 ripley 1406
> flush(stderr()); flush(stdout())
77426 ripley 1407
> 
56353 ripley 1408
> ### Name: lines
1409
> ### Title: Add Connected Line Segments to a Plot
1410
> ### Aliases: lines lines.default
1411
> ### Keywords: aplot
77426 ripley 1412
> 
56353 ripley 1413
> ### ** Examples
77426 ripley 1414
> 
56353 ripley 1415
> # draw a smooth line through a scatter plot
61157 ripley 1416
> plot(cars, main = "Stopping Distance versus Speed")
56353 ripley 1417
> lines(stats::lowess(cars))
77426 ripley 1418
> 
1419
> 
1420
> 
56353 ripley 1421
> cleanEx()
1422
> nameEx("matplot")
1423
> ### * matplot
77426 ripley 1424
> 
56353 ripley 1425
> flush(stderr()); flush(stdout())
77426 ripley 1426
> 
56353 ripley 1427
> ### Name: matplot
1428
> ### Title: Plot Columns of Matrices
1429
> ### Aliases: matplot matpoints matlines
1430
> ### Keywords: hplot aplot array
77426 ripley 1431
> 
56353 ripley 1432
> ### ** Examples
77426 ripley 1433
> 
56353 ripley 1434
> require(grDevices)
1435
> matplot((-4:5)^2, main = "Quadratic") # almost identical to plot(*)
1436
> sines <- outer(1:20, 1:4, function(x, y) sin(x / 20 * pi * y))
1437
> matplot(sines, pch = 1:4, type = "o", col = rainbow(ncol(sines)))
1438
> matplot(sines, type = "b", pch = 21:23, col = 2:5, bg = 2:5,
1439
+         main = "matplot(...., pch = 21:23, bg = 2:5)")
77426 ripley 1440
> 
56353 ripley 1441
> x <- 0:50/50
1442
> matplot(x, outer(x, 1:8, function(x, k) sin(k*pi * x)),
1443
+         ylim = c(-2,2), type = "plobcsSh",
1444
+         main= "matplot(,type = \"plobcsSh\" )")
1445
> ## pch & type =  vector of 1-chars :
1446
> matplot(x, outer(x, 1:4, function(x, k) sin(k*pi * x)),
1447
+         pch = letters[1:4], type = c("b","p","o"))
77426 ripley 1448
> 
56353 ripley 1449
> lends <- c("round","butt","square")
1450
> matplot(matrix(1:12, 4), type="c", lty=1, lwd=10, lend=lends)
1451
> text(cbind(2.5, 2*c(1,3,5)-.4), lends, col= 1:3, cex = 1.5)
77426 ripley 1452
> 
56353 ripley 1453
> table(iris$Species) # is data.frame with 'Species' factor
1454
 
77426 ripley 1455
    setosa versicolor  virginica 
1456
        50         50         50 
56353 ripley 1457
> iS <- iris$Species == "setosa"
1458
> iV <- iris$Species == "versicolor"
1459
> op <- par(bg = "bisque")
61157 ripley 1460
> matplot(c(1, 8), c(0, 4.5), type =  "n", xlab = "Length", ylab = "Width",
56353 ripley 1461
+         main = "Petal and Sepal Dimensions in Iris Blossoms")
1462
> matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = "sS", col = c(2,4))
1463
> matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = "vV", col = c(2,4))
1464
> legend(1, 4, c("    Setosa Petals", "    Setosa Sepals",
1465
+                "Versicolor Petals", "Versicolor Sepals"),
1466
+        pch = "sSvV", col = rep(c(2,4), 2))
77426 ripley 1467
> 
56353 ripley 1468
> nam.var <- colnames(iris)[-5]
1469
> nam.spec <- as.character(iris[1+50*0:2, "Species"])
1470
> iris.S <- array(NA, dim = c(50,4,3),
1471
+                 dimnames = list(NULL, nam.var, nam.spec))
1472
> for(i in 1:3) iris.S[,,i] <- data.matrix(iris[1:50+50*(i-1), -5])
77426 ripley 1473
> 
61169 ripley 1474
> matplot(iris.S[, "Petal.Length",], iris.S[, "Petal.Width",], pch = "SCV",
61157 ripley 1475
+         col = rainbow(3, start = 0.8, end = 0.1),
56353 ripley 1476
+         sub = paste(c("S", "C", "V"), dimnames(iris.S)[[3]],
1477
+                     sep = "=", collapse= ",  "),
1478
+         main = "Fisher's Iris Data")
1479
> par(op)
77426 ripley 1480
> 
1481
> 
1482
> 
56353 ripley 1483
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1484
> cleanEx()
1485
> nameEx("mosaicplot")
1486
> ### * mosaicplot
77426 ripley 1487
> 
56353 ripley 1488
> flush(stderr()); flush(stdout())
77426 ripley 1489
> 
56353 ripley 1490
> ### Name: mosaicplot
1491
> ### Title: Mosaic Plots
1492
> ### Aliases: mosaicplot mosaicplot.default mosaicplot.formula
1493
> ### Keywords: hplot
77426 ripley 1494
> 
56353 ripley 1495
> ### ** Examples
77426 ripley 1496
> 
56353 ripley 1497
> require(stats)
1498
> mosaicplot(Titanic, main = "Survival on the Titanic", color = TRUE)
1499
> ## Formula interface for tabulated data:
1500
> mosaicplot(~ Sex + Age + Survived, data = Titanic, color = TRUE)
77426 ripley 1501
> 
56353 ripley 1502
> mosaicplot(HairEyeColor, shade = TRUE)
1503
> ## Independence model of hair and eye color and sex.  Indicates that
61435 ripley 1504
> ## there are more blue eyed blonde females than expected in the case
56353 ripley 1505
> ## of independence and too few brown eyed blonde females.
1506
> ## The corresponding model is:
1507
> fm <- loglin(HairEyeColor, list(1, 2, 3))
77426 ripley 1508
2 iterations: deviation 5.684342e-14 
56353 ripley 1509
> pchisq(fm$pearson, fm$df, lower.tail = FALSE)
1510
[1] 5.320872e-23
77426 ripley 1511
> 
56353 ripley 1512
> mosaicplot(HairEyeColor, shade = TRUE, margin = list(1:2, 3))
1513
> ## Model of joint independence of sex from hair and eye color.  Males
1514
> ## are underrepresented among people with brown hair and eyes, and are
1515
> ## overrepresented among people with brown hair and blue eyes.
1516
> ## The corresponding model is:
1517
> fm <- loglin(HairEyeColor, list(1:2, 3))
77426 ripley 1518
2 iterations: deviation 5.684342e-14 
56353 ripley 1519
> pchisq(fm$pearson, fm$df, lower.tail = FALSE)
1520
[1] 0.1891745
77426 ripley 1521
> 
56353 ripley 1522
> ## Formula interface for raw data: visualize cross-tabulation of numbers
1523
> ## of gears and carburettors in Motor Trend car data.
1524
> mosaicplot(~ gear + carb, data = mtcars, color = TRUE, las = 1)
1525
> # color recycling
1526
> mosaicplot(~ gear + carb, data = mtcars, color = 2:3, las = 1)
77426 ripley 1527
> 
1528
> 
1529
> 
56353 ripley 1530
> cleanEx()
1531
> nameEx("mtext")
1532
> ### * mtext
77426 ripley 1533
> 
56353 ripley 1534
> flush(stderr()); flush(stdout())
77426 ripley 1535
> 
56353 ripley 1536
> ### Name: mtext
1537
> ### Title: Write Text into the Margins of a Plot
1538
> ### Aliases: mtext
1539
> ### Keywords: aplot
77426 ripley 1540
> 
56353 ripley 1541
> ### ** Examples
77426 ripley 1542
> 
61157 ripley 1543
> plot(1:10, (-4:5)^2, main = "Parabola Points", xlab = "xlab")
56353 ripley 1544
> mtext("10 of them")
1545
> for(s in 1:4)
61169 ripley 1546
+     mtext(paste("mtext(..., line= -1, {side, col, font} = ", s,
56353 ripley 1547
+           ", cex = ", (1+s)/2, ")"), line = -1,
61157 ripley 1548
+           side = s, col = s, font = s, cex = (1+s)/2)
56353 ripley 1549
> mtext("mtext(..., line= -2)", line = -2)
61157 ripley 1550
> mtext("mtext(..., line= -2, adj = 0)", line = -2, adj = 0)
56353 ripley 1551
> ##--- log axis :
61169 ripley 1552
> plot(1:10, exp(1:10), log = "y", main = "log =\"y\"", xlab = "xlab")
61157 ripley 1553
> for(s in 1:4) mtext(paste("mtext(...,side=", s ,")"), side = s)
77426 ripley 1554
> 
1555
> 
1556
> 
56353 ripley 1557
> cleanEx()
1558
> nameEx("pairs")
1559
> ### * pairs
77426 ripley 1560
> 
56353 ripley 1561
> flush(stderr()); flush(stdout())
77426 ripley 1562
> 
56353 ripley 1563
> ### Name: pairs
1564
> ### Title: Scatterplot Matrices
1565
> ### Aliases: pairs pairs.default pairs.formula
1566
> ### Keywords: hplot
77426 ripley 1567
> 
56353 ripley 1568
> ### ** Examples
77426 ripley 1569
> 
56353 ripley 1570
> pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",
1571
+       pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)])
77426 ripley 1572
> 
74871 maechler 1573
> ## formula method, "graph" layout (row 1 at bottom):
1574
> pairs(~ Fertility + Education + Catholic, data = swiss, row1attop=FALSE,
56353 ripley 1575
+       subset = Education < 20, main = "Swiss data, Education < 20")
77426 ripley 1576
> 
74871 maechler 1577
> pairs(USJudgeRatings, gap=1/10) # (gap: not wasting plotting area)
61011 maechler 1578
> ## show only lower triangle (and suppress labeling for whatever reason):
61157 ripley 1579
> pairs(USJudgeRatings, text.panel = NULL, upper.panel = NULL)
77426 ripley 1580
> 
56353 ripley 1581
> ## put histograms on the diagonal
1582
> panel.hist <- function(x, ...)
1583
+ {
1584
+     usr <- par("usr"); on.exit(par(usr))
1585
+     par(usr = c(usr[1:2], 0, 1.5) )
1586
+     h <- hist(x, plot = FALSE)
1587
+     breaks <- h$breaks; nB <- length(breaks)
1588
+     y <- h$counts; y <- y/max(y)
61157 ripley 1589
+     rect(breaks[-nB], 0, breaks[-1], y, col = "cyan", ...)
56353 ripley 1590
+ }
61157 ripley 1591
> pairs(USJudgeRatings[1:5], panel = panel.smooth,
74871 maechler 1592
+       cex = 1.5, pch = 24, bg = "light blue", horOdd=TRUE,
61157 ripley 1593
+       diag.panel = panel.hist, cex.labels = 2, font.labels = 2)
77426 ripley 1594
> 
56353 ripley 1595
> ## put (absolute) correlations on the upper panels,
1596
> ## with size proportional to the correlations.
61157 ripley 1597
> panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...)
56353 ripley 1598
+ {
1599
+     usr <- par("usr"); on.exit(par(usr))
1600
+     par(usr = c(0, 1, 0, 1))
1601
+     r <- abs(cor(x, y))
61157 ripley 1602
+     txt <- format(c(r, 0.123456789), digits = digits)[1]
1603
+     txt <- paste0(prefix, txt)
56353 ripley 1604
+     if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
1605
+     text(0.5, 0.5, txt, cex = cex.cor * r)
1606
+ }
74871 maechler 1607
> pairs(USJudgeRatings, lower.panel = panel.smooth, upper.panel = panel.cor,
1608
+       gap=0, row1attop=FALSE)
77426 ripley 1609
> 
59643 ripley 1610
> pairs(iris[-5], log = "xy") # plot all variables on log scale
62705 maechler 1611
> pairs(iris, log = 1:4, # log the first four
1612
+       main = "Lengths and Widths in [log]", line.main=1.5, oma=c(2,2,3,2))
77426 ripley 1613
> 
1614
> 
1615
> 
56353 ripley 1616
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1617
> cleanEx()
1618
> nameEx("panel.smooth")
1619
> ### * panel.smooth
77426 ripley 1620
> 
56353 ripley 1621
> flush(stderr()); flush(stdout())
77426 ripley 1622
> 
56353 ripley 1623
> ### Name: panel.smooth
1624
> ### Title: Simple Panel Plot
1625
> ### Aliases: panel.smooth
1626
> ### Keywords: hplot dplot
77426 ripley 1627
> 
56353 ripley 1628
> ### ** Examples
77426 ripley 1629
> 
61169 ripley 1630
> pairs(swiss, panel = panel.smooth, pch = ".")  # emphasize the smooths
77336 murrell 1631
> pairs(swiss, panel = panel.smooth, lwd = 2, cex = 1.5, col = 4)  # hmm...
77426 ripley 1632
> 
1633
> 
1634
> 
56353 ripley 1635
> cleanEx()
1636
> nameEx("par")
1637
> ### * par
77426 ripley 1638
> 
56353 ripley 1639
> flush(stderr()); flush(stdout())
77426 ripley 1640
> 
56353 ripley 1641
> ### Name: par
1642
> ### Title: Set or Query Graphical Parameters
1643
> ### Aliases: par .Pars 'graphical parameter' 'graphical parameters'
1644
> ### Keywords: iplot dplot environment
77426 ripley 1645
> 
56353 ripley 1646
> ### ** Examples
77426 ripley 1647
> 
56353 ripley 1648
> op <- par(mfrow = c(2, 2), # 2 x 2 pictures on one plot
1649
+           pty = "s")       # square plotting region,
1650
>                            # independent of device size
77426 ripley 1651
> 
56353 ripley 1652
> ## At end of plotting, reset to previous settings:
1653
> par(op)
77426 ripley 1654
> 
56353 ripley 1655
> ## Alternatively,
1656
> op <- par(no.readonly = TRUE) # the whole list of settable par's.
1657
> ## do lots of plotting and par(.) calls, then reset:
1658
> par(op)
1659
> ## Note this is not in general good practice
77426 ripley 1660
> 
56353 ripley 1661
> par("ylog") # FALSE
1662
[1] FALSE
1663
> plot(1 : 12, log = "y")
1664
> par("ylog") # TRUE
1665
[1] TRUE
77426 ripley 1666
> 
56353 ripley 1667
> plot(1:2, xaxs = "i") # 'inner axis' w/o extra space
1668
> par(c("usr", "xaxp"))
1669
$usr
1670
[1] 1.00 2.00 0.96 2.04
1671
 
1672
$xaxp
1673
[1] 1 2 5
1674
 
77426 ripley 1675
> 
56353 ripley 1676
> ( nr.prof <-
61169 ripley 1677
+ c(prof.pilots = 16, lawyers = 11, farmers = 10, salesmen = 9, physicians = 9,
1678
+   mechanics = 6, policemen = 6, managers = 6, engineers = 5, teachers = 4,
1679
+   housewives = 3, students = 3, armed.forces = 1))
77426 ripley 1680
 prof.pilots      lawyers      farmers     salesmen   physicians    mechanics 
1681
          16           11           10            9            9            6 
1682
   policemen     managers    engineers     teachers   housewives     students 
1683
           6            6            5            4            3            3 
1684
armed.forces 
1685
           1 
56353 ripley 1686
> par(las = 3)
1687
> barplot(rbind(nr.prof)) # R 0.63.2: shows alignment problem
61169 ripley 1688
> par(las = 0)  # reset to default
77426 ripley 1689
> 
56353 ripley 1690
> require(grDevices) # for gray
1691
> ## 'fg' use:
61157 ripley 1692
> plot(1:12, type = "b", main = "'fg' : axes, ticks and box in gray",
1693
+      fg = gray(0.7), bty = "7" , sub = R.version.string)
77426 ripley 1694
> 
56353 ripley 1695
> ex <- function() {
1696
+    old.par <- par(no.readonly = TRUE) # all par settings which
1697
+                                       # could be changed.
1698
+    on.exit(par(old.par))
1699
+    ## ...
1700
+    ## ... do lots of par() settings and plots
1701
+    ## ...
1702
+    invisible() #-- now,  par(old.par)  will be executed
1703
+ }
1704
> ex()
77426 ripley 1705
> 
59180 maechler 1706
> ## Line types
1707
> showLty <- function(ltys, xoff = 0, ...) {
1708
+    stopifnot((n <- length(ltys)) >= 1)
1709
+    op <- par(mar = rep(.5,4)); on.exit(par(op))
61169 ripley 1710
+    plot(0:1, 0:1, type = "n", axes = FALSE, ann = FALSE)
59180 maechler 1711
+    y <- (n:1)/(n+1)
1712
+    clty <- as.character(ltys)
62642 hornik 1713
+    mytext <- function(x, y, txt)
1714
+       text(x, y, txt, adj = c(0, -.3), cex = 0.8, ...)
59180 maechler 1715
+    abline(h = y, lty = ltys, ...); mytext(xoff, y, clty)
1716
+    y <- y - 1/(3*(n+1))
62642 hornik 1717
+    abline(h = y, lty = ltys, lwd = 2, ...)
1718
+    mytext(1/8+xoff, y, paste(clty," lwd = 2"))
59180 maechler 1719
+ }
1720
> showLty(c("solid", "dashed", "dotted", "dotdash", "longdash", "twodash"))
61169 ripley 1721
> par(new = TRUE)  # the same:
61157 ripley 1722
> showLty(c("solid", "44", "13", "1343", "73", "2262"), xoff = .2, col = 2)
59180 maechler 1723
> showLty(c("11", "22", "33", "44",   "12", "13", "14",   "21", "31"))
77426 ripley 1724
> 
1725
> 
1726
> 
56353 ripley 1727
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1728
> cleanEx()
1729
> nameEx("persp")
1730
> ### * persp
77426 ripley 1731
> 
56353 ripley 1732
> flush(stderr()); flush(stdout())
77426 ripley 1733
> 
56353 ripley 1734
> ### Name: persp
1735
> ### Title: Perspective Plots
1736
> ### Aliases: persp persp.default
1737
> ### Keywords: hplot aplot
77426 ripley 1738
> 
56353 ripley 1739
> ### ** Examples
77426 ripley 1740
> 
56353 ripley 1741
> require(grDevices) # for trans3d
1742
> ## More examples in  demo(persp) !!
1743
> ##                   -----------
77426 ripley 1744
> 
56353 ripley 1745
> # (1) The Obligatory Mathematical surface.
1746
> #     Rotated sinc function.
77426 ripley 1747
> 
56353 ripley 1748
> x <- seq(-10, 10, length= 30)
1749
> y <- x
61169 ripley 1750
> f <- function(x, y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
56353 ripley 1751
> z <- outer(x, y, f)
1752
> z[is.na(z)] <- 1
1753
> op <- par(bg = "white")
1754
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
1755
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue",
1756
+       ltheta = 120, shade = 0.75, ticktype = "detailed",
1757
+       xlab = "X", ylab = "Y", zlab = "Sinc( r )"
1758
+ ) -> res
1759
> round(res, 3)
1760
      [,1]   [,2]   [,3]   [,4]
1761
[1,] 0.087 -0.025  0.043 -0.043
1762
[2,] 0.050  0.043 -0.075  0.075
1763
[3,] 0.000  0.074  0.042 -0.042
1764
[4,] 0.000 -0.273 -2.890  3.890
77426 ripley 1765
> 
56353 ripley 1766
> # (2) Add to existing persp plot - using trans3d() :
77426 ripley 1767
> 
56353 ripley 1768
> xE <- c(-10,10); xy <- expand.grid(xE, xE)
61157 ripley 1769
> points(trans3d(xy[,1], xy[,2], 6, pmat = res), col = 2, pch = 16)
1770
> lines (trans3d(x, y = 10, z = 6 + sin(x), pmat = res), col = 3)
77426 ripley 1771
> 
56353 ripley 1772
> phi <- seq(0, 2*pi, len = 201)
1773
> r1 <- 7.725 # radius of 2nd maximum
1774
> xr <- r1 * cos(phi)
1775
> yr <- r1 * sin(phi)
1776
> lines(trans3d(xr,yr, f(xr,yr), res), col = "pink", lwd = 2)
1777
> ## (no hidden lines)
77426 ripley 1778
> 
56353 ripley 1779
> # (3) Visualizing a simple DEM model
77426 ripley 1780
> 
56353 ripley 1781
> z <- 2 * volcano        # Exaggerate the relief
1782
> x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
1783
> y <- 10 * (1:ncol(z))   # 10 meter spacing (E to W)
1784
> ## Don't draw the grid lines :  border = NA
1785
> par(bg = "slategray")
1786
> persp(x, y, z, theta = 135, phi = 30, col = "green3", scale = FALSE,
1787
+       ltheta = -120, shade = 0.75, border = NA, box = FALSE)
77426 ripley 1788
> 
56353 ripley 1789
> # (4) Surface colours corresponding to z-values
77426 ripley 1790
> 
56353 ripley 1791
> par(bg = "white")
1792
> x <- seq(-1.95, 1.95, length = 30)
1793
> y <- seq(-1.95, 1.95, length = 35)
61169 ripley 1794
> z <- outer(x, y, function(a, b) a*b^2)
56353 ripley 1795
> nrz <- nrow(z)
1796
> ncz <- ncol(z)
1797
> # Create a function interpolating colors in the range of specified colors
61435 ripley 1798
> jet.colors <- colorRampPalette( c("blue", "green") )
56353 ripley 1799
> # Generate the desired number of colors from this palette
1800
> nbcol <- 100
1801
> color <- jet.colors(nbcol)
1802
> # Compute the z-value at the facet centres
1803
> zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
1804
> # Recode facet z-values into color indices
1805
> facetcol <- cut(zfacet, nbcol)
61157 ripley 1806
> persp(x, y, z, col = color[facetcol], phi = 30, theta = -30)
77426 ripley 1807
> 
56353 ripley 1808
> par(op)
77426 ripley 1809
> 
1810
> 
1811
> 
56353 ripley 1812
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1813
> cleanEx()
1814
> nameEx("pie")
1815
> ### * pie
77426 ripley 1816
> 
56353 ripley 1817
> flush(stderr()); flush(stdout())
77426 ripley 1818
> 
56353 ripley 1819
> ### Name: pie
1820
> ### Title: Pie Charts
1821
> ### Aliases: pie
1822
> ### Keywords: hplot
77426 ripley 1823
> 
56353 ripley 1824
> ### ** Examples
77426 ripley 1825
> 
56353 ripley 1826
> require(grDevices)
1827
> pie(rep(1, 24), col = rainbow(24), radius = 0.9)
77426 ripley 1828
> 
56353 ripley 1829
> pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
1830
> names(pie.sales) <- c("Blueberry", "Cherry",
1831
+     "Apple", "Boston Cream", "Other", "Vanilla Cream")
1832
> pie(pie.sales) # default colours
1833
> pie(pie.sales, col = c("purple", "violetred1", "green3",
1834
+                        "cornsilk", "cyan", "white"))
61157 ripley 1835
> pie(pie.sales, col = gray(seq(0.4, 1.0, length = 6)))
56353 ripley 1836
> pie(pie.sales, density = 10, angle = 15 + 10 * 1:6)
61157 ripley 1837
> pie(pie.sales, clockwise = TRUE, main = "pie(*, clockwise = TRUE)")
61169 ripley 1838
> segments(0, 0, 0, 1, col = "red", lwd = 2)
1839
> text(0, 1, "init.angle = 90", col = "red")
77426 ripley 1840
> 
56353 ripley 1841
> n <- 200
61169 ripley 1842
> pie(rep(1, n), labels = "", col = rainbow(n), border = NA,
56353 ripley 1843
+     main = "pie(*, labels=\"\", col=rainbow(n), border=NA,..")
77426 ripley 1844
> 
68263 ripley 1845
> ## Another case showing pie() is rather fun than science:
1846
> ## (original by FinalBackwardsGlance on http://imgur.com/gallery/wWrpU4X)
67340 maechler 1847
> pie(c(Sky = 78, "Sunny side of pyramid" = 17, "Shady side of pyramid" = 5),
1848
+     init.angle = 315, col = c("deepskyblue", "yellow", "yellow3"), border = FALSE)
77426 ripley 1849
> 
1850
> 
1851
> 
56353 ripley 1852
> cleanEx()
1853
> nameEx("plot")
1854
> ### * plot
77426 ripley 1855
> 
56353 ripley 1856
> flush(stderr()); flush(stdout())
77426 ripley 1857
> 
56353 ripley 1858
> ### Name: plot
1859
> ### Title: Generic X-Y Plotting
1860
> ### Aliases: plot
1861
> ### Keywords: hplot
77426 ripley 1862
> 
56353 ripley 1863
> ### ** Examples
77426 ripley 1864
> 
67689 ripley 1865
> require(stats) # for lowess, rpois, rnorm
56353 ripley 1866
> plot(cars)
1867
> lines(lowess(cars))
77426 ripley 1868
> 
56579 ripley 1869
> plot(sin, -pi, 2*pi) # see ?plot.function
77426 ripley 1870
> 
56353 ripley 1871
> ## Discrete Distribution Plot:
61169 ripley 1872
> plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 10,
1873
+      main = "rpois(100, lambda = 5)")
77426 ripley 1874
> 
56353 ripley 1875
> ## Simple quantiles/ECDF, see ecdf() {library(stats)} for a better one:
1876
> plot(x <- sort(rnorm(47)), type = "s", main = "plot(x, type = \"s\")")
1877
> points(x, cex = .5, col = "dark red")
77426 ripley 1878
> 
1879
> 
1880
> 
56353 ripley 1881
> cleanEx()
1882
> nameEx("plot.dataframe")
1883
> ### * plot.dataframe
77426 ripley 1884
> 
56353 ripley 1885
> flush(stderr()); flush(stdout())
77426 ripley 1886
> 
56353 ripley 1887
> ### Name: plot.data.frame
1888
> ### Title: Plot Method for Data Frames
1889
> ### Aliases: plot.data.frame
1890
> ### Keywords: hplot methods
77426 ripley 1891
> 
56353 ripley 1892
> ### ** Examples
77426 ripley 1893
> 
61157 ripley 1894
> plot(OrchardSprays[1], method = "jitter")
56353 ripley 1895
> plot(OrchardSprays[c(4,1)])
1896
> plot(OrchardSprays)
77426 ripley 1897
> 
56353 ripley 1898
> plot(iris)
1899
> plot(iris[5:4])
1900
> plot(women)
77426 ripley 1901
> 
1902
> 
1903
> 
56353 ripley 1904
> cleanEx()
1905
> nameEx("plot.default")
1906
> ### * plot.default
77426 ripley 1907
> 
56353 ripley 1908
> flush(stderr()); flush(stdout())
77426 ripley 1909
> 
56353 ripley 1910
> ### Name: plot.default
1911
> ### Title: The Default Scatterplot Function
1912
> ### Aliases: plot.default
1913
> ### Keywords: hplot
77426 ripley 1914
> 
56353 ripley 1915
> ### ** Examples
77426 ripley 1916
> 
56353 ripley 1917
> Speed <- cars$speed
1918
> Distance <- cars$dist
61169 ripley 1919
> plot(Speed, Distance, panel.first = grid(8, 8),
56353 ripley 1920
+      pch = 0, cex = 1.2, col = "blue")
1921
> plot(Speed, Distance,
1922
+      panel.first = lines(stats::lowess(Speed, Distance), lty = "dashed"),
1923
+      pch = 0, cex = 1.2, col = "blue")
77426 ripley 1924
> 
56353 ripley 1925
> ## Show the different plot types
1926
> x <- 0:12
1927
> y <- sin(pi/5 * x)
1928
> op <- par(mfrow = c(3,3), mar = .1+ c(2,2,3,1))
1929
> for (tp in c("p","l","b",  "c","o","h",  "s","S","n")) {
61157 ripley 1930
+    plot(y ~ x, type = tp, main = paste0("plot(*, type = \"", tp, "\")"))
56353 ripley 1931
+    if(tp == "S") {
61169 ripley 1932
+       lines(x, y, type = "s", col = "red", lty = 2)
61157 ripley 1933
+       mtext("lines(*, type = \"s\", ...)", col = "red", cex = 0.8)
56353 ripley 1934
+    }
1935
+ }
1936
> par(op)
77426 ripley 1937
> 
56353 ripley 1938
> ##--- Log-Log Plot  with  custom axes
61169 ripley 1939
> lx <- seq(1, 5, length = 41)
56353 ripley 1940
> yl <- expression(e^{-frac(1,2) * {log[10](x)}^2})
1941
> y <- exp(-.5*lx^2)
70685 maechler 1942
> op <- par(mfrow = c(2,1), mar = par("mar")-c(1,0,2,0), mgp = c(2, .7, 0))
61157 ripley 1943
> plot(10^lx, y, log = "xy", type = "l", col = "purple",
1944
+      main = "Log-Log plot", ylab = yl, xlab = "x")
61169 ripley 1945
> plot(10^lx, y, log = "xy", type = "o", pch = ".", col = "forestgreen",
61157 ripley 1946
+      main = "Log-Log plot with custom axes", ylab = yl, xlab = "x",
56353 ripley 1947
+      axes = FALSE, frame.plot = TRUE)
1948
> my.at <- 10^(1:5)
61157 ripley 1949
> axis(1, at = my.at, labels = formatC(my.at, format = "fg"))
70685 maechler 1950
> e.y <- -5:-1 ; at.y <- 10^e.y
1951
> axis(2, at = at.y, col.axis = "red", las = 1,
1952
+      labels = as.expression(lapply(e.y, function(E) bquote(10^.(E)))))
56353 ripley 1953
> par(op)
77426 ripley 1954
> 
1955
> 
1956
> 
56353 ripley 1957
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
1958
> cleanEx()
1959
> nameEx("plot.design")
1960
> ### * plot.design
77426 ripley 1961
> 
56353 ripley 1962
> flush(stderr()); flush(stdout())
77426 ripley 1963
> 
56353 ripley 1964
> ### Name: plot.design
1965
> ### Title: Plot Univariate Effects of a Design or Model
1966
> ### Aliases: plot.design
1967
> ### Keywords: hplot
77426 ripley 1968
> 
56353 ripley 1969
> ### ** Examples
77426 ripley 1970
> 
56353 ripley 1971
> require(stats)
61169 ripley 1972
> plot.design(warpbreaks)  # automatic for data frame with one numeric var.
77426 ripley 1973
> 
56353 ripley 1974
> Form <- breaks ~ wool + tension
1975
> summary(fm1 <- aov(Form, data = warpbreaks))
77426 ripley 1976
            Df Sum Sq Mean Sq F value  Pr(>F)   
1977
wool         1    451   450.7   3.339 0.07361 . 
57044 ripley 1978
tension      2   2034  1017.1   7.537 0.00138 **
77426 ripley 1979
Residuals   50   6748   135.0                   
56353 ripley 1980
---
67211 ripley 1981
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
61169 ripley 1982
> plot.design(       Form, data = warpbreaks, col = 2)  # same as above
77426 ripley 1983
> 
56353 ripley 1984
> ## More than one y :
1985
> utils::str(esoph)
1986
'data.frame':	88 obs. of  5 variables:
1987
 $ agegp    : Ord.factor w/ 6 levels "25-34"<"35-44"<..: 1 1 1 1 1 1 1 1 1 1 ...
1988
 $ alcgp    : Ord.factor w/ 4 levels "0-39g/day"<"40-79"<..: 1 1 1 1 2 2 2 2 3 3 ...
1989
 $ tobgp    : Ord.factor w/ 4 levels "0-9g/day"<"10-19"<..: 1 2 3 4 1 2 3 4 1 2 ...
1990
 $ ncases   : num  0 0 0 0 0 0 0 0 0 0 ...
1991
 $ ncontrols: num  40 10 6 5 27 7 4 7 2 1 ...
1992
> plot.design(esoph) ## two plots; if interactive you are "ask"ed
77426 ripley 1993
> 
56353 ripley 1994
> ## or rather, compare mean and median:
1995
> op <- par(mfcol = 1:2)
1996
> plot.design(ncases/ncontrols ~ ., data = esoph, ylim = c(0, 0.8))
1997
> plot.design(ncases/ncontrols ~ ., data = esoph, ylim = c(0, 0.8),
1998
+             fun = median)
1999
> par(op)
77426 ripley 2000
> 
2001
> 
2002
> 
56353 ripley 2003
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2004
> cleanEx()
2005
> nameEx("plot.factor")
2006
> ### * plot.factor
77426 ripley 2007
> 
56353 ripley 2008
> flush(stderr()); flush(stdout())
77426 ripley 2009
> 
56353 ripley 2010
> ### Name: plot.factor
2011
> ### Title: Plotting Factor Variables
2012
> ### Aliases: plot.factor
2013
> ### Keywords: hplot
77426 ripley 2014
> 
56353 ripley 2015
> ### ** Examples
77426 ripley 2016
> 
56353 ripley 2017
> require(grDevices)
61169 ripley 2018
> plot(weight ~ group, data = PlantGrowth)           # numeric vector ~ factor
2019
> plot(cut(weight, 2) ~ group, data = PlantGrowth)   # factor ~ factor
56353 ripley 2020
> ## passing "..." to spineplot() eventually:
2021
> plot(cut(weight, 3) ~ group, data = PlantGrowth,
2022
+      col = hcl(c(0, 120, 240), 50, 70))
77426 ripley 2023
> 
61169 ripley 2024
> plot(PlantGrowth$group, axes = FALSE, main = "no axes")  # extremely silly
77426 ripley 2025
> 
2026
> 
2027
> 
56353 ripley 2028
> cleanEx()
2029
> nameEx("plot.formula")
2030
> ### * plot.formula
77426 ripley 2031
> 
56353 ripley 2032
> flush(stderr()); flush(stdout())
77426 ripley 2033
> 
56353 ripley 2034
> ### Name: plot.formula
2035
> ### Title: Formula Notation for Scatterplots
2036
> ### Aliases: plot.formula lines.formula points.formula text.formula
2037
> ### Keywords: hplot aplot
77426 ripley 2038
> 
56353 ripley 2039
> ### ** Examples
77426 ripley 2040
> 
56353 ripley 2041
> op <- par(mfrow = c(2,1))
2042
> plot(Ozone ~ Wind, data = airquality, pch = as.character(Month))
2043
> plot(Ozone ~ Wind, data = airquality, pch = as.character(Month),
2044
+      subset = Month != 7)
2045
> par(op)
77426 ripley 2046
> 
56353 ripley 2047
> ## text.formula() can be very natural:
2048
> wb <- within(warpbreaks, {
2049
+     time <- seq_along(breaks); W.T <- wool:tension })
2050
> plot(breaks ~ time, data = wb, type = "b")
2051
> text(breaks ~ time, data = wb, label = W.T, col = 1+as.integer(wool))
77426 ripley 2052
> 
2053
> 
2054
> 
56353 ripley 2055
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2056
> cleanEx()
67197 murrell 2057
> nameEx("plot.raster")
2058
> ### * plot.raster
77426 ripley 2059
> 
67197 murrell 2060
> flush(stderr()); flush(stdout())
77426 ripley 2061
> 
67197 murrell 2062
> ### Name: plot.raster
2063
> ### Title: Plotting Raster Images
2064
> ### Aliases: plot.raster
2065
> ### Keywords: hplot
77426 ripley 2066
> 
67197 murrell 2067
> ### ** Examples
77426 ripley 2068
> 
67197 murrell 2069
> require(grDevices)
2070
> r <- as.raster(c(0.5, 1, 0.5))
2071
> plot(r)
2072
> # additional arguments to rasterImage()
2073
> plot(r, interpolate=FALSE)
2074
> # distort
2075
> plot(r, asp=NA)
2076
> # fill page
2077
> op <- par(mar=rep(0, 4))
2078
> plot(r, asp=NA)
2079
> par(op)
2080
> # normal annotations work
2081
> plot(r, asp=NA)
2082
> box()
2083
> title(main="This is my raster")
2084
> # add to existing plot
2085
> plot(1)
2086
> plot(r, add=TRUE)
77426 ripley 2087
> 
2088
> 
2089
> 
67197 murrell 2090
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2091
> cleanEx()
56353 ripley 2092
> nameEx("plot.table")
2093
> ### * plot.table
77426 ripley 2094
> 
56353 ripley 2095
> flush(stderr()); flush(stdout())
77426 ripley 2096
> 
56353 ripley 2097
> ### Name: plot.table
2098
> ### Title: Plot Methods for 'table' Objects
2099
> ### Aliases: plot.table lines.table points.table
2100
> ### Keywords: hplot category
77426 ripley 2101
> 
56353 ripley 2102
> ### ** Examples
77426 ripley 2103
> 
56353 ripley 2104
> ## 1-d tables
2105
> (Poiss.tab <- table(N = stats::rpois(200, lambda = 5)))
2106
N
77426 ripley 2107
 1  2  3  4  5  6  7  8  9 10 11 
2108
 4 14 25 38 40 33 21 16  4  2  3 
56353 ripley 2109
> plot(Poiss.tab, main = "plot(table(rpois(200, lambda = 5)))")
77426 ripley 2110
> 
56353 ripley 2111
> plot(table(state.division))
77426 ripley 2112
> 
56353 ripley 2113
> ## 4-D :
2114
> plot(Titanic, main ="plot(Titanic, main= *)")
77426 ripley 2115
> 
2116
> 
2117
> 
2118
> 
56353 ripley 2119
> cleanEx()
2120
> nameEx("plot.window")
2121
> ### * plot.window
77426 ripley 2122
> 
56353 ripley 2123
> flush(stderr()); flush(stdout())
77426 ripley 2124
> 
56353 ripley 2125
> ### Name: plot.window
2126
> ### Title: Set up World Coordinates for Graphics Window
2127
> ### Aliases: plot.window xlim ylim asp
2128
> ### Keywords: aplot
77426 ripley 2129
> 
56353 ripley 2130
> ### ** Examples
77426 ripley 2131
> 
56353 ripley 2132
> ##--- An example for the use of 'asp' :
2133
> require(stats)  # normally loaded
2134
> loc <- cmdscale(eurodist)
2135
> rx <- range(x <- loc[,1])
2136
> ry <- range(y <- -loc[,2])
61157 ripley 2137
> plot(x, y, type = "n", asp = 1, xlab = "", ylab = "")
56353 ripley 2138
> abline(h = pretty(rx, 10), v = pretty(ry, 10), col = "lightgray")
61157 ripley 2139
> text(x, y, labels(eurodist), cex = 0.8)
77426 ripley 2140
> 
2141
> 
2142
> 
56353 ripley 2143
> cleanEx()
2144
> nameEx("plot.xy")
2145
> ### * plot.xy
77426 ripley 2146
> 
56353 ripley 2147
> flush(stderr()); flush(stdout())
77426 ripley 2148
> 
56353 ripley 2149
> ### Name: plot.xy
2150
> ### Title: Basic Internal Plot Function
2151
> ### Aliases: plot.xy
2152
> ### Keywords: aplot
77426 ripley 2153
> 
56353 ripley 2154
> ### ** Examples
77426 ripley 2155
> 
56353 ripley 2156
> points.default # to see how it calls "plot.xy(xy.coords(x, y), ...)"
77426 ripley 2157
function (x, y = NULL, type = "p", ...) 
56353 ripley 2158
plot.xy(xy.coords(x, y), type = type, ...)
77431 ripley 2159
<bytecode: 0x26a6dd8>
56353 ripley 2160
<environment: namespace:graphics>
77426 ripley 2161
> 
2162
> 
2163
> 
56353 ripley 2164
> cleanEx()
2165
> nameEx("plothistogram")
2166
> ### * plothistogram
77426 ripley 2167
> 
56353 ripley 2168
> flush(stderr()); flush(stdout())
77426 ripley 2169
> 
56353 ripley 2170
> ### Name: plot.histogram
2171
> ### Title: Plot Histograms
2172
> ### Aliases: plot.histogram lines.histogram
2173
> ### Keywords: hplot iplot
77426 ripley 2174
> 
56353 ripley 2175
> ### ** Examples
77426 ripley 2176
> 
56353 ripley 2177
> (wwt <- hist(women$weight, nclass = 7, plot = FALSE))
2178
$breaks
2179
 [1] 115 120 125 130 135 140 145 150 155 160 165
2180
 
2181
$counts
2182
 [1] 3 1 2 2 1 1 2 1 1 1
2183
 
2184
$density
2185
 [1] 0.04000000 0.01333333 0.02666667 0.02666667 0.01333333 0.01333333
2186
 [7] 0.02666667 0.01333333 0.01333333 0.01333333
2187
 
2188
$mids
2189
 [1] 117.5 122.5 127.5 132.5 137.5 142.5 147.5 152.5 157.5 162.5
2190
 
2191
$xname
2192
[1] "women$weight"
2193
 
2194
$equidist
2195
[1] TRUE
2196
 
2197
attr(,"class")
2198
[1] "histogram"
2199
> plot(wwt, labels = TRUE) # default main & xlab using wwt$xname
2200
> plot(wwt, border = "dark blue", col = "light blue",
2201
+      main = "Histogram of 15 women's weights", xlab = "weight [pounds]")
77426 ripley 2202
> 
56353 ripley 2203
> ## Fake "lines" example, using non-default labels:
2204
> w2 <- wwt; w2$counts <- w2$counts - 1
2205
> lines(w2, col = "Midnight Blue", labels = ifelse(w2$counts, "> 1", "1"))
77426 ripley 2206
> 
2207
> 
2208
> 
56353 ripley 2209
> cleanEx()
2210
> nameEx("points")
2211
> ### * points
77426 ripley 2212
> 
56353 ripley 2213
> flush(stderr()); flush(stdout())
77426 ripley 2214
> 
56353 ripley 2215
> ### Name: points
2216
> ### Title: Add Points to a Plot
2217
> ### Aliases: points points.default pch
2218
> ### Keywords: aplot
77426 ripley 2219
> 
56353 ripley 2220
> ### ** Examples
77426 ripley 2221
> 
56353 ripley 2222
> require(stats) # for rnorm
61169 ripley 2223
> plot(-4:4, -4:4, type = "n")  # setting up coord. system
56353 ripley 2224
> points(rnorm(200), rnorm(200), col = "red")
2225
> points(rnorm(100)/2, rnorm(100)/2, col = "blue", cex = 1.5)
77426 ripley 2226
> 
56353 ripley 2227
> op <- par(bg = "light blue")
61169 ripley 2228
> x <- seq(0, 2*pi, len = 51)
56353 ripley 2229
> ## something "between type='b' and type='o'":
61157 ripley 2230
> plot(x, sin(x), type = "o", pch = 21, bg = par("bg"), col = "blue", cex = .6,
2231
+  main = 'plot(..., type="o", pch=21, bg=par("bg"))')
56353 ripley 2232
> par(op)
77426 ripley 2233
> 
2234
> ## Not run: 
56353 ripley 2235
> ##D ## The figure was produced by calls like
61157 ripley 2236
> ##D png("pch.png", height = 0.7, width = 7, res = 100, units = "in")
56353 ripley 2237
> ##D par(mar = rep(0,4))
61157 ripley 2238
> ##D plot(c(-1, 26), 0:1, type = "n", axes = FALSE)
56353 ripley 2239
> ##D text(0:25, 0.6, 0:25, cex = 0.5)
2240
> ##D points(0:25, rep(0.3, 26), pch = 0:25, bg = "grey")
2241
> ## End(Not run)
77426 ripley 2242
> 
56353 ripley 2243
> ##-------- Showing all the extra & some char graphics symbols ---------
2244
> pchShow <-
2245
+   function(extras = c("*",".", "o","O","0","+","-","|","%","#"),
2246
+            cex = 3, ## good for both .Device=="postscript" and "x11"
2247
+            col = "red3", bg = "gold", coltext = "brown", cextext = 1.2,
2248
+            main = paste("plot symbols :  points (...  pch = *, cex =",
2249
+                         cex,")"))
2250
+   {
2251
+     nex <- length(extras)
2252
+     np  <- 26 + nex
2253
+     ipch <- 0:(np-1)
2254
+     k <- floor(sqrt(np))
2255
+     dd <- c(-1,1)/2
2256
+     rx <- dd + range(ix <- ipch %/% k)
2257
+     ry <- dd + range(iy <- 3 + (k-1)- ipch %% k)
2258
+     pch <- as.list(ipch) # list with integers & strings
2259
+     if(nex > 0) pch[26+ 1:nex] <- as.list(extras)
61157 ripley 2260
+     plot(rx, ry, type = "n", axes  =  FALSE, xlab = "", ylab = "", main = main)
56353 ripley 2261
+     abline(v = ix, h = iy, col = "lightgray", lty = "dotted")
2262
+     for(i in 1:np) {
2263
+       pc <- pch[[i]]
2264
+       ## 'col' symbols with a 'bg'-colored interior (where available) :
2265
+       points(ix[i], iy[i], pch = pc, col = col, bg = bg, cex = cex)
2266
+       if(cextext > 0)
2267
+           text(ix[i] - 0.3, iy[i], pc, col = coltext, cex = cextext)
2268
+     }
2269
+   }
77426 ripley 2270
> 
56353 ripley 2271
> pchShow()
2272
> pchShow(c("o","O","0"), cex = 2.5)
2273
> pchShow(NULL, cex = 4, cextext = 0, main = NULL)
77426 ripley 2274
> 
2275
> 
2276
> 
56353 ripley 2277
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2278
> cleanEx()
2279
> nameEx("polygon")
2280
> ### * polygon
77426 ripley 2281
> 
56353 ripley 2282
> flush(stderr()); flush(stdout())
77426 ripley 2283
> 
56353 ripley 2284
> ### Name: polygon
2285
> ### Title: Polygon Drawing
2286
> ### Aliases: polygon
2287
> ### Keywords: aplot
77426 ripley 2288
> 
56353 ripley 2289
> ### ** Examples
77426 ripley 2290
> 
61169 ripley 2291
> x <- c(1:9, 8:1)
2292
> y <- c(1, 2*(5:3), 2, -1, 17, 9, 8, 2:9)
2293
> op <- par(mfcol = c(3, 1))
2294
> for(xpd in c(FALSE, TRUE, NA)) {
56353 ripley 2295
+   plot(1:10, main = paste("xpd =", xpd))
61157 ripley 2296
+   box("figure", col = "pink", lwd = 3)
61169 ripley 2297
+   polygon(x, y, xpd = xpd, col = "orange", lty = 2, lwd = 2, border = "red")
56353 ripley 2298
+ }
2299
> par(op)
77426 ripley 2300
> 
56353 ripley 2301
> n <- 100
2302
> xx <- c(0:n, n:0)
61169 ripley 2303
> yy <- c(c(0, cumsum(stats::rnorm(n))), rev(c(0, cumsum(stats::rnorm(n)))))
61157 ripley 2304
> plot   (xx, yy, type = "n", xlab = "Time", ylab = "Distance")
2305
> polygon(xx, yy, col = "gray", border = "red")
56353 ripley 2306
> title("Distance Between Brownian Motions")
77426 ripley 2307
> 
56353 ripley 2308
> # Multiple polygons from NA values
2309
> # and recycling of col, border, and lty
61169 ripley 2310
> op <- par(mfrow = c(2, 1))
2311
> plot(c(1, 9), 1:2, type = "n")
56353 ripley 2312
> polygon(1:9, c(2,1,2,1,1,2,1,2,1),
61157 ripley 2313
+         col = c("red", "blue"),
2314
+         border = c("green", "yellow"),
2315
+         lwd = 3, lty = c("dashed", "solid"))
61169 ripley 2316
> plot(c(1, 9), 1:2, type = "n")
56353 ripley 2317
> polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
61157 ripley 2318
+         col = c("red", "blue"),
2319
+         border = c("green", "yellow"),
2320
+         lwd = 3, lty = c("dashed", "solid"))
56353 ripley 2321
> par(op)
77426 ripley 2322
> 
56353 ripley 2323
> # Line-shaded polygons
61169 ripley 2324
> plot(c(1, 9), 1:2, type = "n")
56353 ripley 2325
> polygon(1:9, c(2,1,2,1,NA,2,1,2,1),
61157 ripley 2326
+         density = c(10, 20), angle = c(-45, 45))
77426 ripley 2327
> 
2328
> 
2329
> 
56353 ripley 2330
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2331
> cleanEx()
2332
> nameEx("polypath")
2333
> ### * polypath
77426 ripley 2334
> 
56353 ripley 2335
> flush(stderr()); flush(stdout())
77426 ripley 2336
> 
56353 ripley 2337
> ### Name: polypath
2338
> ### Title: Path Drawing
2339
> ### Aliases: polypath
2340
> ### Keywords: aplot
77426 ripley 2341
> 
56353 ripley 2342
> ### ** Examples
77426 ripley 2343
> 
56353 ripley 2344
> plotPath <- function(x, y, col = "grey", rule = "winding") {
2345
+     plot.new()
2346
+     plot.window(range(x, na.rm = TRUE), range(y, na.rm = TRUE))
2347
+     polypath(x, y, col = col, rule = rule)
2348
+     if (!is.na(col))
2349
+         mtext(paste("Rule:", rule), side = 1, line = 0)
2350
+ }
77426 ripley 2351
> 
56353 ripley 2352
> plotRules <- function(x, y, title) {
2353
+     plotPath(x, y)
2354
+     plotPath(x, y, rule = "evenodd")
2355
+     mtext(title, side = 3, line = 0)
2356
+     plotPath(x, y, col = NA)
2357
+ }
77426 ripley 2358
> 
56353 ripley 2359
> op <- par(mfrow = c(5, 3), mar = c(2, 1, 1, 1))
77426 ripley 2360
> 
56353 ripley 2361
> plotRules(c(.1, .1, .9, .9, NA, .2, .2, .8, .8),
2362
+           c(.1, .9, .9, .1, NA, .2, .8, .8, .2),
2363
+           "Nested rectangles, both clockwise")
2364
> plotRules(c(.1, .1, .9, .9, NA, .2, .8, .8, .2),
2365
+           c(.1, .9, .9, .1, NA, .2, .2, .8, .8),
2366
+           "Nested rectangles, outer clockwise, inner anti-clockwise")
2367
> plotRules(c(.1, .1, .4, .4, NA, .6, .9, .9, .6),
2368
+           c(.1, .4, .4, .1, NA, .6, .6, .9, .9),
2369
+           "Disjoint rectangles")
2370
> plotRules(c(.1, .1, .6, .6, NA, .4, .4, .9, .9),
2371
+           c(.1, .6, .6, .1, NA, .4, .9, .9, .4),
2372
+           "Overlapping rectangles, both clockwise")
2373
> plotRules(c(.1, .1, .6, .6, NA, .4, .9, .9, .4),
2374
+           c(.1, .6, .6, .1, NA, .4, .4, .9, .9),
2375
+           "Overlapping rectangles, one clockwise, other anti-clockwise")
77426 ripley 2376
> 
56353 ripley 2377
> par(op)
77426 ripley 2378
> 
2379
> 
2380
> 
56353 ripley 2381
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2382
> cleanEx()
56927 ripley 2383
> nameEx("rasterImage")
2384
> ### * rasterImage
77426 ripley 2385
> 
56353 ripley 2386
> flush(stderr()); flush(stdout())
77426 ripley 2387
> 
56353 ripley 2388
> ### Name: rasterImage
2389
> ### Title: Draw One or More Raster Images
2390
> ### Aliases: rasterImage
2391
> ### Keywords: aplot
77426 ripley 2392
> 
56353 ripley 2393
> ### ** Examples
77426 ripley 2394
> 
56353 ripley 2395
> require(grDevices)
2396
> ## set up the plot region:
2397
> op <- par(bg = "thistle")
61157 ripley 2398
> plot(c(100, 250), c(300, 450), type = "n", xlab = "", ylab = "")
2399
> image <- as.raster(matrix(0:1, ncol = 5, nrow = 3))
56353 ripley 2400
Warning in matrix(0:1, ncol = 5, nrow = 3) :
2401
  data length [2] is not a sub-multiple or multiple of the number of rows [3]
61157 ripley 2402
> rasterImage(image, 100, 300, 150, 350, interpolate = FALSE)
56353 ripley 2403
> rasterImage(image, 100, 400, 150, 450)
2404
> rasterImage(image, 200, 300, 200 + xinch(.5), 300 + yinch(.3),
61157 ripley 2405
+             interpolate = FALSE)
2406
> rasterImage(image, 200, 400, 250, 450, angle = 15, interpolate = FALSE)
56353 ripley 2407
> par(op)
77426 ripley 2408
> 
2409
> 
2410
> 
56353 ripley 2411
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2412
> cleanEx()
2413
> nameEx("rect")
2414
> ### * rect
77426 ripley 2415
> 
56353 ripley 2416
> flush(stderr()); flush(stdout())
77426 ripley 2417
> 
56353 ripley 2418
> ### Name: rect
2419
> ### Title: Draw One or More Rectangles
2420
> ### Aliases: rect
2421
> ### Keywords: aplot
77426 ripley 2422
> 
56353 ripley 2423
> ### ** Examples
77426 ripley 2424
> 
56353 ripley 2425
> require(grDevices)
2426
> ## set up the plot region:
2427
> op <- par(bg = "thistle")
61157 ripley 2428
> plot(c(100, 250), c(300, 450), type = "n", xlab = "", ylab = "",
56353 ripley 2429
+      main = "2 x 11 rectangles; 'rect(100+i,300+i,  150+i,380+i)'")
2430
> i <- 4*(0:10)
2431
> ## draw rectangles with bottom left (100, 300)+i
2432
> ## and top right (150, 380)+i
61169 ripley 2433
> rect(100+i, 300+i, 150+i, 380+i, col = rainbow(11, start = 0.7, end = 0.1))
61157 ripley 2434
> rect(240-i, 320+i, 250-i, 410+i, col = heat.colors(11), lwd = i/5)
56353 ripley 2435
> ## Background alternating  ( transparent / "bg" ) :
2436
> j <- 10*(0:5)
2437
> rect(125+j, 360+j,   141+j, 405+j/2, col = c(NA,0),
2438
+      border = "gold", lwd = 2)
2439
> rect(125+j, 296+j/2, 141+j, 331+j/5, col = c(NA,"midnightblue"))
74366 maechler 2440
> mtext("+  2 x 6 rect(*, col = c(NA,0)) and  col = c(NA,\"m..blue\")")
77426 ripley 2441
> 
56353 ripley 2442
> ## an example showing colouring and shading
61157 ripley 2443
> plot(c(100, 200), c(300, 450), type= "n", xlab = "", ylab = "")
56353 ripley 2444
> rect(100, 300, 125, 350) # transparent
61157 ripley 2445
> rect(100, 400, 125, 450, col = "green", border = "blue") # coloured
2446
> rect(115, 375, 150, 425, col = par("bg"), border = "transparent")
2447
> rect(150, 300, 175, 350, density = 10, border = "red")
2448
> rect(150, 400, 175, 450, density = 30, col = "blue",
2449
+      angle = -30, border = "transparent")
77426 ripley 2450
> 
61157 ripley 2451
> legend(180, 450, legend = 1:4, fill = c(NA, "green", par("fg"), "blue"),
2452
+        density = c(NA, NA, 10, 30), angle = c(NA, NA, 30, -30))
77426 ripley 2453
> 
56353 ripley 2454
> par(op)
77426 ripley 2455
> 
2456
> 
2457
> 
56353 ripley 2458
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2459
> cleanEx()
2460
> nameEx("rug")
2461
> ### * rug
77426 ripley 2462
> 
56353 ripley 2463
> flush(stderr()); flush(stdout())
77426 ripley 2464
> 
56353 ripley 2465
> ### Name: rug
2466
> ### Title: Add a Rug to a Plot
2467
> ### Aliases: rug
2468
> ### Keywords: aplot
77426 ripley 2469
> 
56353 ripley 2470
> ### ** Examples
77426 ripley 2471
> 
61169 ripley 2472
> require(stats)  # both 'density' and its default method
56353 ripley 2473
> with(faithful, {
2474
+     plot(density(eruptions, bw = 0.15))
2475
+     rug(eruptions)
2476
+     rug(jitter(eruptions, amount = 0.01), side = 3, col = "light blue")
2477
+ })
77426 ripley 2478
> 
2479
> 
2480
> 
56353 ripley 2481
> cleanEx()
2482
> nameEx("screen")
2483
> ### * screen
77426 ripley 2484
> 
56353 ripley 2485
> flush(stderr()); flush(stdout())
77426 ripley 2486
> 
56353 ripley 2487
> ### Name: screen
2488
> ### Title: Creating and Controlling Multiple Screens on a Single Device
2489
> ### Aliases: screen split.screen erase.screen close.screen
2490
> ### Keywords: aplot dplot device
77426 ripley 2491
> 
56353 ripley 2492
> ### ** Examples
77426 ripley 2493
> 
56353 ripley 2494
> if (interactive()) {
2495
+ par(bg = "white")           # default is likely to be transparent
61169 ripley 2496
+ split.screen(c(2, 1))       # split display into two screens
2497
+ split.screen(c(1, 3), screen = 2) # now split the bottom half into 3
56353 ripley 2498
+ screen(1) # prepare screen 1 for output
2499
+ plot(10:1)
2500
+ screen(4) # prepare screen 4 for output
2501
+ plot(10:1)
2502
+ close.screen(all = TRUE)    # exit split-screen mode
77426 ripley 2503
+ 
61169 ripley 2504
+ split.screen(c(2, 1))       # split display into two screens
2505
+ split.screen(c(1, 2), 2)    # split bottom half in two
56353 ripley 2506
+ plot(1:10)                  # screen 3 is active, draw plot
2507
+ erase.screen()              # forgot label, erase and redraw
61157 ripley 2508
+ plot(1:10, ylab = "ylab 3")
56353 ripley 2509
+ screen(1)                   # prepare screen 1 for output
2510
+ plot(1:10)
2511
+ screen(4)                   # prepare screen 4 for output
61157 ripley 2512
+ plot(1:10, ylab = "ylab 4")
56353 ripley 2513
+ screen(1, FALSE)            # return to screen 1, but do not clear
61157 ripley 2514
+ plot(10:1, axes = FALSE, lty = 2, ylab = "")  # overlay second plot
56353 ripley 2515
+ axis(4)                     # add tic marks to right-hand axis
2516
+ title("Plot 1")
2517
+ close.screen(all = TRUE)    # exit split-screen mode
2518
+ }
77426 ripley 2519
> 
2520
> 
2521
> 
56353 ripley 2522
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2523
> cleanEx()
2524
> nameEx("segments")
2525
> ### * segments
77426 ripley 2526
> 
56353 ripley 2527
> flush(stderr()); flush(stdout())
77426 ripley 2528
> 
56353 ripley 2529
> ### Name: segments
2530
> ### Title: Add Line Segments to a Plot
2531
> ### Aliases: segments
2532
> ### Keywords: aplot
77426 ripley 2533
> 
56353 ripley 2534
> ### ** Examples
77426 ripley 2535
> 
56353 ripley 2536
> x <- stats::runif(12); y <- stats::rnorm(12)
61169 ripley 2537
> i <- order(x, y); x <- x[i]; y <- y[i]
61157 ripley 2538
> plot(x, y, main = "arrows(.) and segments(.)")
56353 ripley 2539
> ## draw arrows from point to point :
61169 ripley 2540
> s <- seq(length(x)-1)  # one shorter than data
56353 ripley 2541
> arrows(x[s], y[s], x[s+1], y[s+1], col= 1:3)
2542
> s <- s[-length(s)]
2543
> segments(x[s], y[s], x[s+2], y[s+2], col= 'pink')
77426 ripley 2544
> 
2545
> 
2546
> 
56353 ripley 2547
> cleanEx()
2548
> nameEx("smoothScatter")
2549
> ### * smoothScatter
77426 ripley 2550
> 
56353 ripley 2551
> flush(stderr()); flush(stdout())
77426 ripley 2552
> 
56353 ripley 2553
> ### Name: smoothScatter
2554
> ### Title: Scatterplots with Smoothed Densities Color Representation
2555
> ### Aliases: smoothScatter
2556
> ### Keywords: hplot
77426 ripley 2557
> 
56353 ripley 2558
> ### ** Examples
77426 ripley 2559
> 
2560
> 
56353 ripley 2561
> cleanEx()
2562
> nameEx("spineplot")
2563
> ### * spineplot
77426 ripley 2564
> 
56353 ripley 2565
> flush(stderr()); flush(stdout())
77426 ripley 2566
> 
56353 ripley 2567
> ### Name: spineplot
2568
> ### Title: Spine Plots and Spinograms
2569
> ### Aliases: spineplot spineplot.default spineplot.formula
2570
> ### Keywords: hplot
77426 ripley 2571
> 
56353 ripley 2572
> ### ** Examples
77426 ripley 2573
> 
56353 ripley 2574
> ## treatment and improvement of patients with rheumatoid arthritis
2575
> treatment <- factor(rep(c(1, 2), c(43, 41)), levels = c(1, 2),
2576
+                     labels = c("placebo", "treated"))
2577
> improved <- factor(rep(c(1, 2, 3, 1, 2, 3), c(29, 7, 7, 13, 7, 21)),
2578
+                    levels = c(1, 2, 3),
2579
+                    labels = c("none", "some", "marked"))
77426 ripley 2580
> 
56353 ripley 2581
> ## (dependence on a categorical variable)
2582
> (spineplot(improved ~ treatment))
2583
         improved
2584
treatment none some marked
2585
  placebo   29    7      7
2586
  treated   13    7     21
77426 ripley 2587
> 
56353 ripley 2588
> ## applications and admissions by department at UC Berkeley
2589
> ## (two-way tables)
2590
> (spineplot(margin.table(UCBAdmissions, c(3, 2)),
2591
+            main = "Applications at UCB"))
2592
    Gender
2593
Dept Male Female
2594
   A  825    108
2595
   B  560     25
2596
   C  325    593
2597
   D  417    375
2598
   E  191    393
2599
   F  373    341
2600
> (spineplot(margin.table(UCBAdmissions, c(3, 1)),
2601
+            main = "Admissions at UCB"))
2602
    Admit
2603
Dept Admitted Rejected
2604
   A      601      332
2605
   B      370      215
2606
   C      322      596
2607
   D      269      523
2608
   E      147      437
2609
   F       46      668
77426 ripley 2610
> 
56353 ripley 2611
> ## NASA space shuttle o-ring failures
2612
> fail <- factor(c(2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1,
2613
+                  1, 1, 1, 2, 1, 1, 1, 1, 1),
2614
+                levels = c(1, 2), labels = c("no", "yes"))
2615
> temperature <- c(53, 57, 58, 63, 66, 67, 67, 67, 68, 69, 70, 70,
2616
+                  70, 70, 72, 73, 75, 75, 76, 76, 78, 79, 81)
77426 ripley 2617
> 
56353 ripley 2618
> ## (dependence on a numerical variable)
2619
> (spineplot(fail ~ temperature))
2620
           fail
2621
temperature no yes
2622
    [50,55]  0   1
2623
    (55,60]  0   2
2624
    (60,65]  0   1
2625
    (65,70]  8   2
2626
    (70,75]  3   1
2627
    (75,80]  4   0
2628
    (80,85]  1   0
2629
> (spineplot(fail ~ temperature, breaks = 3))
2630
           fail
2631
temperature no yes
2632
    [50,60]  0   3
2633
    (60,70]  8   3
2634
    (70,80]  7   1
2635
    (80,90]  1   0
2636
> (spineplot(fail ~ temperature, breaks = quantile(temperature)))
2637
           fail
2638
temperature no yes
2639
    [53,67]  4   4
2640
    (67,70]  4   2
2641
    (70,75]  3   1
2642
    (75,81]  5   0
77426 ripley 2643
> 
56353 ripley 2644
> ## highlighting for failures
2645
> spineplot(fail ~ temperature, ylevels = 2:1)
77426 ripley 2646
> 
2647
> 
2648
> 
56353 ripley 2649
> cleanEx()
2650
> nameEx("stars")
2651
> ### * stars
77426 ripley 2652
> 
56353 ripley 2653
> flush(stderr()); flush(stdout())
77426 ripley 2654
> 
56353 ripley 2655
> ### Name: stars
2656
> ### Title: Star (Spider/Radar) Plots and Segment Diagrams
2657
> ### Aliases: stars
2658
> ### Keywords: hplot multivariate
77426 ripley 2659
> 
56353 ripley 2660
> ### ** Examples
77426 ripley 2661
> 
56353 ripley 2662
> require(grDevices)
2663
> stars(mtcars[, 1:7], key.loc = c(14, 2),
2664
+       main = "Motor Trend Cars : stars(*, full = F)", full = FALSE)
2665
> stars(mtcars[, 1:7], key.loc = c(14, 1.5),
61157 ripley 2666
+       main = "Motor Trend Cars : full stars()", flip.labels = FALSE)
77426 ripley 2667
> 
56353 ripley 2668
> ## 'Spider' or 'Radar' plot:
61169 ripley 2669
> stars(mtcars[, 1:7], locations = c(0, 0), radius = FALSE,
2670
+       key.loc = c(0, 0), main = "Motor Trend Cars", lty = 2)
77426 ripley 2671
> 
56353 ripley 2672
> ## Segment Diagrams:
2673
> palette(rainbow(12, s = 0.6, v = 0.75))
2674
> stars(mtcars[, 1:7], len = 0.8, key.loc = c(12, 1.5),
2675
+       main = "Motor Trend Cars", draw.segments = TRUE)
2676
> stars(mtcars[, 1:7], len = 0.6, key.loc = c(1.5, 0),
2677
+       main = "Motor Trend Cars", draw.segments = TRUE,
61157 ripley 2678
+       frame.plot = TRUE, nrow = 4, cex = .7)
77426 ripley 2679
> 
56353 ripley 2680
> ## scale linearly (not affinely) to [0, 1]
2681
> USJudge <- apply(USJudgeRatings, 2, function(x) x/max(x))
2682
> Jnam <- row.names(USJudgeRatings)
61169 ripley 2683
> Snam <- abbreviate(substring(Jnam, 1, regexpr("[,.]",Jnam) - 1), 7)
56353 ripley 2684
> stars(USJudge, labels = Jnam, scale = FALSE,
2685
+       key.loc = c(13, 1.5), main = "Judge not ...", len = 0.8)
2686
> stars(USJudge, labels = Snam, scale = FALSE,
2687
+       key.loc = c(13, 1.5), radius = FALSE)
77426 ripley 2688
> 
56353 ripley 2689
> loc <- stars(USJudge, labels = NULL, scale = FALSE,
2690
+              radius = FALSE, frame.plot = TRUE,
2691
+              key.loc = c(13, 1.5), main = "Judge not ...", len = 1.2)
2692
> text(loc, Snam, col = "blue", cex = 0.8, xpd = TRUE)
77426 ripley 2693
> 
56353 ripley 2694
> ## 'Segments':
2695
> stars(USJudge, draw.segments = TRUE, scale = FALSE, key.loc = c(13,1.5))
77426 ripley 2696
> 
56353 ripley 2697
> ## 'Spider':
61169 ripley 2698
> stars(USJudgeRatings, locations = c(0, 0), scale = FALSE, radius  =  FALSE,
2699
+       col.stars = 1:10, key.loc = c(0, 0), main = "US Judges rated")
56747 maechler 2700
> ## Same as above, but with colored lines instead of filled polygons.
61169 ripley 2701
> stars(USJudgeRatings, locations = c(0, 0), scale = FALSE, radius  =  FALSE,
2702
+       col.lines = 1:10, key.loc = c(0, 0), main = "US Judges rated")
56353 ripley 2703
> ## 'Radar-Segments'
61157 ripley 2704
> stars(USJudgeRatings[1:10,], locations = 0:1, scale = FALSE,
2705
+       draw.segments = TRUE, col.segments = 0, col.stars = 1:10, key.loc =  0:1,
2706
+       main = "US Judges 1-10 ")
56353 ripley 2707
> palette("default")
61169 ripley 2708
> stars(cbind(1:16, 10*(16:1)), draw.segments = TRUE,
56353 ripley 2709
+       main = "A Joke -- do *not* use symbols on 2D data!")
77426 ripley 2710
> 
2711
> 
2712
> 
56353 ripley 2713
> cleanEx()
2714
> nameEx("stem")
2715
> ### * stem
77426 ripley 2716
> 
56353 ripley 2717
> flush(stderr()); flush(stdout())
77426 ripley 2718
> 
56353 ripley 2719
> ### Name: stem
2720
> ### Title: Stem-and-Leaf Plots
2721
> ### Aliases: stem
2722
> ### Keywords: univar distribution
77426 ripley 2723
> 
56353 ripley 2724
> ### ** Examples
77426 ripley 2725
> 
56353 ripley 2726
> stem(islands)
2727
 
2728
  The decimal point is 3 digit(s) to the right of the |
2729
 
2730
 
2731
   2 | 07
2732
   4 | 5
2733
   6 | 8
2734
   8 | 4
2735
  10 | 5
77426 ripley 2736
  12 | 
2737
  14 | 
56353 ripley 2738
  16 | 0
2739
 
2740
> stem(log10(islands))
2741
 
2742
  The decimal point is at the |
2743
 
2744
  1 | 1111112222233444
2745
  1 | 5555556666667899999
2746
  2 | 3344
2747
  2 | 59
77426 ripley 2748
  3 | 
56353 ripley 2749
  3 | 5678
2750
  4 | 012
2751
 
77426 ripley 2752
> 
2753
> 
2754
> 
56353 ripley 2755
> cleanEx()
2756
> nameEx("stripchart")
2757
> ### * stripchart
77426 ripley 2758
> 
56353 ripley 2759
> flush(stderr()); flush(stdout())
77426 ripley 2760
> 
56353 ripley 2761
> ### Name: stripchart
2762
> ### Title: 1-D Scatter Plots
2763
> ### Aliases: stripchart stripchart.default stripchart.formula
2764
> ### Keywords: hplot
77426 ripley 2765
> 
56353 ripley 2766
> ### ** Examples
77426 ripley 2767
> 
56353 ripley 2768
> x <- stats::rnorm(50)
2769
> xr <- round(x, 1)
2770
> stripchart(x) ; m <- mean(par("usr")[1:2])
2771
> text(m, 1.04, "stripchart(x, \"overplot\")")
2772
> stripchart(xr, method = "stack", add = TRUE, at = 1.2)
2773
> text(m, 1.35, "stripchart(round(x,1), \"stack\")")
2774
> stripchart(xr, method = "jitter", add = TRUE, at = 0.7)
2775
> text(m, 0.85, "stripchart(round(x,1), \"jitter\")")
77426 ripley 2776
> 
56353 ripley 2777
> stripchart(decrease ~ treatment,
61431 ripley 2778
+     main = "stripchart(OrchardSprays)",
56353 ripley 2779
+     vertical = TRUE, log = "y", data = OrchardSprays)
77426 ripley 2780
> 
56353 ripley 2781
> stripchart(decrease ~ treatment, at = c(1:8)^2,
61431 ripley 2782
+     main = "stripchart(OrchardSprays)",
56353 ripley 2783
+     vertical = TRUE, log = "y", data = OrchardSprays)
77426 ripley 2784
> 
2785
> 
2786
> 
56353 ripley 2787
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2788
> cleanEx()
2789
> nameEx("strwidth")
2790
> ### * strwidth
77426 ripley 2791
> 
56353 ripley 2792
> flush(stderr()); flush(stdout())
77426 ripley 2793
> 
56353 ripley 2794
> ### Name: strwidth
2795
> ### Title: Plotting Dimensions of Character Strings and Math Expressions
2796
> ### Aliases: strwidth strheight
2797
> ### Keywords: dplot character
77426 ripley 2798
> 
56353 ripley 2799
> ### ** Examples
77426 ripley 2800
> 
56353 ripley 2801
> str.ex <- c("W","w","I",".","WwI.")
61169 ripley 2802
> op <- par(pty = "s"); plot(1:100, 1:100, type = "n")
56353 ripley 2803
> sw <- strwidth(str.ex); sw
2804
[1] 3.2600930 2.4934186 0.9600698 0.9600698 7.6736512
2805
> all.equal(sum(sw[1:4]), sw[5])
2806
[1] TRUE
2807
> #- since the last string contains the others
77426 ripley 2808
> 
61169 ripley 2809
> sw.i <- strwidth(str.ex, "inches"); 25.4 * sw.i  # width in [mm]
56353 ripley 2810
[1] 3.996267 3.056467 1.176867 1.176867 9.406467
2811
> unique(sw / sw.i)
2812
[1] 20.72093 20.72093
2813
> # constant factor: 1 value
61169 ripley 2814
> mean(sw.i / strwidth(str.ex, "fig")) / par('fin')[1]  # = 1: are the same
56353 ripley 2815
[1] 1
77426 ripley 2816
> 
56353 ripley 2817
> ## See how letters fall in classes
2818
> ##  -- depending on graphics device and font!
2819
> all.lett <- c(letters, LETTERS)
61169 ripley 2820
> shL <- strheight(all.lett, units = "inches") * 72  # 'big points'
2821
> table(shL)  # all have same heights ...
56353 ripley 2822
shL
77426 ripley 2823
8.616 
2824
   52 
56353 ripley 2825
> mean(shL)/par("cin")[2] # around 0.6
2826
[1] 43.08
77426 ripley 2827
> 
61169 ripley 2828
> (swL <- strwidth(all.lett, units = "inches") * 72)  # 'big points'
56353 ripley 2829
 [1]  6.672  6.672  6.000  6.672  6.672  3.336  6.672  6.672  2.664  2.664
2830
[11]  6.000  2.664  9.996  6.672  6.672  6.672  6.672  3.996  6.000  3.336
2831
[21]  6.672  6.000  8.664  6.000  6.000  6.000  8.004  8.004  8.664  8.664
2832
[31]  8.004  7.332  9.336  8.664  3.336  6.000  8.004  6.672  9.996  8.664
2833
[41]  9.336  8.004  9.336  8.664  8.004  7.332  8.664  8.004 11.328  8.004
2834
[51]  8.004  7.332
2835
> split(all.lett, factor(round(swL, 2)))
2836
$`2.66`
2837
[1] "i" "j" "l"
2838
 
2839
$`3.34`
2840
[1] "f" "t" "I"
2841
 
2842
$`4`
2843
[1] "r"
2844
 
2845
$`6`
2846
[1] "c" "k" "s" "v" "x" "y" "z" "J"
2847
 
2848
$`6.67`
2849
 [1] "a" "b" "d" "e" "g" "h" "n" "o" "p" "q" "u" "L"
2850
 
2851
$`7.33`
2852
[1] "F" "T" "Z"
2853
 
2854
$`8`
2855
[1] "A" "B" "E" "K" "P" "S" "V" "X" "Y"
2856
 
2857
$`8.66`
2858
[1] "w" "C" "D" "H" "N" "R" "U"
2859
 
2860
$`9.34`
2861
[1] "G" "O" "Q"
2862
 
2863
$`10`
2864
[1] "m" "M"
2865
 
2866
$`11.33`
2867
[1] "W"
2868
 
77426 ripley 2869
> 
56353 ripley 2870
> sumex <- expression(sum(x[i], i=1,n), e^{i * pi} == -1)
2871
> strwidth(sumex)
2872
[1]  5.795241 11.484959
2873
> strheight(sumex)
2874
[1] 8.057449 3.420738
77426 ripley 2875
> 
61169 ripley 2876
> par(op)  #- reset to previous setting
77426 ripley 2877
> 
2878
> 
2879
> 
56353 ripley 2880
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
2881
> cleanEx()
2882
> nameEx("sunflowerplot")
2883
> ### * sunflowerplot
77426 ripley 2884
> 
56353 ripley 2885
> flush(stderr()); flush(stdout())
77426 ripley 2886
> 
56353 ripley 2887
> ### Name: sunflowerplot
2888
> ### Title: Produce a Sunflower Scatter Plot
2889
> ### Aliases: sunflowerplot sunflowerplot.default sunflowerplot.formula
2890
> ### Keywords: hplot smooth nonparametric
77426 ripley 2891
> 
56353 ripley 2892
> ### ** Examples
77426 ripley 2893
> 
67689 ripley 2894
> require(stats) # for rnorm
56353 ripley 2895
> require(grDevices)
77426 ripley 2896
> 
56353 ripley 2897
> ## 'number' is computed automatically:
2898
> sunflowerplot(iris[, 3:4])
65261 ripley 2899
> ## Imitating Chambers et al, p.109, closely:
61157 ripley 2900
> sunflowerplot(iris[, 3:4], cex = .2, cex.fact = 1, size = .035, seg.lwd = .8)
56353 ripley 2901
> ## or
61157 ripley 2902
> sunflowerplot(Petal.Width ~ Petal.Length, data = iris,
2903
+               cex = .2, cex.fact = 1, size = .035, seg.lwd = .8)
77426 ripley 2904
> 
2905
> 
61169 ripley 2906
> sunflowerplot(x = sort(2*round(rnorm(100))), y = round(rnorm(100), 0),
56353 ripley 2907
+               main = "Sunflower Plot of Rounded N(0,1)")
2908
> ## Similarly using a "xyTable" argument:
61169 ripley 2909
> xyT <- xyTable(x = sort(2*round(rnorm(100))), y = round(rnorm(100), 0),
61157 ripley 2910
+                digits = 3)
2911
> utils::str(xyT, vec.len = 20)
56353 ripley 2912
List of 3
2913
 $ x     : num [1:25] -6 -4 -4 -4 -4 -2 -2 -2 -2 -2 0 0 0 0 0 0 2 2 2 2 2 4 4 4 4
2914
 $ y     : num [1:25] 0 -2 0 1 2 -2 -1 0 1 2 -3 -2 -1 0 1 2 -2 -1 0 1 3 -1 0 1 2
2915
 $ number: int [1:25] 1 1 1 1 3 1 4 8 4 1 1 3 5 17 12 2 1 8 12 6 1 1 3 2 1
2916
> sunflowerplot(xyT, main = "2nd Sunflower Plot of Rounded N(0,1)")
77426 ripley 2917
> 
56353 ripley 2918
> ## A 'marked point process' {explicit 'number' argument}:
61169 ripley 2919
> sunflowerplot(rnorm(100), rnorm(100), number = rpois(n = 100, lambda = 2),
61157 ripley 2920
+               main = "Sunflower plot (marked point process)",
2921
+               rotate = TRUE, col = "blue4")
77426 ripley 2922
> 
2923
> 
2924
> 
56353 ripley 2925
> cleanEx()
2926
> nameEx("symbols")
2927
> ### * symbols
77426 ripley 2928
> 
56353 ripley 2929
> flush(stderr()); flush(stdout())
77426 ripley 2930
> 
56353 ripley 2931
> ### Name: symbols
2932
> ### Title: Draw Symbols (Circles, Squares, Stars, Thermometers, Boxplots)
2933
> ### Aliases: symbols
2934
> ### Keywords: aplot hplot multivariate
77426 ripley 2935
> 
56353 ripley 2936
> ### ** Examples
77426 ripley 2937
> 
56353 ripley 2938
> require(stats); require(grDevices)
2939
> x <- 1:10
2940
> y <- sort(10*runif(10))
2941
> z <- runif(10)
2942
> z3 <- cbind(z, 2*runif(10), runif(10))
2943
> symbols(x, y, thermometers = cbind(.5, 1, z), inches = .5, fg = 1:10)
2944
> symbols(x, y, thermometers = z3, inches = FALSE)
61169 ripley 2945
> text(x, y, apply(format(round(z3, digits = 2)), 1, paste, collapse = ","),
56353 ripley 2946
+      adj = c(-.2,0), cex = .75, col = "purple", xpd = NA)
77426 ripley 2947
> 
56353 ripley 2948
> ## Note that  example(trees)  shows more sensible plots!
2949
> N <- nrow(trees)
2950
> with(trees, {
2951
+ ## Girth is diameter in inches
2952
+ symbols(Height, Volume, circles = Girth/24, inches = FALSE,
2953
+         main = "Trees' Girth") # xlab and ylab automatically
2954
+ ## Colours too:
2955
+ op <- palette(rainbow(N, end = 0.9))
2956
+ symbols(Height, Volume, circles = Girth/16, inches = FALSE, bg = 1:N,
2957
+         fg = "gray30", main = "symbols(*, circles = Girth/16, bg = 1:N)")
2958
+ palette(op)
2959
+ })
77426 ripley 2960
> 
2961
> 
2962
> 
56353 ripley 2963
> cleanEx()
2964
> nameEx("title")
2965
> ### * title
77426 ripley 2966
> 
56353 ripley 2967
> flush(stderr()); flush(stdout())
77426 ripley 2968
> 
56353 ripley 2969
> ### Name: title
2970
> ### Title: Plot Annotation
2971
> ### Aliases: title
2972
> ### Keywords: aplot
77426 ripley 2973
> 
56353 ripley 2974
> ### ** Examples
77426 ripley 2975
> 
56353 ripley 2976
> plot(cars, main = "") # here, could use main directly
2977
> title(main = "Stopping Distance versus Speed")
77426 ripley 2978
> 
56353 ripley 2979
> plot(cars, main = "")
61157 ripley 2980
> title(main = list("Stopping Distance versus Speed", cex = 1.5,
2981
+                   col = "red", font = 3))
77426 ripley 2982
> 
56353 ripley 2983
> ## Specifying "..." :
2984
> plot(1, col.axis = "sky blue", col.lab = "thistle")
2985
> title("Main Title", sub = "sub title",
2986
+       cex.main = 2,   font.main= 4, col.main= "blue",
2987
+       cex.sub = 0.75, font.sub = 3, col.sub = "red")
77426 ripley 2988
> 
2989
> 
56353 ripley 2990
> x <- seq(-4, 4, len = 101)
2991
> y <- cbind(sin(x), cos(x))
2992
> matplot(x, y, type = "l", xaxt = "n",
2993
+         main = expression(paste(plain(sin) * phi, "  and  ",
2994
+                                 plain(cos) * phi)),
2995
+         ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
2996
+         xlab = expression(paste("Phase Angle ", phi)),
2997
+         col.main = "blue")
2998
> axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
2999
+      labels = expression(-pi, -pi/2, 0, pi/2, pi))
3000
> abline(h = 0, v = pi/2 * c(-1,1), lty = 2, lwd = .1, col = "gray70")
77426 ripley 3001
> 
3002
> 
3003
> 
56353 ripley 3004
> cleanEx()
3005
> nameEx("units")
3006
> ### * units
77426 ripley 3007
> 
56353 ripley 3008
> flush(stderr()); flush(stdout())
77426 ripley 3009
> 
56353 ripley 3010
> ### Name: units
3011
> ### Title: Graphical Units
3012
> ### Aliases: xinch yinch xyinch
3013
> ### Keywords: dplot
77426 ripley 3014
> 
56353 ripley 3015
> ### ** Examples
77426 ripley 3016
> 
61169 ripley 3017
> all(c(xinch(), yinch()) == xyinch()) # TRUE
56353 ripley 3018
[1] TRUE
3019
> xyinch()
3020
[1] 1.5000000 0.4185559
3021
> xyinch #- to see that is really   delta{"usr"} / "pin"
77426 ripley 3022
function (xy = 1, warn.log = TRUE) 
56353 ripley 3023
{
77426 ripley 3024
    if (warn.log && (par("xlog") || par("ylog"))) 
60841 ripley 3025
        warning("log scale:  xyinch() is nonsense")
56353 ripley 3026
    u <- par("usr")
3027
    xy * c(u[2L] - u[1L], u[4L] - u[3L])/par("pin")
3028
}
77431 ripley 3029
<bytecode: 0x410e170>
56353 ripley 3030
<environment: namespace:graphics>
77426 ripley 3031
> 
56353 ripley 3032
> ## plot labels offset 0.12 inches to the right
3033
> ## of plotted symbols in a plot
3034
> with(mtcars, {
61157 ripley 3035
+     plot(mpg, disp, pch = 19, main = "Motor Trend Cars")
56353 ripley 3036
+     text(mpg + xinch(0.12), disp, row.names(mtcars),
61169 ripley 3037
+          adj = 0, cex = .7, col = "blue")
56353 ripley 3038
+     })
77426 ripley 3039
> 
3040
> 
3041
> 
56353 ripley 3042
> cleanEx()
3043
> nameEx("xspline")
3044
> ### * xspline
77426 ripley 3045
> 
56353 ripley 3046
> flush(stderr()); flush(stdout())
77426 ripley 3047
> 
56353 ripley 3048
> ### Name: xspline
3049
> ### Title: Draw an X-spline
3050
> ### Aliases: xspline
3051
> ### Keywords: aplot
77426 ripley 3052
> 
56353 ripley 3053
> ### ** Examples
77426 ripley 3054
> 
56353 ripley 3055
> ## based on examples in ?grid.xspline
77426 ripley 3056
> 
56353 ripley 3057
> xsplineTest <- function(s, open = TRUE,
3058
+                         x = c(1,1,3,3)/4,
3059
+                         y = c(1,3,3,1)/4, ...) {
61157 ripley 3060
+     plot(c(0,1), c(0,1), type = "n", axes = FALSE, xlab = "", ylab = "")
3061
+     points(x, y, pch = 19)
56353 ripley 3062
+     xspline(x, y, s, open, ...)
3063
+     text(x+0.05*c(-1,-1,1,1), y+0.05*c(-1,1,1,-1), s)
3064
+ }
61157 ripley 3065
> op <- par(mfrow = c(3,3), mar = rep(0,4), oma = c(0,0,2,0))
56353 ripley 3066
> xsplineTest(c(0, -1, -1, 0))
3067
> xsplineTest(c(0, -1,  0, 0))
3068
> xsplineTest(c(0, -1,  1, 0))
3069
> xsplineTest(c(0,  0, -1, 0))
3070
> xsplineTest(c(0,  0,  0, 0))
3071
> xsplineTest(c(0,  0,  1, 0))
3072
> xsplineTest(c(0,  1, -1, 0))
3073
> xsplineTest(c(0,  1,  0, 0))
3074
> xsplineTest(c(0,  1,  1, 0))
61157 ripley 3075
> title("Open X-splines", outer = TRUE)
77426 ripley 3076
> 
61157 ripley 3077
> par(mfrow = c(3,3), mar = rep(0,4), oma = c(0,0,2,0))
3078
> xsplineTest(c(0, -1, -1, 0), FALSE, col = "grey80")
3079
> xsplineTest(c(0, -1,  0, 0), FALSE, col = "grey80")
3080
> xsplineTest(c(0, -1,  1, 0), FALSE, col = "grey80")
3081
> xsplineTest(c(0,  0, -1, 0), FALSE, col = "grey80")
3082
> xsplineTest(c(0,  0,  0, 0), FALSE, col = "grey80")
3083
> xsplineTest(c(0,  0,  1, 0), FALSE, col = "grey80")
3084
> xsplineTest(c(0,  1, -1, 0), FALSE, col = "grey80")
3085
> xsplineTest(c(0,  1,  0, 0), FALSE, col = "grey80")
3086
> xsplineTest(c(0,  1,  1, 0), FALSE, col = "grey80")
3087
> title("Closed X-splines", outer = TRUE)
77426 ripley 3088
> 
56353 ripley 3089
> par(op)
77426 ripley 3090
> 
56353 ripley 3091
> x <- sort(stats::rnorm(5))
3092
> y <- sort(stats::rnorm(5))
61157 ripley 3093
> plot(x, y, pch = 19)
3094
> res <- xspline(x, y, 1, draw = FALSE)
56353 ripley 3095
> lines(res)
3096
> ## the end points may be very close together,
3097
> ## so use last few for direction
3098
> nr <- length(res$x)
61157 ripley 3099
> arrows(res$x[1], res$y[1], res$x[4], res$y[4], code = 1, length = 0.1)
3100
> arrows(res$x[nr-3], res$y[nr-3], res$x[nr], res$y[nr], code = 2, length = 0.1)
77426 ripley 3101
> 
3102
> 
3103
> 
56353 ripley 3104
> graphics::par(get("par.postscript", pos = 'CheckExEnv'))
3105
> ### * <FOOTER>
3106
> ###
73819 hornik 3107
> cleanEx()
62439 ripley 3108
> options(digits = 7L)
61787 ripley 3109
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
77431 ripley 3110
Time elapsed:  2.867 0.049 2.931 0 0 
56353 ripley 3111
> grDevices::dev.off()
77426 ripley 3112
null device 
3113
          1 
56353 ripley 3114
> ###
3115
> ### Local variables: ***
3116
> ### mode: outline-minor ***
3117
> ### outline-regexp: "\\(> \\)?### [*]+" ***
3118
> ### End: ***
3119
> quit('no')