46
Directions For R Graphics Development Ross Ihaka The R Foundation & University of Auckland [email protected]

Directions For R Graphics Development

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Directions For R Graphics Development

Directions For R Graphics Development

Ross Ihaka

The R Foundation & University of Auckland

[email protected]

Page 2: Directions For R Graphics Development

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.

Page 3: Directions For R Graphics Development

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.

Page 4: Directions For R Graphics Development

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.

Page 5: Directions For R Graphics Development

●●

●●

●●

●●

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)

Page 6: Directions For R Graphics Development

$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

Page 7: Directions For R Graphics Development
Page 8: Directions For R Graphics Development

$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

Page 9: Directions For R Graphics Development

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).

Page 10: Directions For R Graphics Development

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

Page 11: Directions For R Graphics Development

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")

Page 12: Directions For R Graphics Development

5

10

15

20Jan

Feb

Mar

Apr

May

JunJul

Aug

Sep

Oct

Nov

Dec

Average Monthly Temperatures in London

Page 13: Directions For R Graphics Development

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))

Page 14: Directions For R Graphics Development

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.

Page 15: Directions For R Graphics Development

0 10 20 30 40

Lamb

Mutton

Pigmeat

Poultry

Beef

New Zealand Meat Consumption by Category

Percentage in Category

Page 16: Directions For R Graphics Development

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")

Page 17: Directions For R Graphics Development

0 5 10 15 20 25 30

0

2

4

6

8

●●

●●

●● ●

●●

Jazzing Up R Graphics

Line colour variation.

Page 18: Directions For R Graphics Development

0 5 10 15 20 25 30

0

2

4

6

8

●●

●●

●● ●

●●

●●

●●

●● ●

●●

Jazzing Up R Graphics

Line colour variation and shadows.

Page 19: Directions For R Graphics Development

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")

Page 20: Directions For R Graphics Development

1960 1970 1980 1990 2000

−0.2

0.0

0.2

0.4

0.6

Annual Global Temperature Increase (°C)

Time

Page 21: Directions For R Graphics Development

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)

Page 22: Directions For R Graphics Development

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

Page 23: Directions For R Graphics Development

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.

Page 24: Directions For R Graphics Development

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.

Page 25: Directions For R Graphics Development

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.

Page 26: Directions For R Graphics Development

Cross Hatching and Dot Shading

Cross-hatching and dot shading provide an alternative tofilling polygons with solid colours.

Page 27: Directions For R Graphics Development

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.

Page 28: Directions For R Graphics Development

Logical Operations on Polygons

Intersection Set Difference

Page 29: Directions For R Graphics Development

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.

Page 30: Directions For R Graphics Development

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.

Page 31: Directions For R Graphics Development

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.

Page 32: Directions For R Graphics Development

An HSV Colour Wheel

Page 33: Directions For R Graphics Development

A CIELAB Colour Wheel

Page 34: Directions For R Graphics Development

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

Page 35: Directions For R Graphics Development

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

Page 36: Directions For R Graphics Development

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

Page 37: Directions For R Graphics Development

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

Page 38: Directions For R Graphics Development

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

Page 39: Directions For R Graphics Development

Three Dimensional Graphics

Page 40: Directions For R Graphics Development

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.

Page 41: Directions For R Graphics Development

Realistic and Abstract Rendering Techniques

Page 42: Directions For R Graphics Development

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.

Page 43: Directions For R Graphics Development

The Topography of Norfolk Island

Page 44: Directions For R Graphics Development

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”

Page 45: Directions For R Graphics Development
Page 46: Directions For R Graphics Development

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.