-
Notifications
You must be signed in to change notification settings - Fork 2
/
15-R-intro.Rmd
555 lines (554 loc) · 51.9 KB
/
15-R-intro.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# Graphical procedures
<p>Graphical facilities are an important and extremely versatile component of the R environment. It is possible to use the facilities to display a wide variety of statistical graphs and also to build entirely new types of graph.</p>
<p>The graphics facilities can be used in both interactive and batch modes, but in most cases, interactive use is more productive. Interactive use is also easy because at startup time R initiates a graphics <em>device driver</em> which opens a special <em>graphics window</em> for the display of interactive graphics. Although this is done automatically, it may useful to know that the command used is <code class="calibre2">X11()</code> under UNIX, <code class="calibre2">windows()</code> under Windows and <code class="calibre2">quartz()</code> under macOS. A new device can always be opened by <code class="calibre2">dev.new()</code>.</p>
<p>Once the device driver is running, R plotting commands can be used to produce a variety of graphical displays and to create entirely new kinds of display.</p>
<p>Plotting commands are divided into three basic groups:</p>
<ul>
<li><strong>High-level</strong> plotting functions create a new plot on the graphics device, possibly with axes, labels, titles and so on.</li>
<li><strong>Low-level</strong> plotting functions add more information to an existing plot, such as extra points, lines and labels.</li>
<li><strong>Interactive</strong> graphics functions allow you interactively add information to, or extract information from, an existing plot, using a pointing device such as a mouse.</li>
</ul>
<p>In addition, R maintains a list of <em>graphical parameters</em> which can be manipulated to customize your plots.</p>
<p>This manual only describes what are known as ‘base’ graphics. A separate graphics sub-system in package <strong>grid</strong> coexists with base – it is more powerful but harder to use. There is a recommended package <a href="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a> which builds on <strong>grid</strong> and provides ways to produce multi-panel plots akin to those in the <em>Trellis</em> system in S.</p>
<hr />
<p><a href="" id="High_002dlevel-plotting-commands"></a> <a href="" id="High_002dlevel-plotting-commands-1"></a></p>
<h3 id="high-level-plotting-commands" class="section">12.1 High-level plotting commands</h3>
<p>High-level plotting functions are designed to generate a complete plot of the data passed as arguments to the function. Where appropriate, axes, labels and titles are automatically generated (unless you request otherwise.) High-level plotting commands always start a new plot, erasing the current plot if necessary.</p>
<hr />
<p><a href="" id="The-plot_0028_0029-function"></a> <a href="" id="The-plot_0028_0029-function-1"></a></p>
<h4 id="the-plot-function" class="subheading">12.1.1 The <code class="calibre2">plot()</code> function</h4>
<p><a href="" id="index-plot-1"></a></p>
<p>One of the most frequently used plotting functions in R is the <code class="calibre2">plot()</code> function. This is a <em>generic</em> function: the type of plot produced is dependent on the type or <em>class</em> of the first argument.</p>
<dl>
<dt><code class="calibre2">plot(x, y)</code><br />
<code class="calibre2">plot(xy)</code></dt>
<dd><p>If x and y are vectors, <code class="calibre2">plot(x, y)</code> produces a scatterplot of y against x. The same effect can be produced by supplying one argument (second form) as either a list containing two elements x and y or a two-column matrix.</p>
</dd>
<dt><code class="calibre2">plot(x)</code></dt>
<dd><p>If x is a time series, this produces a time-series plot. If x is a numeric vector, it produces a plot of the values in the vector against their index in the vector. If x is a complex vector, it produces a plot of imaginary versus real parts of the vector elements.</p>
</dd>
<dt><code class="calibre2">plot(f)</code><br />
<code class="calibre2">plot(f, y)</code></dt>
<dd><p>f is a factor object, y is a numeric vector. The first form generates a bar plot of f; the second form produces boxplots of y for each level of f.</p>
</dd>
<dt><code class="calibre2">plot(df)</code><br />
<code class="calibre2">plot(~ expr)</code><br />
<code class="calibre2">plot(y ~ expr)</code></dt>
<dd><p>df is a data frame, y is any object, expr is a list of object names separated by ‘<code class="calibre2">+</code>’ (e.g., <code class="calibre2">a + b + c</code>). The first two forms produce distributional plots of the variables in a data frame (first form) or of a number of named objects (second form). The third form plots y against every object named in expr.</p>
</dd>
</dl>
<hr />
<p><a href="" id="Displaying-multivariate-data"></a> <a href="" id="Displaying-multivariate-data-1"></a></p>
<h4 id="displaying-multivariate-data" class="subheading">12.1.2 Displaying multivariate data</h4>
<p>R provides two very useful functions for representing multivariate data. If <code class="calibre2">X</code> is a numeric matrix or data frame, the command</p>
<div class="example">
<pre class="example1"><code>> pairs(X)</code></pre>
</div>
<p><a href="" id="index-pairs"></a></p>
<p>produces a pairwise scatterplot matrix of the variables defined by the columns of <code class="calibre2">X</code>, that is, every column of <code class="calibre2">X</code> is plotted against every other column of <code class="calibre2">X</code> and the resulting <em>n(n-1)</em> plots are arranged in a matrix with plot scales constant over the rows and columns of the matrix.</p>
<p>When three or four variables are involved a <em>coplot</em> may be more enlightening. If <code class="calibre2">a</code> and <code class="calibre2">b</code> are numeric vectors and <code class="calibre2">c</code> is a numeric vector or factor object (all of the same length), then the command</p>
<div class="example">
<pre class="example1"><code>> coplot(a ~ b | c)</code></pre>
</div>
<p><a href="" id="index-coplot"></a></p>
<p>produces a number of scatterplots of <code class="calibre2">a</code> against <code class="calibre2">b</code> for given values of <code class="calibre2">c</code>. If <code class="calibre2">c</code> is a factor, this simply means that <code class="calibre2">a</code> is plotted against <code class="calibre2">b</code> for every level of <code class="calibre2">c</code>. When <code class="calibre2">c</code> is numeric, it is divided into a number of <em>conditioning intervals</em> and for each interval <code class="calibre2">a</code> is plotted against <code class="calibre2">b</code> for values of <code class="calibre2">c</code> within the interval. The number and position of intervals can be controlled with <code class="calibre2">given.values=</code> argument to <code class="calibre2">coplot()</code>—the function <code class="calibre2">co.intervals()</code> is useful for selecting intervals. You can also use two <em>given</em> variables with a command like</p>
<div class="example">
<pre class="example1"><code>> coplot(a ~ b | c + d)</code></pre>
</div>
<p>which produces scatterplots of <code class="calibre2">a</code> against <code class="calibre2">b</code> for every joint conditioning interval of <code class="calibre2">c</code> and <code class="calibre2">d</code>.</p>
<p>The <code class="calibre2">coplot()</code> and <code class="calibre2">pairs()</code> function both take an argument <code class="calibre2">panel=</code> which can be used to customize the type of plot which appears in each panel. The default is <code class="calibre2">points()</code> to produce a scatterplot but by supplying some other low-level graphics function of two vectors <code class="calibre2">x</code> and <code class="calibre2">y</code> as the value of <code class="calibre2">panel=</code> you can produce any type of plot you wish. An example panel function useful for coplots is <code class="calibre2">panel.smooth()</code>.</p>
<hr />
<p><a href="" id="Display-graphics"></a> <a href="" id="Display-graphics-1"></a></p>
<h4 id="display-graphics" class="subheading">12.1.3 Display graphics</h4>
<p>Other high-level graphics functions produce different types of plots. Some examples are:</p>
<dl>
<dt><code class="calibre2">qqnorm(x)</code><br />
<code class="calibre2">qqline(x)</code><br />
<code class="calibre2">qqplot(x, y)</code></dt>
<dd><p><a href="" id="index-qqnorm-1"></a> <a href="" id="index-qqline-1"></a> <a href="" id="index-qqplot"></a></p>
<p>Distribution-comparison plots. The first form plots the numeric vector <code class="calibre2">x</code> against the expected Normal order scores (a normal scores plot) and the second adds a straight line to such a plot by drawing a line through the distribution and data quartiles. The third form plots the quantiles of <code class="calibre2">x</code> against those of <code class="calibre2">y</code> to compare their respective distributions.</p>
</dd>
<dt><code class="calibre2">hist(x)</code><br />
<code class="calibre2">hist(x, nclass=n)</code><br />
<code class="calibre2">hist(x, breaks=b, …)</code></dt>
<dd><p><a href="" id="index-hist-1"></a></p>
<p>Produces a histogram of the numeric vector <code class="calibre2">x</code>. A sensible number of classes is usually chosen, but a recommendation can be given with the <code class="calibre2">nclass=</code> argument. Alternatively, the breakpoints can be specified exactly with the <code class="calibre2">breaks=</code> argument. If the <code class="calibre2">probability=TRUE</code> argument is given, the bars represent relative frequencies divided by bin width instead of counts.</p>
</dd>
<dt><code class="calibre2">dotchart(x, …)</code></dt>
<dd><p><a href="" id="index-dotchart"></a></p>
<p>Constructs a dotchart of the data in <code class="calibre2">x</code>. In a dotchart the <em>y</em>-axis gives a labelling of the data in <code class="calibre2">x</code> and the <em>x</em>-axis gives its value. For example it allows easy visual selection of all data entries with values lying in specified ranges.</p>
</dd>
<dt><code class="calibre2">image(x, y, z, …)</code><br />
<code class="calibre2">contour(x, y, z, …)</code><br />
<code class="calibre2">persp(x, y, z, …)</code></dt>
<dd><p><a href="" id="index-image"></a> <a href="" id="index-contour"></a> <a href="" id="index-persp"></a></p>
<p>Plots of three variables. The <code class="calibre2">image</code> plot draws a grid of rectangles using different colours to represent the value of <code class="calibre2">z</code>, the <code class="calibre2">contour</code> plot draws contour lines to represent the value of <code class="calibre2">z</code>, and the <code class="calibre2">persp</code> plot draws a 3D surface.</p>
</dd>
</dl>
<hr />
<p><a href="" id="Arguments-to-high_002dlevel-plotting-functions"></a> <a href="" id="Arguments-to-high_002dlevel-plotting-functions-1"></a></p>
<h4 id="arguments-to-high-level-plotting-functions" class="subheading">12.1.4 Arguments to high-level plotting functions</h4>
<p>There are a number of arguments which may be passed to high-level graphics functions, as follows:</p>
<dl>
<dt><code class="calibre2">add=TRUE</code></dt>
<dd><p>Forces the function to act as a low-level graphics function, superimposing the plot on the current plot (some functions only).</p>
</dd>
<dt><code class="calibre2">axes=FALSE</code></dt>
<dd><p>Suppresses generation of axes—useful for adding your own custom axes with the <code class="calibre2">axis()</code> function. The default, <code class="calibre2">axes=TRUE</code>, means include axes.</p>
</dd>
<dt><code class="calibre2">log="x"</code><br />
<code class="calibre2">log="y"</code><br />
<code class="calibre2">log="xy"</code></dt>
<dd><p>Causes the <em>x</em>, <em>y</em> or both axes to be logarithmic. This will work for many, but not all, types of plot.</p>
</dd>
<dt><code class="calibre2">type=</code></dt>
<dd><p>The <code class="calibre2">type=</code> argument controls the type of plot produced, as follows:</p>
<dl>
<dt><code class="calibre2">type="p"</code></dt>
<dd><p>Plot individual points (the default)</p>
</dd>
<dt><code class="calibre2">type="l"</code></dt>
<dd><p>Plot lines</p>
</dd>
<dt><code class="calibre2">type="b"</code></dt>
<dd><p>Plot points connected by lines (<em>both</em>)</p>
</dd>
<dt><code class="calibre2">type="o"</code></dt>
<dd><p>Plot points overlaid by lines</p>
</dd>
<dt><code class="calibre2">type="h"</code></dt>
<dd><p>Plot vertical lines from points to the zero axis (<em>high-density</em>)</p>
</dd>
<dt><code class="calibre2">type="s"</code><br />
<code class="calibre2">type="S"</code></dt>
<dd><p>Step-function plots. In the first form, the top of the vertical defines the point; in the second, the bottom.</p>
</dd>
<dt><code class="calibre2">type="n"</code></dt>
<dd><p>No plotting at all. However axes are still drawn (by default) and the coordinate system is set up according to the data. Ideal for creating plots with subsequent low-level graphics functions.</p>
</dd>
</dl>
</dd>
<dt><code class="calibre2">xlab=string</code><br />
<code class="calibre2">ylab=string</code></dt>
<dd><p>Axis labels for the <em>x</em> and <em>y</em> axes. Use these arguments to change the default labels, usually the names of the objects used in the call to the high-level plotting function.</p>
</dd>
<dt><code class="calibre2">main=string</code></dt>
<dd><p>Figure title, placed at the top of the plot in a large font.</p>
</dd>
<dt><code class="calibre2">sub=string</code></dt>
<dd><p>Sub-title, placed just below the <em>x</em>-axis in a smaller font.</p>
</dd>
</dl>
<hr />
<p><a href="" id="Low_002dlevel-plotting-commands"></a> <a href="" id="Low_002dlevel-plotting-commands-1"></a></p>
<h3 id="low-level-plotting-commands" class="section">12.2 Low-level plotting commands</h3>
<p>Sometimes the high-level plotting functions don’t produce exactly the kind of plot you desire. In this case, low-level plotting commands can be used to add extra information (such as points, lines or text) to the current plot.</p>
<p>Some of the more useful low-level plotting functions are:</p>
<dl>
<dt><code class="calibre2">points(x, y)</code><br />
<code class="calibre2">lines(x, y)</code></dt>
<dd><p><a href="" id="index-points"></a> <a href="" id="index-lines"></a></p>
<p>Adds points or connected lines to the current plot. <code class="calibre2">plot()</code>’s <code class="calibre2">type=</code> argument can also be passed to these functions (and defaults to <code class="calibre2">"p"</code> for <code class="calibre2">points()</code> and <code class="calibre2">"l"</code> for <code class="calibre2">lines()</code>.)</p>
</dd>
<dt><code class="calibre2">text(x, y, labels, …)</code></dt>
<dd><p><a href="" id="index-text"></a></p>
<p>Add text to a plot at points given by <code class="calibre2">x, y</code>. Normally <code class="calibre2">labels</code> is an integer or character vector in which case <code class="calibre2">labels[i]</code> is plotted at point <code class="calibre2">(x[i], y[i])</code>. The default is <code class="calibre2">1:length(x)</code>.</p>
<p><strong>Note</strong>: This function is often used in the sequence</p>
<div class="example">
<pre class="example1"><code>> plot(x, y, type="n"); text(x, y, names)</code></pre>
</div>
<p>The graphics parameter <code class="calibre2">type="n"</code> suppresses the points but sets up the axes, and the <code class="calibre2">text()</code> function supplies special characters, as specified by the character vector <code class="calibre2">names</code> for the points.</p>
</dd>
<dt><code class="calibre2">abline(a, b)</code><br />
<code class="calibre2">abline(h=y)</code><br />
<code class="calibre2">abline(v=x)</code><br />
<code class="calibre2">abline(lm.obj)</code></dt>
<dd><p><a href="" id="index-abline"></a></p>
<p>Adds a line of slope <code class="calibre2">b</code> and intercept <code class="calibre2">a</code> to the current plot. <code class="calibre2">h=y</code> may be used to specify <em>y</em>-coordinates for the heights of horizontal lines to go across a plot, and <code class="calibre2">v=x</code> similarly for the <em>x</em>-coordinates for vertical lines. Also lm.obj may be list with a <code class="calibre2">coefficients</code> component of length 2 (such as the result of model-fitting functions,) which are taken as an intercept and slope, in that order.</p>
</dd>
<dt><code class="calibre2">polygon(x, y, …)</code></dt>
<dd><p><a href="" id="index-polygon"></a></p>
<p>Draws a polygon defined by the ordered vertices in (<code class="calibre2">x</code>, <code class="calibre2">y</code>) and (optionally) shade it in with hatch lines, or fill it if the graphics device allows the filling of figures.</p>
</dd>
<dt><code class="calibre2">legend(x, y, legend, …)</code></dt>
<dd><p><a href="" id="index-legend"></a></p>
<p>Adds a legend to the current plot at the specified position. Plotting characters, line styles, colors etc., are identified with the labels in the character vector <code class="calibre2">legend</code>. At least one other argument v (a vector the same length as <code class="calibre2">legend</code>) with the corresponding values of the plotting unit must also be given, as follows:</p>
<dl>
<dt><code class="calibre2">legend( , fill=v)</code></dt>
<dd><p>Colors for filled boxes</p>
</dd>
<dt><code class="calibre2">legend( , col=v)</code></dt>
<dd><p>Colors in which points or lines will be drawn</p>
</dd>
<dt><code class="calibre2">legend( , lty=v)</code></dt>
<dd><p>Line styles</p>
</dd>
<dt><code class="calibre2">legend( , lwd=v)</code></dt>
<dd><p>Line widths</p>
</dd>
<dt><code class="calibre2">legend( , pch=v)</code></dt>
<dd><p>Plotting characters (character vector)</p>
</dd>
</dl>
</dd>
<dt><code class="calibre2">title(main, sub)</code></dt>
<dd><p><a href="" id="index-title"></a></p>
<p>Adds a title <code class="calibre2">main</code> to the top of the current plot in a large font and (optionally) a sub-title <code class="calibre2">sub</code> at the bottom in a smaller font.</p>
</dd>
<dt><code class="calibre2">axis(side, …)</code></dt>
<dd><p><a href="" id="index-axis"></a></p>
<p>Adds an axis to the current plot on the side given by the first argument (1 to 4, counting clockwise from the bottom.) Other arguments control the positioning of the axis within or beside the plot, and tick positions and labels. Useful for adding custom axes after calling <code class="calibre2">plot()</code> with the <code class="calibre2">axes=FALSE</code> argument.</p>
</dd>
</dl>
<p>Low-level plotting functions usually require some positioning information (e.g., <em>x</em> and <em>y</em> coordinates) to determine where to place the new plot elements. Coordinates are given in terms of <em>user coordinates</em> which are defined by the previous high-level graphics command and are chosen based on the supplied data.</p>
<p>Where <code class="calibre2">x</code> and <code class="calibre2">y</code> arguments are required, it is also sufficient to supply a single argument being a list with elements named <code class="calibre2">x</code> and <code class="calibre2">y</code>. Similarly a matrix with two columns is also valid input. In this way functions such as <code class="calibre2">locator()</code> (see below) may be used to specify positions on a plot interactively.</p>
<hr />
<p><a href="" id="Mathematical-annotation"></a> <a href="" id="Mathematical-annotation-1"></a></p>
<h4 id="mathematical-annotation" class="subheading">12.2.1 Mathematical annotation</h4>
<p>In some cases, it is useful to add mathematical symbols and formulae to a plot. This can be achieved in R by specifying an <em>expression</em> rather than a character string in any one of <code class="calibre2">text</code>, <code class="calibre2">mtext</code>, <code class="calibre2">axis</code>, or <code class="calibre2">title</code>. For example, the following code draws the formula for the Binomial probability function:</p>
<div class="example">
<pre class="example1"><code>> text(x, y, expression(paste(bgroup("(", atop(n, x), ")"), p^x, q^{n-x})))</code></pre>
</div>
<p>More information, including a full listing of the features available can obtained from within R using the commands:</p>
<div class="example">
<pre class="example1"><code>> help(plotmath)
> example(plotmath)
> demo(plotmath)</code></pre>
</div>
<hr />
<p><a href="" id="Hershey-vector-fonts"></a> <a href="" id="Hershey-vector-fonts-1"></a></p>
<h4 id="hershey-vector-fonts" class="subheading">12.2.2 Hershey vector fonts</h4>
<p>It is possible to specify Hershey vector fonts for rendering text when using the <code class="calibre2">text</code> and <code class="calibre2">contour</code> functions. There are three reasons for using the Hershey fonts:</p>
<ul>
<li>Hershey fonts can produce better output, especially on a computer screen, for rotated and/or small text.</li>
<li>Hershey fonts provide certain symbols that may not be available in the standard fonts. In particular, there are zodiac signs, cartographic symbols and astronomical symbols.</li>
<li>Hershey fonts provide cyrillic and japanese (Kana and Kanji) characters.</li>
</ul>
<p>More information, including tables of Hershey characters can be obtained from within R using the commands:</p>
<div class="example">
<pre class="example1"><code>> help(Hershey)
> demo(Hershey)
> help(Japanese)
> demo(Japanese)</code></pre>
</div>
<hr />
<p><a href="" id="Interacting-with-graphics"></a> <a href="" id="Interacting-with-graphics-1"></a></p>
<h3 id="interacting-with-graphics" class="section">12.3 Interacting with graphics</h3>
<p>R also provides functions which allow users to extract or add information to a plot using a mouse. The simplest of these is the <code class="calibre2">locator()</code> function:</p>
<dl>
<dt><code class="calibre2">locator(n, type)</code></dt>
<dd><p><a href="" id="index-locator"></a></p>
<p>Waits for the user to select locations on the current plot using the left mouse button. This continues until <code class="calibre2">n</code> (default 512) points have been selected, or another mouse button is pressed. The <code class="calibre2">type</code> argument allows for plotting at the selected points and has the same effect as for high-level graphics commands; the default is no plotting. <code class="calibre2">locator()</code> returns the locations of the points selected as a list with two components <code class="calibre2">x</code> and <code class="calibre2">y</code>.</p>
</dd>
</dl>
<p><code class="calibre2">locator()</code> is usually called with no arguments. It is particularly useful for interactively selecting positions for graphic elements such as legends or labels when it is difficult to calculate in advance where the graphic should be placed. For example, to place some informative text near an outlying point, the command</p>
<div class="example">
<pre class="example1"><code>> text(locator(1), "Outlier", adj=0)</code></pre>
</div>
<p>may be useful. (<code class="calibre2">locator()</code> will be ignored if the current device, such as <code class="calibre2">postscript</code> does not support interactive pointing.)</p>
<dl>
<dt><code class="calibre2">identify(x, y, labels)</code></dt>
<dd><p><a href="" id="index-identify"></a></p>
<p>Allow the user to highlight any of the points defined by <code class="calibre2">x</code> and <code class="calibre2">y</code> (using the left mouse button) by plotting the corresponding component of <code class="calibre2">labels</code> nearby (or the index number of the point if <code class="calibre2">labels</code> is absent). Returns the indices of the selected points when another button is pressed.</p>
</dd>
</dl>
<p>Sometimes we want to identify particular <em>points</em> on a plot, rather than their positions. For example, we may wish the user to select some observation of interest from a graphical display and then manipulate that observation in some way. Given a number of <em>(x, y)</em> coordinates in two numeric vectors <code class="calibre2">x</code> and <code class="calibre2">y</code>, we could use the <code class="calibre2">identify()</code> function as follows:</p>
<div class="example">
<pre class="example1"><code>> plot(x, y)
> identify(x, y)</code></pre>
</div>
<p>The <code class="calibre2">identify()</code> functions performs no plotting itself, but simply allows the user to move the mouse pointer and click the left mouse button near a point. If there is a point near the mouse pointer it will be marked with its index number (that is, its position in the <code class="calibre2">x</code>/<code class="calibre2">y</code> vectors) plotted nearby. Alternatively, you could use some informative string (such as a case name) as a highlight by using the <code class="calibre2">labels</code> argument to <code class="calibre2">identify()</code>, or disable marking altogether with the <code class="calibre2">plot = FALSE</code> argument. When the process is terminated (see above), <code class="calibre2">identify()</code> returns the indices of the selected points; you can use these indices to extract the selected points from the original vectors <code class="calibre2">x</code> and <code class="calibre2">y</code>.</p>
<hr />
<p><a href="" id="Using-graphics-parameters"></a> <a href="" id="Using-graphics-parameters-1"></a></p>
<h3 id="using-graphics-parameters" class="section">12.4 Using graphics parameters</h3>
<p>When creating graphics, particularly for presentation or publication purposes, R’s defaults do not always produce exactly that which is required. You can, however, customize almost every aspect of the display using <em>graphics parameters</em>. R maintains a list of a large number of graphics parameters which control things such as line style, colors, figure arrangement and text justification among many others. Every graphics parameter has a name (such as ‘<code class="calibre2">col</code>’, which controls colors,) and a value (a color number, for example.)</p>
<p>A separate list of graphics parameters is maintained for each active device, and each device has a default set of parameters when initialized. Graphics parameters can be set in two ways: either permanently, affecting all graphics functions which access the current device; or temporarily, affecting only a single graphics function call.</p>
<hr />
<p><a href="" id="The-par_0028_0029-function"></a> <a href="" id="Permanent-changes_003a-The-par_0028_0029-function"></a></p>
<h4 id="permanent-changes-the-par-function" class="subheading">12.4.1 Permanent changes: The <code class="calibre2">par()</code> function</h4>
<p><a href="" id="index-par"></a> <a href="" id="index-Graphics-parameters"></a></p>
<p>The <code class="calibre2">par()</code> function is used to access and modify the list of graphics parameters for the current graphics device.</p>
<dl>
<dt><code class="calibre2">par()</code></dt>
<dd><p>Without arguments, returns a list of all graphics parameters and their values for the current device.</p>
</dd>
<dt><code class="calibre2">par(c("col", "lty"))</code></dt>
<dd><p>With a character vector argument, returns only the named graphics parameters (again, as a list.)</p>
</dd>
<dt><code class="calibre2">par(col=4, lty=2)</code></dt>
<dd><p>With named arguments (or a single list argument), sets the values of the named graphics parameters, and returns the original values of the parameters as a list.</p>
</dd>
</dl>
<p>Setting graphics parameters with the <code class="calibre2">par()</code> function changes the value of the parameters <em>permanently</em>, in the sense that all future calls to graphics functions (on the current device) will be affected by the new value. You can think of setting graphics parameters in this way as setting “default” values for the parameters, which will be used by all graphics functions unless an alternative value is given.</p>
<p>Note that calls to <code class="calibre2">par()</code> <em>always</em> affect the global values of graphics parameters, even when <code class="calibre2">par()</code> is called from within a function. This is often undesirable behavior—usually we want to set some graphics parameters, do some plotting, and then restore the original values so as not to affect the user’s R session. You can restore the initial values by saving the result of <code class="calibre2">par()</code> when making changes, and restoring the initial values when plotting is complete.</p>
<div class="example">
<pre class="example1"><code>> oldpar <- par(col=4, lty=2)
… plotting commands …
> par(oldpar)</code></pre>
</div>
<p>To save and restore <em>all</em> settable<a href="appendix-f-references.html#FOOT25" id="DOCF25"><sup>25</sup></a> graphical parameters use</p>
<div class="example">
<pre class="example1"><code>> oldpar <- par(no.readonly=TRUE)
… plotting commands …
> par(oldpar)</code></pre>
</div>
<hr />
<p><a href="" id="Arguments-to-graphics-functions"></a> <a href="" id="Temporary-changes_003a-Arguments-to-graphics-functions"></a></p>
<h4 id="temporary-changes-arguments-to-graphics-functions" class="subheading">12.4.2 Temporary changes: Arguments to graphics functions</h4>
<p>Graphics parameters may also be passed to (almost) any graphics function as named arguments. This has the same effect as passing the arguments to the <code class="calibre2">par()</code> function, except that the changes only last for the duration of the function call. For example:</p>
<div class="example">
<pre class="example1"><code>> plot(x, y, pch="+")</code></pre>
</div>
<p>produces a scatterplot using a plus sign as the plotting character, without changing the default plotting character for future plots.</p>
<p>Unfortunately, this is not implemented entirely consistently and it is sometimes necessary to set and reset graphics parameters using <code class="calibre2">par()</code>.</p>
<hr />
<p><a href="" id="Graphics-parameters"></a> <a href="" id="Graphics-parameters-list"></a></p>
<h3 id="graphics-parameters-list" class="section">12.5 Graphics parameters list</h3>
<p>The following sections detail many of the commonly-used graphical parameters. The R help documentation for the <code class="calibre2">par()</code> function provides a more concise summary; this is provided as a somewhat more detailed alternative.</p>
<p>Graphics parameters will be presented in the following form:</p>
<dl>
<dt><code class="calibre2">name=value</code></dt>
<dd><p>A description of the parameter’s effect. name is the name of the parameter, that is, the argument name to use in calls to <code class="calibre2">par()</code> or a graphics function. value is a typical value you might use when setting the parameter.</p>
</dd>
</dl>
<p>Note that <code class="calibre2">axes</code> is <strong>not</strong> a graphics parameter but an argument to a few <code class="calibre2">plot</code> methods: see <code class="calibre2">xaxt</code> and <code class="calibre2">yaxt</code>.</p>
<hr />
<p><a href="" id="Graphical-elements"></a> <a href="" id="Graphical-elements-1"></a></p>
<h4 id="graphical-elements" class="subheading">12.5.1 Graphical elements</h4>
<p>R plots are made up of points, lines, text and polygons (filled regions.) Graphical parameters exist which control how these <em>graphical elements</em> are drawn, as follows:</p>
<dl>
<dt><code class="calibre2">pch="+"</code></dt>
<dd><p>Character to be used for plotting points. The default varies with graphics drivers, but it is usually a circle. Plotted points tend to appear slightly above or below the appropriate position unless you use <code class="calibre2">"."</code> as the plotting character, which produces centered points.</p>
</dd>
<dt><code class="calibre2">pch=4</code></dt>
<dd><p>When <code class="calibre2">pch</code> is given as an integer between 0 and 25 inclusive, a specialized plotting symbol is produced. To see what the symbols are, use the command</p>
<div class="example">
<pre class="example1"><code>> legend(locator(1), as.character(0:25), pch = 0:25)</code></pre>
</div>
<p>Those from 21 to 25 may appear to duplicate earlier symbols, but can be coloured in different ways: see the help on <code class="calibre2">points</code> and its examples.</p>
<p>In addition, <code class="calibre2">pch</code> can be a character or a number in the range <code class="calibre2">32:255</code> representing a character in the current font.</p>
</dd>
<dt><code class="calibre2">lty=2</code></dt>
<dd><p>Line types. Alternative line styles are not supported on all graphics devices (and vary on those that do) but line type 1 is always a solid line, line type 0 is always invisible, and line types 2 and onwards are dotted or dashed lines, or some combination of both.</p>
</dd>
<dt><code class="calibre2">lwd=2</code></dt>
<dd><p>Line widths. Desired width of lines, in multiples of the “standard” line width. Affects axis lines as well as lines drawn with <code class="calibre2">lines()</code>, etc. Not all devices support this, and some have restrictions on the widths that can be used.</p>
</dd>
<dt><code class="calibre2">col=2</code></dt>
<dd><p>Colors to be used for points, lines, text, filled regions and images. A number from the current palette (see <code class="calibre2">?palette</code>) or a named colour.</p>
</dd>
<dt><code class="calibre2">col.axis</code><br />
<code class="calibre2">col.lab</code><br />
<code class="calibre2">col.main</code><br />
<code class="calibre2">col.sub</code></dt>
<dd><p>The color to be used for axis annotation, <em>x</em> and <em>y</em> labels, main and sub-titles, respectively.</p>
</dd>
<dt><code class="calibre2">font=2</code></dt>
<dd><p>An integer which specifies which font to use for text. If possible, device drivers arrange so that <code class="calibre2">1</code> corresponds to plain text, <code class="calibre2">2</code> to bold face, <code class="calibre2">3</code> to italic, <code class="calibre2">4</code> to bold italic and <code class="calibre2">5</code> to a symbol font (which include Greek letters).</p>
</dd>
<dt><code class="calibre2">font.axis</code><br />
<code class="calibre2">font.lab</code><br />
<code class="calibre2">font.main</code><br />
<code class="calibre2">font.sub</code></dt>
<dd><p>The font to be used for axis annotation, <em>x</em> and <em>y</em> labels, main and sub-titles, respectively.</p>
</dd>
<dt><code class="calibre2">adj=-0.1</code></dt>
<dd><p>Justification of text relative to the plotting position. <code class="calibre2">0</code> means left justify, <code class="calibre2">1</code> means right justify and <code class="calibre2">0.5</code> means to center horizontally about the plotting position. The actual value is the proportion of text that appears to the left of the plotting position, so a value of <code class="calibre2">-0.1</code> leaves a gap of 10% of the text width between the text and the plotting position.</p>
</dd>
<dt><code class="calibre2">cex=1.5</code></dt>
<dd><p>Character expansion. The value is the desired size of text characters (including plotting characters) relative to the default text size.</p>
</dd>
<dt><code class="calibre2">cex.axis</code><br />
<code class="calibre2">cex.lab</code><br />
<code class="calibre2">cex.main</code><br />
<code class="calibre2">cex.sub</code></dt>
<dd><p>The character expansion to be used for axis annotation, <em>x</em> and <em>y</em> labels, main and sub-titles, respectively.</p>
</dd>
</dl>
<hr />
<p><a href="" id="Axes-and-tick-marks"></a> <a href="" id="Axes-and-tick-marks-1"></a></p>
<h4 id="axes-and-tick-marks" class="subheading">12.5.2 Axes and tick marks</h4>
<p>Many of R’s high-level plots have axes, and you can construct axes yourself with the low-level <code class="calibre2">axis()</code> graphics function. Axes have three main components: the <em>axis line</em> (line style controlled by the <code class="calibre2">lty</code> graphics parameter), the <em>tick marks</em> (which mark off unit divisions along the axis line) and the <em>tick labels</em> (which mark the units.) These components can be customized with the following graphics parameters.</p>
<dl>
<dt><code class="calibre2">lab=c(5, 7, 12)</code></dt>
<dd><p>The first two numbers are the desired number of tick intervals on the <em>x</em> and <em>y</em> axes respectively. The third number is the desired length of axis labels, in characters (including the decimal point.) Choosing a too-small value for this parameter may result in all tick labels being rounded to the same number!</p>
</dd>
<dt><code class="calibre2">las=1</code></dt>
<dd><p>Orientation of axis labels. <code class="calibre2">0</code> means always parallel to axis, <code class="calibre2">1</code> means always horizontal, and <code class="calibre2">2</code> means always perpendicular to the axis.</p>
</dd>
<dt><code class="calibre2">mgp=c(3, 1, 0)</code></dt>
<dd><p>Positions of axis components. The first component is the distance from the axis label to the axis position, in text lines. The second component is the distance to the tick labels, and the final component is the distance from the axis position to the axis line (usually zero). Positive numbers measure outside the plot region, negative numbers inside.</p>
</dd>
<dt><code class="calibre2">tck=0.01</code></dt>
<dd><p>Length of tick marks, as a fraction of the size of the plotting region. When <code class="calibre2">tck</code> is small (less than 0.5) the tick marks on the <em>x</em> and <em>y</em> axes are forced to be the same size. A value of 1 gives grid lines. Negative values give tick marks outside the plotting region. Use <code class="calibre2">tck=0.01</code> and <code class="calibre2">mgp=c(1,-1.5,0)</code> for internal tick marks.</p>
</dd>
<dt><code class="calibre2">xaxs="r"</code><br />
<code class="calibre2">yaxs="i"</code></dt>
<dd><p>Axis styles for the <em>x</em> and <em>y</em> axes, respectively. With styles <code class="calibre2">"i"</code> (internal) and <code class="calibre2">"r"</code> (the default) tick marks always fall within the range of the data, however style <code class="calibre2">"r"</code> leaves a small amount of space at the edges. (S has other styles not implemented in R.)</p>
</dd>
</dl>
<hr />
<p><a href="" id="Figure-margins"></a> <a href="" id="Figure-margins-1"></a></p>
<h4 id="figure-margins" class="subheading">12.5.3 Figure margins</h4>
<p>A single plot in R is known as a <code class="calibre2">figure</code> and comprises a <em>plot region</em> surrounded by margins (possibly containing axis labels, titles, etc.) and (usually) bounded by the axes themselves.</p>
<p>A typical figure is</p>
<p><img src="fig11.png" alt="images/fig11" class="calibre20" /></p>
<p>Graphics parameters controlling figure layout include:</p>
<dl>
<dt><code class="calibre2">mai=c(1, 0.5, 0.5, 0)</code></dt>
<dd><p>Widths of the bottom, left, top and right margins, respectively, measured in inches.</p>
</dd>
<dt><code class="calibre2">mar=c(4, 2, 2, 1)</code></dt>
<dd><p>Similar to <code class="calibre2">mai</code>, except the measurement unit is text lines.</p>
</dd>
</dl>
<p><code class="calibre2">mar</code> and <code class="calibre2">mai</code> are equivalent in the sense that setting one changes the value of the other. The default values chosen for this parameter are often too large; the right-hand margin is rarely needed, and neither is the top margin if no title is being used. The bottom and left margins must be large enough to accommodate the axis and tick labels. Furthermore, the default is chosen without regard to the size of the device surface: for example, using the <code class="calibre2">postscript()</code> driver with the <code class="calibre2">height=4</code> argument will result in a plot which is about 50% margin unless <code class="calibre2">mar</code> or <code class="calibre2">mai</code> are set explicitly. When multiple figures are in use (see below) the margins are reduced, however this may not be enough when many figures share the same page.</p>
<hr />
<p><a href="" id="Multiple-figure-environment"></a> <a href="" id="Multiple-figure-environment-1"></a></p>
<h4 id="multiple-figure-environment" class="subheading">12.5.4 Multiple figure environment</h4>
<p>R allows you to create an <em>n</em> by <em>m</em> array of figures on a single page. Each figure has its own margins, and the array of figures is optionally surrounded by an <em>outer margin</em>, as shown in the following figure.</p>
<p><img src="fig12.png" alt="images/fig12" class="calibre20" /></p>
<p>The graphical parameters relating to multiple figures are as follows:</p>
<dl>
<dt><code class="calibre2">mfcol=c(3, 2)</code><br />
<code class="calibre2">mfrow=c(2, 4)</code></dt>
<dd><p>Set the size of a multiple figure array. The first value is the number of rows; the second is the number of columns. The only difference between these two parameters is that setting <code class="calibre2">mfcol</code> causes figures to be filled by column; <code class="calibre2">mfrow</code> fills by rows.</p>
<p>The layout in the Figure could have been created by setting <code class="calibre2">mfrow=c(3,2)</code>; the figure shows the page after four plots have been drawn.</p>
<p>Setting either of these can reduce the base size of symbols and text (controlled by <code class="calibre2">par("cex")</code> and the pointsize of the device). In a layout with exactly two rows and columns the base size is reduced by a factor of 0.83: if there are three or more of either rows or columns, the reduction factor is 0.66.</p>
</dd>
<dt><code class="calibre2">mfg=c(2, 2, 3, 2)</code></dt>
<dd><p>Position of the current figure in a multiple figure environment. The first two numbers are the row and column of the current figure; the last two are the number of rows and columns in the multiple figure array. Set this parameter to jump between figures in the array. You can even use different values for the last two numbers than the <em>true</em> values for unequally-sized figures on the same page.</p>
</dd>
<dt><code class="calibre2">fig=c(4, 9, 1, 4)/10</code></dt>
<dd><p>Position of the current figure on the page. Values are the positions of the left, right, bottom and top edges respectively, as a percentage of the page measured from the bottom left corner. The example value would be for a figure in the bottom right of the page. Set this parameter for arbitrary positioning of figures within a page. If you want to add a figure to a current page, use <code class="calibre2">new=TRUE</code> as well (unlike S).</p>
</dd>
<dt><code class="calibre2">oma=c(2, 0, 3, 0)</code><br />
<code class="calibre2">omi=c(0, 0, 0.8, 0)</code></dt>
<dd><p>Size of outer margins. Like <code class="calibre2">mar</code> and <code class="calibre2">mai</code>, the first measures in text lines and the second in inches, starting with the bottom margin and working clockwise.</p>
</dd>
</dl>
<p>Outer margins are particularly useful for page-wise titles, etc. Text can be added to the outer margins with the <code class="calibre2">mtext()</code> function with argument <code class="calibre2">outer=TRUE</code>. There are no outer margins by default, however, so you must create them explicitly using <code class="calibre2">oma</code> or <code class="calibre2">omi</code>.</p>
<p>More complicated arrangements of multiple figures can be produced by the <code class="calibre2">split.screen()</code> and <code class="calibre2">layout()</code> functions, as well as by the <strong>grid</strong> and <a href="https://CRAN.R-project.org/package=lattice"><strong>lattice</strong></a> packages.</p>
<hr />
<p><a href="" id="Device-drivers"></a> <a href="" id="Device-drivers-1"></a></p>
<h3 id="device-drivers" class="section">12.6 Device drivers</h3>
<p><a href="" id="index-Graphics-device-drivers"></a></p>
<p>R can generate graphics (of varying levels of quality) on almost any type of display or printing device. Before this can begin, however, R needs to be informed what type of device it is dealing with. This is done by starting a <em>device driver</em>. The purpose of a device driver is to convert graphical instructions from R (“draw a line,” for example) into a form that the particular device can understand.</p>
<p>Device drivers are started by calling a device driver function. There is one such function for every device driver: type <code class="calibre2">help(Devices)</code> for a list of them all. For example, issuing the command</p>
<div class="example">
<pre class="example1"><code>> postscript()</code></pre>
</div>
<p>causes all future graphics output to be sent to the printer in PostScript format. Some commonly-used device drivers are:</p>
<dl>
<dt><code class="calibre2">X11()</code></dt>
<dd><p><a href="" id="index-X11"></a></p>
<p>For use with the X11 window system on Unix-alikes</p>
</dd>
<dt><code class="calibre2">windows()</code></dt>
<dd><p><a href="" id="index-windows"></a></p>
<p>For use on Windows</p>
</dd>
<dt><code class="calibre2">quartz()</code></dt>
<dd><p><a href="" id="index-quartz"></a></p>
<p>For use on macOS</p>
</dd>
<dt><code class="calibre2">postscript()</code></dt>
<dd><p><a href="" id="index-postscript"></a></p>
<p>For printing on PostScript printers, or creating PostScript graphics files.</p>
</dd>
<dt><code class="calibre2">pdf()</code></dt>
<dd><p><a href="" id="index-pdf"></a></p>
<p>Produces a PDF file, which can also be included into PDF files.</p>
</dd>
<dt><code class="calibre2">png()</code></dt>
<dd><p><a href="" id="index-png"></a></p>
<p>Produces a bitmap PNG file. (Not always available: see its help page.)</p>
</dd>
<dt><code class="calibre2">jpeg()</code></dt>
<dd><p><a href="" id="index-jpeg"></a></p>
<p>Produces a bitmap JPEG file, best used for <code class="calibre2">image</code> plots. (Not always available: see its help page.)</p>
</dd>
</dl>
<p>When you have finished with a device, be sure to terminate the device driver by issuing the command</p>
<div class="example">
<pre class="example1"><code>> dev.off()</code></pre>
</div>
<p>This ensures that the device finishes cleanly; for example in the case of hardcopy devices this ensures that every page is completed and has been sent to the printer. (This will happen automatically at the normal end of a session.)</p>
<hr />
<p><a href="" id="PostScript-diagrams-for-typeset-documents"></a> <a href="" id="PostScript-diagrams-for-typeset-documents-1"></a></p>
<h4 id="postscript-diagrams-for-typeset-documents" class="subheading">12.6.1 PostScript diagrams for typeset documents</h4>
<p>By passing the <code class="calibre2">file</code> argument to the <code class="calibre2">postscript()</code> device driver function, you may store the graphics in PostScript format in a file of your choice. The plot will be in landscape orientation unless the <code class="calibre2">horizontal=FALSE</code> argument is given, and you can control the size of the graphic with the <code class="calibre2">width</code> and <code class="calibre2">height</code> arguments (the plot will be scaled as appropriate to fit these dimensions.) For example, the command</p>
<div class="example">
<pre class="example1"><code>> postscript("file.ps", horizontal=FALSE, height=5, pointsize=10)</code></pre>
</div>
<p>will produce a file containing PostScript code for a figure five inches high, perhaps for inclusion in a document. It is important to note that if the file named in the command already exists, it will be overwritten. This is the case even if the file was only created earlier in the same R session.</p>
<p>Many usages of PostScript output will be to incorporate the figure in another document. This works best when <em>encapsulated</em> PostScript is produced: R always produces conformant output, but only marks the output as such when the <code class="calibre2">onefile=FALSE</code> argument is supplied. This unusual notation stems from S-compatibility: it really means that the output will be a single page (which is part of the EPSF specification). Thus to produce a plot for inclusion use something like</p>
<div class="example">
<pre class="example1"><code>> postscript("plot1.eps", horizontal=FALSE, onefile=FALSE,
height=8, width=6, pointsize=10)</code></pre>
</div>
<hr />
<p><a href="" id="Multiple-graphics-devices"></a> <a href="" id="Multiple-graphics-devices-1"></a></p>
<h4 id="multiple-graphics-devices" class="subheading">12.6.2 Multiple graphics devices</h4>
<p>In advanced use of R it is often useful to have several graphics devices in use at the same time. Of course only one graphics device can accept graphics commands at any one time, and this is known as the <em>current device</em>. When multiple devices are open, they form a numbered sequence with names giving the kind of device at any position.</p>
<p>The main commands used for operating with multiple devices, and their meanings are as follows:</p>
<dl>
<dt><code class="calibre2">X11()</code></dt>
<dd><p>[UNIX]</p>
</dd>
<dt><code class="calibre2">windows()</code><br />
<code class="calibre2">win.printer()</code><br />
<code class="calibre2">win.metafile()</code></dt>
<dd><p>[Windows]</p>
</dd>
<dt><code class="calibre2">quartz()</code></dt>
<dd><p>[macOS]</p>
</dd>
<dt><code class="calibre2">postscript()</code><br />
<code class="calibre2">pdf()</code><br />
<code class="calibre2">png()</code><br />
<code class="calibre2">jpeg()</code><br />
<code class="calibre2">tiff()</code><br />
<code class="calibre2">bitmap()</code><br />
<code class="calibre2">…</code></dt>
<dd><p>Each new call to a device driver function opens a new graphics device, thus extending by one the device list. This device becomes the current device, to which graphics output will be sent.</p>
</dd>
<dt><code class="calibre2">dev.list()</code></dt>
<dd><p><a href="" id="index-dev_002elist"></a></p>
<p>Returns the number and name of all active devices. The device at position 1 on the list is always the <em>null device</em> which does not accept graphics commands at all.</p>
</dd>
<dt><code class="calibre2">dev.next()</code><br />
<code class="calibre2">dev.prev()</code></dt>
<dd><p><a href="" id="index-dev_002enext"></a> <a href="" id="index-dev_002eprev"></a></p>
<p>Returns the number and name of the graphics device next to, or previous to the current device, respectively.</p>
</dd>
<dt><code class="calibre2">dev.set(which=k)</code></dt>
<dd><p><a href="" id="index-dev_002eset"></a></p>
<p>Can be used to change the current graphics device to the one at position k of the device list. Returns the number and label of the device.</p>
</dd>
<dt><code class="calibre2">dev.off(k)</code></dt>
<dd><p><a href="" id="index-dev_002eoff"></a></p>
<p>Terminate the graphics device at point k of the device list. For some devices, such as <code class="calibre2">postscript</code> devices, this will either print the file immediately or correctly complete the file for later printing, depending on how the device was initiated.</p>
</dd>
<dt><code class="calibre2">dev.copy(device, …, which=k)</code><br />
<code class="calibre2">dev.print(device, …, which=k)</code></dt>
<dd><p>Make a copy of the device k. Here <code class="calibre2">device</code> is a device function, such as <code class="calibre2">postscript</code>, with extra arguments, if needed, specified by ‘…’. <code class="calibre2">dev.print</code> is similar, but the copied device is immediately closed, so that end actions, such as printing hardcopies, are immediately performed.</p>
</dd>
<dt><code class="calibre2">graphics.off()</code></dt>
<dd><p>Terminate all graphics devices on the list, except the null device.</p>
</dd>
</dl>
<hr />
<p><a href="" id="Dynamic-graphics"></a> <a href="" id="Dynamic-graphics-1"></a></p>
<h3 id="dynamic-graphics" class="section">12.7 Dynamic graphics</h3>
<p><a href="" id="index-Dynamic-graphics"></a></p>
<p>R does not have builtin capabilities for dynamic or interactive graphics, e.g. rotating point clouds or to “brushing” (interactively highlighting) points. However, extensive dynamic graphics facilities are available in the system GGobi by Swayne, Cook and Buja available from</p>
<blockquote>
<p><a href="http://www.ggobi.org/" class="uri">http://www.ggobi.org/</a></p>
</blockquote>
<p>and these can be accessed from R via the package <a href="https://CRAN.R-project.org/package=rggobi"><strong>rggobi</strong></a>, described at <a href="http://www.ggobi.org/rggobi" class="uri">http://www.ggobi.org/rggobi</a>.</p>
<p>Also, package <a href="https://CRAN.R-project.org/package=rgl"><strong>rgl</strong></a> provides ways to interact with 3D plots, for example of surfaces.</p>
<hr />
<p><a href="" id="Packages"></a> <a href="" id="Packages-1"></a></p>
<div id="calibre_pb_28" class="calibre8">
</div>