directions for r graphics development
TRANSCRIPT
Directions For R Graphics Development
Ross Ihaka
The R Foundation & University of Auckland
Origins of The R Graphics System
• The R base graphics system is loosely based on theGRZ graphics system used in S.
• This in turn was based on an even earlier GRX graphicssystem.
• The initial implementation of the graphics system wasdone 1994 and refined in the years since then.
• The new R graphics system “Grid” is now emergingand promises much more flexible graphics capabilities.
The Nature of the Graphics System
The R graphics systems are built upon a small set of graphicalprimitives.
Graph
ics T
ext
line segments filled polygons text strings
Drawing is carried out using a “painting metaphor” – laterpicture elements are drawn over earlier ones.
Graphics Organization
The basic graphics capabilities are packaged with otherfacilities.
• Layout capabilities
• Coordinate transformations
• State management
Most R graphical displays are built using these simplefacilities.
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
5 10 15 20 25
020
4060
8010
012
0
Stopping Distance vs Speed of 50 Cars
Speed (mph)
Sto
ppin
g D
ista
nce
(ft)
$4.0$4.6
$5.5
$6.2$6.7
$7.4$7.8
$8.5
$9.7
$10.7 $10.8
1966−’67
’67−’68
’68−’69
’69−’70
’70−’71
’71−’72
’72−’73
’73−’74
’74−’75
’75−’76
’76−’77
New York StateTotal Budget Expenditures andAid to Localities In billions of dollars
Total Budget
Total Aid toLocalities**Varying from a lowof 56.7 percent oftotal in 1970−71to a high of 60.7percent in 1972−73
Estimated Recommended
$4.0$4.6
$5.5
$6.2$6.7
$7.4$7.8
$8.5
$9.7
$10.7 $10.8
1966−’67
’67−’68
’68−’69
’69−’70
’70−’71
’71−’72
’72−’73
’73−’74
’74−’75
’75−’76
’76−’77
New York StateTotal Budget Expenditures andAid to Localities In billions of dollars
Total Budget
Total Aid toLocalities**Varying from a lowof 56.7 percent oftotal in 1970−71to a high of 60.7percent in 1972−73
Estimated Recommended
Techniques
In this case we are using a custom page size and explicitlycolouring the page white. The outer boundary of the page hasa box drawn around it
pdf("tufte.pdf", width = 7, height = 4.5,bg = "white", paper = "special")
...
box("outer")
The coordinates within the plot were determined bymeasuring the original (in mm).
0°− 9°
− 21°− 11°
− 20°− 24°
− 30°− 26°
Oct.18Nov.9Nov.14Nov.28Dec.1Dec.6Dec.7
100 km
Moscow
MaloyaroslavetsVyazma
Polotsk
Minsk
Vilna Smolensk
Borodino
Dnieper R.
Berezina R.
Nieman R.
The Minard Map of Napoleon’s 1812 Campaign in Russia
Techniques
pdf("minard.pdf", width=7, height=4,bg = "transparent", paper="special")
...
usr = par("usr")rect(usr[1], usr[3], usr[2], usr[4],
col = "white", border = NA)
...
box("plot")
5
10
15
20Jan
Feb
Mar
Apr
May
JunJul
Aug
Sep
Oct
Nov
Dec
Average Monthly Temperatures in London
Drawing Circles With R
When the changes of direction in a chain of line segments areless than 5◦, humans perceive the chain as a smooth curve.
This suggests the following recipe for drawing a “circle.”
plot.new()plot.window(xlim = c(-r, r),
ylim = c(-r, r),asp = 1)
...
ang = seq(0, 2 * pi, length = 73)[1:72]polygon(r * cos(ans), r * sin(ang))
Tricks for Presentation Graphics
• The graphics in R (and S) are usually considered to bevery good for publication, but not exciting enough foron-screen presentation.
• There are few simple tricks which make it possible to“brighten up” the displays considerably.
• At present, using these tricks requires some low-levelgraphics programming, but I hope to package at leastsome of this functionality in a presentation graphicspackage.
0 10 20 30 40
Lamb
Mutton
Pigmeat
Poultry
Beef
New Zealand Meat Consumption by Category
Percentage in Category
Creating Drop Shadows
The effect of a shadow can be created can be created bydrawing a copy of an object in gray, offset to the right anddownward, before drawing the object itself.
rect(xl + xinch(.04), yl - yinch(.04),xr + xinch(.04), yr - yinch(.04),col = "gray", border = "gray")
rect(xl, yl, xr, yr, col = "darkseagreen")
0 5 10 15 20 25 30
0
2
4
6
8
●●
●
●
●
●
●●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●
●●
●
●
●
●
Jazzing Up R Graphics
Line colour variation.
0 5 10 15 20 25 30
0
2
4
6
8
●●
●
●
●
●
●●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●● ●
●
●
●
●
●
●
●
●
●●
●
●
●
●
Jazzing Up R Graphics
Line colour variation and shadows.
Filled Points and Lines
lines(x + xinch(.04), y + yinch(.04),col = "gray", lwd = 5)
points(x + xinch(.04), y + yinch(.04),pch = 21, cex = 1.5,col = "gray", bg = "gray")
lines(x, y, col = "black", lwd = 5)lines(x, y, col = "yellow", lwd = 2)
points(x, y, pch = 21,col = "black", bg = "lightblue")
1960 1970 1980 1990 2000
−0.2
0.0
0.2
0.4
0.6
Annual Global Temperature Increase (°C)
Time
Color Gradients
The colour gradient in the previous plot is easy to produce.
for(d in seq(0, 1, length = 50))polygon(x, y - .3 * d,
col = hsv((1 - i)/6),border = NA)
Rectangular gradients are even easier.
rect(xl, yb, xr, seq(yb, yt, length = 100),col = hsv(seq(1/3, 2/3, length = 100), .2, 1),border = NA)
Extended Graphics Operations
• While the basic R graphics engine is capable ofproducing a very wide range of graphs, there are somefacilities which are clearly missing.
• I have a number of items on my To-Do list.
– Improved polygon rendering.
– Perceptually based colour handling
– A 3-d rendering toolkit
– Interactive 3-d graphics
Polygon Rendering
• In R, a polygon is defined by a (possiblyself-intersecting) polygonal path whose last point isjoined to its first.
• The filling of such polygons is carried out in a devicedependent fashion.
Polygons “With Holes”
• An alternative way of defining polygonal regions iswith a set of polygonal rings.
• Some of the rings define exterior boundaries and someinterior.
Rendering Polygons With Holes
• Some devices provide the ability to render polygonswith holes.
• For other devices the simplest strategy is to convert thepolygons into simply connected ones.
Cross Hatching and Dot Shading
Cross-hatching and dot shading provide an alternative tofilling polygons with solid colours.
An Efficient Cross Hatching Algorithm
• Rotate the polygon so that the cross-hatching lines arehorizontal.
• Use an edge-table and edge-coherence to compute theline/edge intersections.
Logical Operations on Polygons
Intersection Set Difference
Implementing Logical Operations
• The most commonly used code for carrying outclipping and logical operations on polygons is AlanMurta’s GPC implementation of the Vatti algorithm.This implementation is not “free” and is not competitivewith newer algorithms.
• The Greiner & Hormann algorithm (1998,ACMTransactions on Graphics) handles self-intersectingpolygons efficiently.
• The Liu, Gao & Huang algorithm (2003,Journal ofSoftware) handles the “polygons with holes” case.
• Both of these algorithms are easy to implement.
Colour In R
• The present implementation of Colour in R uses a24-bit colour model.
• The device dependence of R’s colour description isbecoming an issue.
• Predictable colour results will require the introductionof a device independent model.
Colour Choice for Presentation Graphics
• Choosing a “good” set of colours for area fills inpresentation graphics is a difficult problem.
• One method of choosing such colours is to work in aperceptually uniform colour space (e.g. CIELAB) andto choose colours with equal luminance and chroma(equal impact colours).
• This produces harmonious colours and eliminateseffects such as the misjudgement of sizes caused by theirradiation illusion.
An HSV Colour Wheel
A CIELAB Colour Wheel
72 73 74 75 76 77 78 79 80 81 82 83 84 85
FallSummerSpringWinter
Computer Science PhD Graduates
Year
Stu
dent
s
0
5
10
15
20
25
30
A Colour Tetrad
72 73 74 75 76 77 78 79 80 81 82 83 84 85
FallSummerSpringWinter
Computer Science PhD Graduates
Year
Stu
dent
s
0
5
10
15
20
25
30
Analogous Colours
72 73 74 75 76 77 78 79 80 81 82 83 84 85
FallSummerSpringWinter
Computer Science PhD Graduates
Year
Stu
dent
s
0
5
10
15
20
25
30
Metaphorical Colours
72 73 74 75 76 77 78 79 80 81 82 83 84 85
FallSummerSpringWinter
Computer Science PhD Graduates
Year
Stu
dent
s
0
5
10
15
20
25
30
Cool Colours
72 73 74 75 76 77 78 79 80 81 82 83 84 85
FallSummerSpringWinter
Computer Science PhD Graduates
Year
Stu
dent
s
0
5
10
15
20
25
30
Warm Colours
Three Dimensional Graphics
Implementing a 3D Toolkit
• To produce high quality output it is important to have avector-based package.
• A reasonable approach is to build a general toolkitbased on BSP trees.
• This would permit both hidden line and hidden surfacemethods to be implemented.
• It would also be useful to investigate the possibilities ofnon-photorealistic rendering.
Realistic and Abstract Rendering Techniques
Interactive Graphics
• R is very strong in the area of static graphics, but morelimited in the area of interactive graphics.
• There are two notable exceptions to this generalisation;ggobiandRGL.
• The major problem in producing good interactivecomponents is the lack of a general GUI structure.
• This in turn is because of a lack of framework forevent-handling and threading.
• An effort is being led by Luke Tierney to address thisproblem.
The Topography of Norfolk Island
Scientific Visualisation
• RGL is an indication that R shows some promise as anenvironment for scientific visualisation.
• In particular, the use of OpenGL permits the creation ofhigh quality visualisation tools.
• To make this truly useful we need a toolkit which willpermit us to mix visual components and controlwidgets.
• Ideally, this toolkit should be interactivelyprogrammable so that visualisation facilities can beassembled “on the fly”
Conclusions
• To date, R has not been a “serious” visualisationenvironment.
• It does however have more capabilities than manypeople know about.
• Over the next few years it is likely that R will develop amuch richer set of visualisation tools.
• The combination of a rich data analysis environmentwith powerful visualisation tools should make it aneven more productive environment.