Beruflich Dokumente
Kultur Dokumente
Paul Murrell
paul@stat.auckland.ac.nz
R Graphics p.1/47
Overview
Standard (base) R graphics grid graphics Graphics Regions and Coordinate Systems Directing Graphics Output Producing Graphics Output Plots from First Principles grid and lattice
R Graphics p.2/47
R Graphics Fundamentals
Graphics Regions and Coordinate Systems Outer Margins Figure Regions Figure Margins Plot Regions Directing Graphics Output Which graphics functions to use Producing Graphics Output Graphical parameters
R Graphics p.3/47
Outer margin 2
Figure Region
Outer margin 1
Outer margin 4
R Graphics p.4/47
Figure 2
Figure 1
R Graphics p.5/47
Figure Margin 3
Figure Margin 2
Outer margin 2
Figure Margin 4
Plot Region
Figure Margin 1
Outer margin 1
Outer margin 4
R Graphics p.6/47
User Coordinates
Maximum y value
Minimum y value
yi
Minimum x value
xi
Maximum x value
R Graphics p.7/47
11
3 lines
R Graphics p.8/47
11 Current Plot
11 Current Plot 0 0 11
es
0 0
11
lin
es
Outer Margin 1
es
lin
Outer Margin 1
es
lin
lin
R Graphics p.9/47
R Graphics p.10/47
Graphical Parameters
Permanent settings par(<param>=) Temporary settings <plot.function>(..., <param>=)
colour of lines, text, ... line width line type font face (plain, bold, italic) type of plotting symbol string rotation
R Graphics p.11/47
1:10 0 0 2 4
10
10
R Graphics p.12/47
R Graphics p.13/47
12
14
Group 4 10
Group 3
Group 2 4
Group 1 2 0
R Graphics p.14/47
R Graphics p.15/47
R Graphics p.16/47
R Graphics p.17/47
Viewports
0.
5n
pc
0.
0.5npc
25 1 0
np
0.5npc
R Graphics p.18/47
R Graphics p.19/47
R Graphics p.20/47
R Graphics p.21/47
R Graphics p.22/47
R Graphics p.23/47
20
40
60
80
100
R Graphics p.24/47
Units
"npc"
"native" "inches"
"cm"
Normalised Parent Coordinates. Treats the bottom-left corner of the current viewand the top-right port as the location corner as . Locations and sizes are relative to the xand y-scales for the current viewport. Locations and sizes are in terms of physical inches. For locations, is at the bottom-left of the viewport. Same as "inches", except in centimetres.
R Graphics p.25/47
Units
"char"
"lines"
"snpc"
Locations and sizes are specied in terms of multiples of the current nominal fontheight. Locations and sizes are specied in terms of multiples of the height of a line of text (dependent on both the current fontsize and the current lineheight). Square Normalised Parent Coordinates. Locations and size are expressed as a proportion of the smaller of the width and height of the current viewport.
R Graphics p.26/47
Units
"strwidth"
"strheight" "grobwidth"
"grobheight"
Locations and sizes are expressed as multiples of the width of a given string (dependent on the string and the current fontsize). Like "strwidth". Locations and sizes are expressed as multiples of the width of a given graphical object (dependent on the current state of the graphical object). Like "grobwidth".
R Graphics p.27/47
R Graphics p.28/47
R Graphics p.29/47
Layouts
push.viewport(viewport(layout=grid.layout(4, 5))) grid.rect(gp=gpar(col="grey")) grid.segments(c(1:4/5, rep(0, 3)), c(rep(0, 4), 1:3/4), c(1:4/5, rep(1, 3)), c(rep(1, 4), 1:3/4), gp=gpar(col="grey")) push.viewport(viewport(layout.pos.col=2:3, layout.pos.row=3)) grid.rect(gp=gpar(lwd=3)) pop.viewport(2)
R Graphics p.30/47
Layouts
R Graphics p.31/47
Layouts
grid.layout(4, 4, widths=unit(c(3, 1, 1, 1), c("lines", "null", "null", "cm")), heights=unit(c(1, 1, 2, 3), c("cm", "null", "null", "lines")))
R Graphics p.32/47
Layouts
3lines 1cm (1, 1) 1null (1, 2) 1null (1, 3) 1cm (1, 4) 1cm
1null (2, 1)
(2, 2)
(2, 3)
(2, 4) 1null
2null (3, 1)
(3, 2)
(3, 3)
(3, 4) 2null
(4, 2) 1null
(4, 3) 1null
R Graphics p.33/47
R Graphics p.34/47
Graphical Parameters
Specify using gp argument of viewport or graphical object. Viewport settings are inherited by subsequent viewports and graphical objects.
col fill lwd lty fontface fontfamily fontsize
colour of lines, text, ... colour for lling polygons, ... line width line type font face (plain, bold, italic) font family (Helvetica, Hershey, ...) font size (points)
R Graphics p.35/47
Graphical Parameters
push.viewport( viewport(gp=gpar(fill="grey", fontface="italic"))) grid.rect() grid.rect(width=0.8, height=0.6, gp=gpar(fill="white")) grid.text("This text and the inner rectangle\n have specified their own gpar settings", y=0.75, gp=gpar(fontface="plain")) grid.text("This text and the outer rectangle\n accept the gpar settings of the viewport", y=0.25) pop.viewport()
R Graphics p.36/47
Graphical Parameters
This text and the inner rectangle have specified their own gpar settings
This text and the outer rectangle accept the gpar settings of the viewport
R Graphics p.37/47
R Graphics p.38/47
10
1:10
10
R Graphics p.39/47
R Graphics p.40/47
R Graphics p.41/47
R Graphics p.42/47
10 Group A 8 Group B 6
Group C
2 Something Longer 0
R Graphics p.43/47
R Graphics p.44/47
R Graphics p.45/47
R Graphics p.46/47
A panel of text produced using raw grid code that describes the plot to the right.
R Graphics p.47/47