3 views

Uploaded by Marius_2010

- integrated lesson plan 3 art
- Dmat 0093 Notes
- Julia Kuo-20 Ways to Draw a Dress and 44 Other Fabulous Fashions and Accessories a Sketchbook for Artists, Designers, And Doodlers-Quarry Books (2013)
- Proof of the Pythagorean Theorem using the Mean Proportional
- English for design
- geometry lesson 3
- Element and Principles of Design 1
- 10115.pdf
- 5bdf63c9-9951-4c78-9669-88f5bb860dbc.pdf
- Design Basics
- Foundations Proposal
- 8 Distributed Loads _part A
- Imaging and Design for Online Environment
- Everyday-structures-and-systems.pdf
- 30 Q on Equation of a Straight Line in Various Forms
- studentteachingthirdgradelessonthreedimensionalpaulklee
- Art Appreciation
- ACC. part1
- elementprinciples-110910084054-phpapp01
- math lesson plan

You are on page 1of 6

Its What You Not Draw

Drawing Paths with Holes in R Graphics

by Paul Murrell

port for drawing complex paths via the func-

tions polypath() and grid.path(). This article

explains what is meant by a complex path and

demonstrates the usefulness of complex paths

in drawing non-trivial shapes, logos, customised

data symbols, and maps.

Figure 1: A triangle drawn by the grid.path() func-

tion from a set of three ( x, y) locations.

One of the design goals of the R graphics system (R

Development Core Team, 2011) is to allow fine con- As for any basic shape, it is possible to control

trol over the small details of plots. One way that the the colour and thickness of the path border and the

R graphics system does this is by providing access colour used to fill the interior of the path.

to low-level generic graphics facilities, such as the We can also provide more than one set of ( x, y)

ability to draw basic shapes and the ability to control locations when drawing a path. The following code

apparently esoteric, but still useful, features of those provides an example, defining a new set of six lo-

shapes, such as the line end style used for drawing cations along with an id vector that can be used to

lines. break the locations into two groups of three.

In R version 2.12.0, another low-level graphics fa-

cility was added to R: the ability to draw complex

paths (not just polygons). > x <- c(.1, .5, .9,

This article describes this new facility and + .1, .2, .3)

presents some examples that show how complex > y <- c(.1, .8, .1,

paths might be useful. + .7, .6, .7)

> id <- rep(1:2, each=3)

Drawing paths with holes

The concept of a path is similar to the concept of a x y id

polygon: a path is defined by a series of ( x, y) loca- [1,] 0.1 0.1 1

tions that describe the boundary of the path. [2,] 0.5 0.8 1

For example, the following code defines a set of [3,] 0.9 0.1 1

[4,] 0.1 0.7 2

( x, y) locations that describe a simple triangle.

[5,] 0.2 0.6 2

[6,] 0.3 0.7 2

> x <- c(.1, .5, .9)

> y <- c(.1, .8, .1)

These locations can be used to describe a path

A triangle can be drawn from these locations us- that consists of two distinct triangles. The following

ing either the polypath() function from the graphics code draws such a path using grid.path(). The id

package or, as shown below and in Figure 1, using argument is used to identify distinct groups of loca-

the grid.path() function from the grid package. tions when using grid.path().1 Figure 2 shows the

result of drawing this path.

> library(grid)

> grid.path(x, y, id=id,

> grid.path(x, y, gp=gpar(fill="grey")) + gp=gpar(fill="grey"))

1 When using polypath(), NA values must be inserted between distinct groups of ( x, y) values.

C ONTRIBUTED A RTICLE 2

Fill rules

There are two ways to determine the interior of a

path like this. The default is called the non-zero

winding rule. This draws an imaginary straight line

and looks at where the straight line intersects the

boundary of the shape. A count is made of how

many times the boundary is running left-to-right at

the intersection and how many times the boundary is

running right-to-left; if the two counts are the same

then we are outside the shape and if the counts are

different, we are inside the shape.

Figure 2: Two triangles drawn by the grid.path()

To see this more clearly, Figure 4 uses lines with

function from a set of six ( x, y) locations broken into

arrows to show the directions on the boundaries of

two groups of three locations.

the path from Figure 3.

This output looks exactly the same as the out-

put we would get from drawing the two groups

of locations as polygons, using grid.polygon() or

polygon(), but conceptually there is a difference be-

cause the path treats the two groups of locations as

more clearly if we move the smaller triangle so that

it lies within the larger triangle (see Figure 3).

+ .4, .5, .6)

> y <- c(.1, .8, .1, Figure 4: The direction of the boundaries for the path

+ .5, .4, .5) in Figure 3.

> grid.path(x, y, id=id, The outer triangle boundary is clockwise and the

+ gp=gpar(fill="grey")) inner triangle boundary is anti-clockwise, so, using

the non-zero winding rule, the region within the in-

ner triangle is actually outside the path. A straight

line from inside the inner triangle to outside the

outer triangle intersects two boundaries, one going

right-to-left and one going left-to-right.

To further demonstrate this rule, the following

code defines a more complex path, this time consist-

ing of three triangles: one large clockwise triangle,

with two smaller triangles inside, one clockwise and

one anti-clockwise.

Figure 3: On the left is a path drawn by the + .4, .5, .6,

grid.path() function where the boundary of the + .4, .6, .5)

path consists of two distinct triangles (one within the > y <- c(.1, .8, .1,

+ .5, .4, .5,

other). The result is a single shape with a hole in it.

+ .3, .3, .2)

On the right is the result of drawing the two bound-

> id <- rep(1:3, each=3)

aries with the grid.polygon() function, which treats

the boundaries as two separate shapes. In this case,

the smaller triangle is drawn (filled in) on top of the Figure 5 shows a diagram of the boundary direc-

larger triangle. tions and the result of drawing this path. Because

the second smaller triangle is clockwise, the region

This example demonstrates that the two triangles inside that triangle is still part of the interior of the

together define a single shape, which is a triangular path, according to the non-zero winding rule.

region with a triangular hole in it. The interior of the

shapethe area that is shadeddoes not include the > grid.path(x, y, id=id,

region within the smaller triangle. + gp=gpar(fill="grey"))

C ONTRIBUTED A RTICLE 3

examples can be obtained from the online resources

for that accompany this article.2

A trivial observation is that complex paths allow

us to draw complex shapes. The triangle with trian-

gular holes from the previous section is an example

of a complex shape; it is not possible to describe this

shape as a simple polygon.

Another way that paths can be useful for draw-

ing complex shapes is that they allow us to combine

several simpler shapes to construct a more complex

Figure 5: A path where the boundary consists of whole. Figure 7 shows an example of this, where the

three triangles (two smaller ones within one larger overall shape shape has a very complex outline, but

one). The diagram on the left shows the direction of it can be constructed as a path simply by combining

the boundaries for the path. On the right, the path is circles and triangles.

drawn by the grid.path() function, with the interior

of the path determined using the non-zero winding

rule.

path is called the even-odd rule. This just draws an

imaginary straight line through the shape and counts

how many times the straight line crosses the bound-

ary of the shape. Each time a boundary is crossed,

we toggle between outside and inside the shape.

The following code draws the same path as in

Figure 5, but uses the even-odd rule to determine the

shapes interior. This time, the result is a larger trian-

Figure 7: A complex shape constructed from simple

gle with two smaller triangular holes punched out of

shapes combined together to make a path.

it (see Figure 6).

> grid.path(x, y, id=id, Figure 8 shows how this shape might be used

+ rule="evenodd", to dramatically highlight a point of interest within

+ gp=gpar(fill="grey")) a graph (in this case, to bring attention to the data for

the Ferrari Dino in the mtcars data set).

35

30

25

mpg

20

Figure 6: The path from Figure 5 drawn using the

even-odd fill rule.

15

The SVG language specification contains a nice

simple explanation and demonstration of these

10

fill rules; see http://www.w3.org/TR/SVG/painting.

html#FillRuleProperty. 100 200 300 400

disp

Applications

So what can these complex paths be used for? The Figure 8: A plot with a complex path used to high-

possibilities are endless, but this section describes a light a special point of interest.

2 http://www.stat.auckland.ac.nz/~paul/R/Paths/

C ONTRIBUTED A RTICLE 4

plex paths can be useful is if we are trying to draw

a shape that someone else has created. For example,

we might want to draw the logo of a company or an

organisation as a label on a plot.

sists of a single complex path, so we must be able to

draw such paths in order to render it correctly.

symbols on a lattice scatterplot of daily bus ridership

data.

900

800

700

bus/1000

logo. 600

500

background watermark for a lattice barchart (Sarkar,

300

2008).

n 01 n 02 n 03 n 04 n 05 n 06 n 07

Ja Ja Ja Ja Ja Ja Ja

Number of Citations per Year

date

2007

2006

Figure 12: A plot with the bus icon from Figure 11

2005

2004 used as a data symbol.

2003

2002 Another general area where complex paths arise

2001 is the drawing of maps. The outline of a countrys

2000 borders or coastline represents a set of ( x, y) coordi-

1999

1998

nates, often in a very complicated arrangement. One

situation where it can be useful to treat the map out-

0 500 1000 1500 2000 line as a path is the case where a country contains

a lake; the lake can be thought of as a hole in the

country shape. Things can get quite complicated if

the lake then contains an island, and the island has a

Figure 10: A plot with the GNU logo from Figure 9 lake, and so on. If the map is treated as a path to fill

as a background watermark. then all of these cases are dealt with quite easily.

Figure 13 shows a map of part of the South Is-

land of New Zealand. The lake in the lower right

quadrant of this map is Lake Te Anau and at the base

Another way that we might use external complex of one of the westerly spurs of this lake is an island.

shapes is as data symbols on a plot. Figure 11 shows This map outline has been drawn as a path with a

a bus icon. Again, this bus icon is a single path so it green fill colour used to indicate land area and an

must be drawn using grid.path() or polypath() in appropriate fill rule ensures that the lake is not filled

order for it to render correctly. in, but the island on the lake is.

C ONTRIBUTED A RTICLE 5

is constructed from the New Zealand coastline (mid-

dle), and then a bounding rectangle is added to the

path (right). This combined path allows us to fill a

region that is everything outside of the New Zealand

coastline and that can be drawn on top of the original

image to obscure those parts of the contour that are

not over land.

Caveats

The polypath() and grid.path() functions are

only supported on the pdf(), postscript(),

x11(type="cairo"), windows(), and quartz()

graphics devices (and associated raster formats).

These functions are not supported on

x11(type="Xlib"), xfig(), or pictex() and support

Figure 13: A map showing an island in a lake on an

is not guaranteed on graphics devices provided by

island.

extension packages.

Although R provides many low-level graphics fa-

cilities, such as the ability to draw complex paths,

there are still some basic tricks that it does not yet

Summary

support. One example is the ability to clip output to There are new functions, polypath() and

an arbitrary region on the page (it is currently only grid.path() for drawing complex paths, including

possible to clip to rectangular regions with R). paths with holes, in R graphics output. These func-

Sometimes, a missing feature like this can be tions can be useful for drawing non-trivial shapes,

worked around by making inventive use of the ex- logos, custom data symbols, and maps.

isting facilities. Figure 14 shows an example of this,

where a contour of earthquake events has been over-

laid on a map of New Zealand, but the contours are Acknowledgements

only visible over land areas.

The following material and data were used in this ar-

ticle:

The GNU logo was created by Aurelio A. Heck-

ert and is available from http://www.gnu.org/

graphics/heckert_gnu.html.

The bus icon was created by the Geographics

Unit, School of Environment, The University of

Auckland.

Both the GNU logo and the bus icon shape in-

formation were imported into R and drawn us-

ing the grImport package (Murrell, 2009).

The bus data were obtained from the

City of Chicago Data Portal http://

data.cityofchicago.org/Transportation/

CTA-Ridership-Daily-Boarding-Totals/.

The detailed map of the South Island of

New Zealand came from the Global Self-

consistent, Hierarchical, High-resolution

Shoreline Database (version 2.0; Wessel and

Figure 14: A map with an overlaid contour. A path

Smith, 1996) and was loaded into R using the

has been used to obscure the contour where it does

maptools package (Lewin-Koh and Bivand,

not overlap with land.

2011).

This result was achieved using complex paths The earthquake data came from the GeoNet

(see Figure 15). The starting point is the entire con- Project: http://www.geonet.org.nz/

C ONTRIBUTED A RTICLE 6

The New Zealand coastline information for 137, 2009. URL http://www.jstatsoft.org/v30/

Figures 14 and 15 came from the maps package i04/.

(Brownrigg and Minka, 2011).

R Development Core Team. R: A Language and Envi-

Many thanks also to the anonymous reviewers ronment for Statistical Computing. R Foundation for

who suggested several useful improvements to this Statistical Computing, Vienna, Austria, 2011. URL

article. http://www.R-project.org/. ISBN 3-900051-07-0.

R. Springer, New York, 2008. URL http://lmdvr.

Bibliography r-forge.r-project.org. ISBN 978-0-387-75968-5.

R. Brownrigg and T. P. Minka. maps: Draw Geographi- P. Wessel and W. H. F. Smith. A global self-consistent,

cal Maps, 2011. URL http://CRAN.R-project.org/ hierarchical, high-resolution shoreline database.

package=maps. R package version 2.1-6. Journal of Geophysical Research, pages 87418743,

1996.

N. J. Lewin-Koh and R. Bivand. maptools: Tools for

reading and handling spatial objects, 2011. URL http:

//CRAN.R-project.org/package=maptools. R Paul Murrell

package version 0.8-6. Department of Statistics

The University of Auckland

P. Murrell. Importing vector graphics: The grImport New Zealand

package for R. Journal of Statistical Software, 30(4): paul@stat.auckland.ac.nz

- integrated lesson plan 3 artUploaded byapi-394257560
- Dmat 0093 NotesUploaded byDeborah Tarpley Hicks
- Julia Kuo-20 Ways to Draw a Dress and 44 Other Fabulous Fashions and Accessories a Sketchbook for Artists, Designers, And Doodlers-Quarry Books (2013)Uploaded byreismarina
- Proof of the Pythagorean Theorem using the Mean ProportionalUploaded bywilburhoo
- English for designUploaded byJebemvammaturu
- geometry lesson 3Uploaded byapi-311466759
- Element and Principles of Design 1Uploaded byCollenne Kaye-Lie Garcia Uy
- 10115.pdfUploaded bySadiq Merchant
- 5bdf63c9-9951-4c78-9669-88f5bb860dbc.pdfUploaded byAvdhoot Gautam
- Design BasicsUploaded byCamille Cruz
- Foundations ProposalUploaded byamandalechner9859
- 8 Distributed Loads _part AUploaded byKarthik Krish
- Imaging and Design for Online EnvironmentUploaded byRichard Melvin Garcia
- Everyday-structures-and-systems.pdfUploaded byMoe yeik may
- 30 Q on Equation of a Straight Line in Various FormsUploaded bySujeendra R
- studentteachingthirdgradelessonthreedimensionalpaulkleeUploaded byapi-302306432
- Art AppreciationUploaded byHannah Micah Gelvero Gayanilo
- ACC. part1Uploaded byLevi John Baclig
- elementprinciples-110910084054-phpapp01Uploaded byAshish Singh
- math lesson planUploaded byapi-295655000
- symmetry lesson 5Uploaded byapi-253644500
- v2 CI2541 ChappellUploaded byAbdelkrim Jomaa
- Original Unit of InstructionUploaded bydawajo15
- 12-148-1-PB(1).pdfUploaded byetet100
- 07putnam4Uploaded byxristostsifakis
- Coordinate-Geometry-Doc.docxUploaded byJiaJia Lau
- 2010 Inter SchoolUploaded byjkfyeung
- tower paperUploaded byapi-352669460
- Lesson 22 Missing AnglesUploaded bymisterreid
- A Road Trafc Signal Recognition System Based on Template Matching Employing Tree ClassierUploaded bydknightghilli

- Advanced Cluster AnalysisUploaded byWole Ajala
- Tune ParetoUploaded byMarius_2010
- Metric LearningUploaded byMarius_2010
- Chap10 Anomaly DetectionUploaded bymunich_1502
- Survery of nearest neighborUploaded byGanesh Chandrasekaran
- curs2.pdfUploaded byMarius_2010
- Build KD TreeUploaded byMarius_2010
- KD Skinny FatUploaded byMarius_2010
- curs11Uploaded byMarius_2010
- Curs10 SiteUploaded byMarius_2010
- Rice Price PredictionUploaded byMarius_2010
- Non-param Classification BallTreeUploaded byMarius_2010
- A Comparison between Memetic algorithm and Genetic algorithm for the cryptanalysis of Simplified Data Encryption Standard AlgorithmUploaded byAIRCC - IJNSA
- PricesUploaded byMarius_2010
- xgboost-150831021017-lva1-app6891Uploaded byPushp Toshniwal
- BoostedTree.pdfUploaded bysleepyhollowinoz
- curs1siteUploaded byMarius_2010
- WNvanWieringen HDDA Lecture4 RidgeRegression 20162017Uploaded byMarius_2010
- r Mals ChainsUploaded byMarius_2010
- IntroMAs-advanced.pdfUploaded byMarius_2010
- curs7.pdfUploaded byMarius_2010
- PE_Tutorial.pdfUploaded byMarius_2010
- 4. hierarchical clustering.pdfUploaded byMarius_2010
- dtree18.pdfUploaded byMarius_2010
- seq-patUploaded byMarius_2010
- Chap1 IntroUploaded byTSMFORU

- Stellite 6Uploaded byRinjal Banerjee
- HouseholdUploaded byEnio Miguel Cano Lima
- (Lecture Notes in Computer Science _ Image Processing Computer Vision Pattern Recognition and Graphics 8668) Elena Barcucci, Andrea Frosini, Simone Rinaldi-Discrete Geometry for Computer Imagery_ 18thUploaded byquasemanobras
- Order Dismissing Petition for Writ of Mandamus.pdfUploaded byTim Nelson
- rstdUploaded bySonam Sethi
- Psg Governor With Aluminum CaseUploaded byNam Anh Trần
- Manual ArmaCADUploaded byIoan-Cosmin Scurtu
- Infineon BSC093N04LSG DS v02 01 EnUploaded byarafatasghar
- Filt RosUploaded byEnrique Ramirez Herrera
- OHS-Handbook-WorkforceXS-Monbulk.pdfUploaded byAlejandro Cea
- Rediff Tips 12-NOV-2010Uploaded byismailssk
- Economics MarketsUploaded bymocmocxvongvong
- Business StrategyUploaded byAnamika Sengupta
- IV Sem Mech_mt -II Lab ManualUploaded bySaravanan Mathi
- Enhancement of Iraqi Light Naphtha Octane Number Using Pt Supported HMOR Zeolite CatalystUploaded byMustafaJabbar
- Grey Owl Capital ManagementUploaded byZerohedge
- Kurita Cetamine BrochureUploaded byDanielWheeler
- Hiding in Plain Sight. Steganography andUploaded bymemx
- Geology Geophysics in Oil ExplorationUploaded byarasquare
- is.15708.2006 (1) brake padsUploaded byVenkatesh V
- Control Valves FekerUploaded byTayebASherif
- PortraitProfessional15.0 Win ManualUploaded bysppram
- OAB.docxUploaded bykrishna
- Mind on Statistics Ch14 QUploaded byglenlcy
- Saved RecsUploaded byaisha_mahmood
- WhatsApp MessengerUploaded byFabiano Andrade
- SP800-30-Rev1-ipdUploaded byMichael Williams
- Weld Office Training ManualUploaded byAnonymous AlhzFE9EVN
- How to Clean Up Your Label J DiazUploaded byrushmore01
- 2015 Global Food & Agriculture Investment OutlookUploaded byValoralAdvisors