importing graphics for statistical plots

25
Importing Graphics for Statistical Plots Paul Murrell August 10 2006

Upload: buinhu

Post on 02-Jan-2017

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Importing Graphics for Statistical Plots

Importing Graphics for Statistical Plots

Paul Murrell

August 10 2006

Page 2: Importing Graphics for Statistical Plots

Introduction

Old hat• A statistical graphics system is most commonly used as only a

producer of graphical images.

R

WMF[file]

PNG[file]

Powerpoint

Gimp

Web

Page 3: Importing Graphics for Statistical Plots

Introduction

New hat• It is also useful to make the statistical graphics system a

consumer of graphical images.

PNG[file]

PostScript[file]

R

WMF[file]

PNG[file]

Powerpoint

Gimp

Web

Page 4: Importing Graphics for Statistical Plots

Motivation

Why import graphics?

• Adding company/institution logos

● ●

●●

● ●

● ●

0.0

0.5

1.0

1.5

2.0

Jan Mar May Jul Aug Oct Dec

The Level of Interest in R

1996

Page 5: Importing Graphics for Statistical Plots

Motivation

Why import graphics?

• Backgrounds/watermarks

1993 1996 1998 2001

0

50

100

150

200

250

Estimated Population (max.) of Bengal Tigers(in Bhutan)

Page 6: Importing Graphics for Statistical Plots

Motivation

Why import graphics?

• Custom plotting symbols

20 40 60 80

Match C51 Evans Gambit

07 C51 Evans Gambit

07 C51 Evans Gambit

London C51 Evans Gambit

London D20 Queen's Gambit Accepted

London m 18 C33 King's Gambit Accepted

London D20 Queen's Gambit Accepted

London C53 Giuoco Piano

18 B30 Sicilian

London D20 Queen's Gambit Accepted

03 D20 Queen's Gambit Accepted

04 D20 Queen's Gambit Accepted

13 C51 Evans Gambit

London m1 C23 Bishop's Opening

03 C51 Evans Gambit

11 C51 Evans Gambit

12 C33 King's Gambit Accepted

London B21 Sicilian, 2.f4 and 2.d4

06 B21 Sicilian, 2.f4 and 2.d4

London A03 Bird's Opening

08 C38 King's Gambit Accepted

09 C38 King's Gambit Accepted

number of moves in game

Opening Gambits of Louis Charles Mahe De La Bourdonnais

Page 7: Importing Graphics for Statistical Plots

Motivation

Why import graphics?

• Chart Junk!

1993 1996 1998 2001

0

50

100

150

200

250

Estimated Population (max.) of Bengal Tigers(in Bhutan)

Page 8: Importing Graphics for Statistical Plots

Problem Statement

Importing graphics with R

• The pixmap package already provides facilities for importingbitmap images.

• We would like a mechanism for importing vector images.• vector images scale• for some images, the output will be much smaller than for a

bitmap

Page 9: Importing Graphics for Statistical Plots

Solution Statement

Target only PostScript

• There are many (free) tools for converting to PostScript fromother formats: ghostscript and xpdf (PDF), xfig (FIG),InkScape (SVG).

• PostScript is a sophisticated graphics language, so verycomplex images can be represented

• There is an open source interpreter for PostScript(ghostscript) so we do not have to write an interpreter

• PostScript is a programming language so we can write aPostScript program to export other PostScript images

PDFFIGSVG

InkScape

ghostscript

PostScript[file]

Page 10: Importing Graphics for Statistical Plots

Solution Statement

Convert to intermediate XML format• XML is plain text, but with a discoverable structure, and a

natural support for storing hierarchical information, etc ...

• XML can be read by R

• XML can be read by other software

• XML can be produced by other software

• XML can be converted to other formats (e.g., SVG)

PDFFIGSVG

InkScape

ghostscript

PostScript[file]

PostScriptTrace()

ghostscript

RGML[file]

Page 11: Importing Graphics for Statistical Plots

Solution Statement

Read into general R object

• We may want to draw the picture using grid or usingtraditional graphics

• The image information is data; we may want to transform theimage before drawing it, or we may want to just analyse theimage

• These general R objects can be created from informationother than imported PostScript files

PDFFIGSVG

InkScape

ghostscript

PostScript[file]

PostScriptTrace()

ghostscript

RGML[file]

readPicture() "Picture"[R object]

Page 12: Importing Graphics for Statistical Plots

Solution Statement

The grImport package makes it possible to import externalPostScript images for use within an R plot.

PostScript[file]

PostScriptTrace()

ghostscript

RGML[file]

readPicture() "Picture"[R object]

grid.picture()

grid.symbols()

Page 13: Importing Graphics for Statistical Plots

Examples

%!PS-Adobe-2.0 EPSF-1.2

%%Creator: Adobe Illustrator(TM)

%%For: OpenWindows Version 2

%%Title: tiger.eps

...

.8 setgray

clippath fill

-110 -300 translate

1.1 dup scale

0 g

0 G

0 i

0 J

0 j

0.172 w

10 M

[]0 d

0 0 0 0 k

...

Page 14: Importing Graphics for Statistical Plots

Examples

PostScriptTrace("tiger.ps")

tiger <-readPicture("tiger.ps.xml")

pushViewport(plotViewport())grid.picture(tiger)...

1993 1996 1998 2001

0

50

100

150

200

250

Estimated Population (max.) of Bengal Tigers(in Bhutan)

Page 15: Importing Graphics for Statistical Plots

Examples

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG"

"http://www.w3.org/TR/2001/REC-SVG...">

<!-- Created with Sodipodi -->

<svg version="1.0">

...

<g

style="font-size:12;"

id="g874">

<path

d="M 0 437 L 437 0 "

style="fill:none;fill-opacity:1"

id="path616" />

...

# Convert SVG to PostScript# using InkScape

PostScriptTrace("chess.ps")

chess <-readPicture("chess.ps.xml")

Page 16: Importing Graphics for Statistical Plots

Examples

The picturePaths() function draws individual paths from apicture, which makes it possible to identify elements of a picture.

"Picture" objects can be subsetted, which makes it possible toextract elements of a picture.

picturePaths(chess[125:136])

Page 17: Importing Graphics for Statistical Plots

Examples

pawn <-chess[205:206]

grid.symbols(pawn,x, y)

20 40 60 80

Match C51 Evans Gambit

07 C51 Evans Gambit

07 C51 Evans Gambit

London C51 Evans Gambit

London D20 Queen's Gambit Accepted

London m 18 C33 King's Gambit Accepted

London D20 Queen's Gambit Accepted

London C53 Giuoco Piano

18 B30 Sicilian

London D20 Queen's Gambit Accepted

03 D20 Queen's Gambit Accepted

04 D20 Queen's Gambit Accepted

13 C51 Evans Gambit

London m1 C23 Bishop's Opening

03 C51 Evans Gambit

11 C51 Evans Gambit

12 C33 King's Gambit Accepted

London B21 Sicilian, 2.f4 and 2.d4

06 B21 Sicilian, 2.f4 and 2.d4

London A03 Bird's Opening

08 C38 King's Gambit Accepted

09 C38 King's Gambit Accepted

number of moves in game

Opening Gambits of Louis Charles Mahe De La Bourdonnais

Page 18: Importing Graphics for Statistical Plots

Examples

hourglass <-

new("Picture",

paths=

list(new("PictureFill",

x=c(0, 1, 0, 1),

y=c(0, 0, 1, 1),

rgb="black")),

summary=

new("PictureSummary",

numPaths=1,

xscale=c(0, 1),

yscale=c(0, 1)))

grid.symbols(hourglass, x, y)

yield

Svansota

No. 462

Manchuria

No. 475

Velvet

Peatland

Glabron

No. 457

Wisconsin No. 38

Trebi

20 30 40 50 60

1932

Svansota

No. 462

Manchuria

No. 475

Velvet

Peatland

Glabron

No. 457

Wisconsin No. 38

Trebi

1931

Page 19: Importing Graphics for Statistical Plots

Issues

R graphics is primitive

• PostScript can do things that R graphics cannot• R only supports filling regions using the“winding rule”

(PostScript also supports“even-odd”filling)• R does not support general paths (e.g., disjoint paths)• R does not support general clipping regions

• Other vector formats can do things that PostScript cannot• PDF and SVG support transparency, image composition

operators, ...• Conversions may appear to work, but you might end up with a

bitmap in your PostScript file

• It is not easy/possible to export all of every PostScript file• It is possible to export PostScript text as a path, but it is not

ideal, and in most cases probably illegal• It is unclear how to export a bitmap from a PostScript image

Page 20: Importing Graphics for Statistical Plots

Issues

A Picture object is data

The R object representing an external vector image can be treatedlike any other data source; it can be transformed, subsetted,augmented ... even repaired.

PRD <- readPicture("PRD_XUS_s_red.eps.xml")grid.picture(PRD)

Page 21: Importing Graphics for Statistical Plots

Issues

Page 22: Importing Graphics for Statistical Plots

Issues

Healing the logo

The single complex path can be processed to produce multipledisjoint paths.

newPRD <- explodePaths(PRD)grid.picture(newPRD)

Page 23: Importing Graphics for Statistical Plots

Issues

Healing the logo

The new disjoint paths can be reordered.

grid.picture(newPRD)grid.picture(newPRD[c(264, 268, 278:279, 284)], ...)grid.picture(newPRD[180], ...)

Page 24: Importing Graphics for Statistical Plots

Conclusions

Disclaimer

The grImport package is not a licence to violate copyright licences!

Summary

• It is useful to be able to import vector images for use instatistical plots.

• PostScript is a good external graphics format to target.

• An intermediate XML format allows alterative sources anddestinations for graphics.

• An intermediate R object allows the picture to be treated as“just”data.

• There are limits to what R graphics can do, but with a bit ofimagination there are some reasonable workarounds.

Page 25: Importing Graphics for Statistical Plots

Acknowledgements

• Richard Walton made significant improvements to the grImport code last(Southern) Summer.

• The conversion of PostScript files is performed using ghostscripthttp://www.cs.wisc.edu/~ghost/

• The tiger image is part of the ghostscript distribution; the tiger data are fromhttp://www.globaltiger.org/population.htm.

• The greyscale version of the tiger used the colorspace package by Ross Ihaka.

• The chess board image (by Jose Hevia) is from the Open Clip Art Libraryhttp://openclipart.org/clipart//recreation/games/chess/chess_game_01.svg

• The chess data are from chessgames.comhttp://www.chessgames.com/perl/chess.pl?page=1&pid=31596

• The man/woman picture is from the Cisco Network Topology Iconshttp://www.cisco.com/web/about/ac50/ac47/2.html

• The“problem logo” is used with kind permission from J&JPRD