survey of r graphics tools - bwmtechblog.net · survey of r graphics tools bruce moore, d. eng....

53
Survey of R Graphics Tools Bruce Moore, D. Eng. Moore Software Services, LLC P.O. Box 183 Coppell, TX 75019 (972) 652-0254 [email protected] www.mooresoftwareservices.com Simplifying Financial Data Analysis February 22, 2014

Upload: others

Post on 06-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of R Graphics Tools

Bruce Moore, D. Eng.

Moore Software Services, LLCP.O. Box 183

Coppell, TX 75019(972) 652-0254

[email protected]

www.mooresoftwareservices.com

S i m p l i f y i n g F i n a n c i a l D at a A n a l y s i s

February 22, 2014

Page 2: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Graphics tools for web, publication andvisualization

Complex graph that inspired presentation

Try to give a guide on when to use which tool

Gnuplot

R plot()

R ggplot2()

R lattice()

R rgl()

ImageMagick utility

qrencode utility

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 3: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

The complex graph that inspired this presentation

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 4: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Gnuplot is an old and easy standby that can beused stand-alone or embedded

Gnuplot is embedded in other or has interface to otheropen-source projects

Octave

Maxima

Several file output options

#set terminal postscript;

set terminal pdf;

#set terminal jpg;

set output "images/r_graphics_survey_gnuplot_line.pdf"

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 5: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Gnuplot is handy if you are just plotting functions

o(x)=0;

f(x)=100-10*x;

p(x)=x/12*f(x);

#

# For parametric 3d plots

#

z(u,v)=p(u)+pg(v);

z(x,y)=p(x)+pg(y);

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 6: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generating a simple plot is very compact

set output "images/r_graphics_survey_gnuplot_line.pdf"

set xlabel "Price"

set ylabel "Unit Sales"

plot [x=0:10] f(x) \

"Demand for Product A" linecolor rgb "red";

0

20

40

60

80

100

0 2 4 6 8 10

Unit

Sale

s

Price

Demand for Product A

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 7: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generating an annotated chart with fitted data isstill compact

set title "Maximum Profit vs. Sales"

set label "Max profit occurs \

at A=50, B=100" at 25,41.67

set label "Profit for A<=50, B<=40" at 92,34.16

profit(x)=a*x**3 + b*x**2 + c*x + d

fit profit(x) "data/r_graphics_survey_gnuplot.dat" \

using 1:2 via a,b,c,d

plot "data/r_graphics_survey_gnuplot.dat" \

notitle,"data/r_graphics_survey_gnuplot_2.dat" \

notitle,profit(x) with lines \

linecolor rgb "blue" title "Maximum profit";

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 8: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generating an annotated chart with fitted data isstill compact

0

5

10

15

20

25

30

35

40

45

0 50 100 150 200 250

Pro

fit

Sales

Maximum Profit vs. Sales

Max profit occurs at A=50, B=100

Profit for A<=50, B<=40

Maximum profit

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 9: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Gnuplot is unusual in ability to do 3d plot withsuperimposed contour lines

set samp 200;

set iso 200;

set pm3d;

set palette model RGB;

set palette defined;

set key off;

set grid layerdefault;

set hidden3d;

set label 1 "";

splot [x=0:10] [y=0:5] z(x,y) title combTitle \

with pm3d;

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 10: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Gnuplot is unusual in ability to do 3d plot withsuperimposed contour lines

0 2

4 6

8 10

Product APrice

0 1

2 3

4 5

Product BPrice

0 5

10 15 20 25 30 35 40 45

Revenue($)

0 5 10 15 20 25 30 35 40 45

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 11: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Unfortunately, things break down when you add aplane to represent a sales constraint

Vertical plane shows sales constraint.

0 2

4 6

8 10

Product APrice

0 1

2 3

4 5

Product BPrice

0

10

20

30

40

50

Revenue

($)

0 5 10 15 20 25 30 35 40 45 50

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 12: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

This was the best I could come up with in Gnuplot

Vertical plane shows sales constraint.

0 2

4 6

8 10

Product APrice

0 1

2 3

4 5

Product BPrice

0

10

20

30

40

50

Revenue

($)

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 13: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

R plot is oldest of R graphic tools

It is frequently defined as a method for a model object

Trivial to generate a simple graphVery useful for diagnostic graphs

Simple and relatively inflexible

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 14: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

First, pull down some swap interest rate data fromFRED2

require(fImport)

require(reshape2)

swpTs<-fredSeries(c("MSWP1"),nDaysBack=365*10

,to=Sys.timeDate())

swpTs$dateVal = as.Date(rownames(swpTs))

swpDf <- data.frame(swpTs)

swpDf$dateVal <- as.character(swpDf$dateVal)

swpDf <- melt(swpDf)

swpDf$dateVal <- as.Date(swpDf$dateVal)

attach(swpDf)

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 15: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Save a scatter plot to a PDF file

pdf("images/r_graphics_survey_plot_scatter.pdf")

#jpeg("images/r_graphics_survey_plot_scatter.jpg")

#postscript("r_graphics_survey_plot_scatter.eps")

jpg

plot(value~dateVal)

dev.off()

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 16: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Save a scatter plot to a PDF file

●●●●●

●●●●

●●

●●●

●●●●

●●

●●●

●●●●

●●●●●

●●

●●

●●●●

●●

●●

●●

●●●●●●●

●●

●●●

●●●●●●●●●●●●

●●●

●●●●●●

●●●●●●●●●●●●●●●●●●●

2004 2006 2008 2010 2012 2014

12

34

5

dateVal

valu

e

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 17: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Add greek letters

plot(value~dateVal

,main=expression(

paste("You can include greek letters "

,sigma(phi))))

●●●●●

●●●●

●●

●●●

●●●●

●●

●●●

●●●●

●●●●●

●●

●●

●●●●

●●

●●

●●

●●●●●●●

●●

●●●

●●●●●●●●●●●●

●●●

●●●●●●

●●●●●●●●●●●●●●●●●●●

2004 2006 2008 2010 2012 2014

12

34

5

You can include greek letters σ(φ)

dateVal

valu

eSurvey of R Graphics Tools Bruce Moore, D. Eng.

Page 18: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

plot() is defined as a method for many objectclasses, and used for diagnostic plots

simpleLm <- lm(value~dateVal)

plot(simpleLm)

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 19: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

lm() and others implement residual plots

pdf("images/r_graphics_survey_plot_residual_1.pdf")

plot(simpleLm,which=1)

dev.off()

pdf("images/r_graphics_survey_plot_residual_2.pdf")

plot(simpleLm,which=2)

dev.off()

pdf("images/r_graphics_survey_plot_residual_3.pdf")

plot(simpleLm,which=3)

dev.off()

pdf("images/r_graphics_survey_plot_residual_4.pdf")

plot(simpleLm,which=4)

dev.off()

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 20: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

lm(), glm() and others implement residual plots

0 1 2 3 4

−4

−3

−2

−1

01

23

Fitted values

Res

idua

ls

●●●●

●●●

●●

●●●

●●

●●

●●

●●●●●●●

●●

●●●

●●

●●

●●●●

●●●

●●●●

●●●

●●●●●

●●●

●●●●

●●●●●●●●●

●●

●●●●●●

●●●●●●●●●●●●●

●●●●●●●●

lm(value ~ dateVal)

Residuals vs Fitted

1

2

3

● ●●●

●●●

●●

●●●

●●

●●

●●

●●●●●

● ●●

●●

●●

●●

●●

●●●●

●●

● ●● ●

●●●

●●●●

●●●

●●●●

●●●●●●●●●

●●

●●●●●●●●●●●● ●●●

●●●●

●●●●●●

●●

−2 −1 0 1 2

−2

−1

01

2

Theoretical Quantiles

Sta

ndar

dize

d re

sidu

als

lm(value ~ dateVal)

Normal Q−Q

1

2

3

0 1 2 3 4

0.0

0.5

1.0

1.5

Fitted values

Sta

ndar

dize

d re

sidu

als

●●●

●●

●●

●●

●●

●●●●●

●●●

●●

●●

●●

●●

●●●

●●

●●

●●

●●

●●●●

●●

●●●

●●●●●●

●●

●●

●●

●●

●●

●●●●

●●

lm(value ~ dateVal)

Scale−Location

1

2

3

0 20 40 60 80 100 120

0.00

0.02

0.04

0.06

0.08

0.10

0.12

0.14

Obs. number

Coo

k's

dist

ance

lm(value ~ dateVal)

Cook's distance

1

2

3

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 21: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

plot() can also be used to simple descriptive plot ofvariables

plot(swpDf)

dateVal

0.6 0.8 1.0 1.2 1.4

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

1250

013

500

1450

015

500

● ● ● ●●●●● ●●●● ●●●

● ●●● ● ●●●●●●●●●●●●●

●●●●●●●●●●●●●●●● ●● ●●●●

●●●●●●●●●●●●●●●●●●●

●●●●●●●●●●●●

●●●●●●

●●●●●●●●●

●●●●●●●●●●●●●●●●●●

0.6

0.8

1.0

1.2

1.4

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●● variable ● ● ● ●●●●● ●●●● ●●●● ●●● ● ●●● ●●●●●●●●●●●●●●●●●●●●●●●●●● ●● ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

12500 13500 14500 15500

●●●●●

●●●●

●●●●

●●●

●●●●

●●●●●●●●●●●

●●●●●●●

●●●

●●

●●

●●●●

●●●●●●●●●●●●●●●●

●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●

●●●●

●●●●

●●●

●●●●●●●●●●●●●●●●●●●●●●

●●●

●●

●●

●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

1 2 3 4 5

12

34

5

value

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 22: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

qplot() generates exploratory plots quickly

qplot(dateVal,value,swpDf)

●●

●●●●●●

●●●●●●●●

●●●●●●●

●●●●●●●●●●●●●●●●●●●●●

●●

●●

●●●●●●●

●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

12345

2004 2006 2008 2010 2012 2014dateVal

Inte

rest

_Rat

e

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 23: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Using color and shape for each of the four swaprates is easy

qplot(dateVal,Interest_Rate,data=swpDf

,colour=FRED2_Series)

●●●●●●●●●●

●●●●●●●

●●●●●●●●●●

●●●●●●●●●●

●●●●●

●●

●●●●●●●●

●●●

●●●●●●

●●●

●●●●●●●●●●

●●●●●

●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●●●

●●●●●●●

●●●●●●●●●●

●●●●●●●

●●●●●

●●●

●●

●●●●●●●●

●●

●●●●●●

●●●

●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●

●●●●●●●

●●●●●●●●●●●●●

●●●●●

●●●

●●

●●●●●●●●●

●●●●●●

●●●●●●●●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

●●●●●●●●

●●●●●●●●

●●●●●●●

●●●●●●●●●●●●●●●●●●●●●

●●

●●●●

●●●●●

●●●●●●

●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

12345

2004 2006 2008 2010 2012 2014dateVal

Inte

rest

_Rat

e FRED2_Series

MSWP5

MSWP3

MSWP2

MSWP1

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 24: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

To add titles and work on a slightly more complexplot

swpIntPlot <- ggplot(data=swpDf

,aes(swpDf$dateVal

,swpDf$Interest_Rate

,group=swpDf$FRED2_Series

,color=swpDf$FRED2_Series))

+ geom_line()

swpIntPlot + ggtitle("Swap Rates")

+ xlab("Date") + ylab("Rate")

+ labs(colour="Swap Series")

+ scale_x_date()

12345

2004 2006 2008 2010 2012 2014Date

Rat

e

Swap Series

MSWP5

MSWP3

MSWP2

MSWP1

Swap Rates

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 25: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

In ggplot2, it is easy to change the file type,resolution and aspect ratio to fit the publication use

ggsave("images/r_graphics_survey_ggplot2_simple.pdf"

,width=7

,height=1.8

,dpi=100)

12345

2004 2006 2008 2010 2012 2014Date

Rat

e

Swap Series

MSWP5

MSWP3

MSWP2

MSWP1

Swap Rates

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 26: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Annotation requires getting the data type right forthe ”date” scale...easiest to use the range function

xrng=range(swpDf$dateVal)

xrng[2]=as.Date("2012-01-01")

yrng=range(swpDf$Interest_Rate)

swpIntPlot +

annotate("text",x=xrng[2],y=yrng[2]-1

,label="The term spread\nnarrowed

\nduring the recession.")

The term spread

narrowed

during the recession.12345

2004 2006 2008 2010 2012 2014Date

Rat

e

Swap Series

MSWP5

MSWP3

MSWP2

MSWP1

Swap Rates

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 27: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Greek symbols and other notes

ggplot2 will not do 3d

For greek symbols, see Hadley Wickham’s wiki: https:

//github.com/hadley/ggplot2/wiki/Plotmath

The paper ggplot2 book from Springer is out of date, butstill useful.

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 28: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Lattice was the second graphics subsystem in R

Intended to allow more complex graphics

Not tied to the legacy of beginning a method to all of themodel objects

Introduced trellis graphics

Introduced 3d graphics

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 29: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Lattice will easily generate a 3d graphic withexcellent fonts

wireframe(total_profit~interest_rateA*interest_rateB

,multiDf

,shade=TRUE

,col.groups=c("red","lightGreen_pms368C")

,group=plane

,scales=list(z.ticks=5,arrows=FALSE

,font=10,tck=1.0)

,screen=list(z=40,x=-75,y=0)

,xlab=paste(labelA,"\nPrice")

,ylab=paste(labelB,"\nPrice")

,zlab="Total\nProfit"

,xlim=c(0,10)

,ylim=c(0,5)

,zlim=c(0,50))Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 30: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Lattice will easily generate a beautiful 3d chart, butI couldn’t get it to do intersecting planes

02

46

810

01

23

45

0

10

20

30

40

50

Label A Price

Label B Price

TotalProfit

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 31: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

There is MUCH more to lattice

Lattice is the only package that does publication-oriented3d

Rich in data exploration functions

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 32: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Finally...the complex graph that inspired thispresentation

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 33: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

rgl is intended for interactive graphics...notpublication graphics

Interactive visualization

Only output is low quality PNG and EPS

Can generate a HTML/Javascript that can be accessedlocally from PowerPoint or embedded in web page

Can generate animated GIF for web or local use

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 34: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Set up environment

require(rgl)

r3dDefaults$windowRect <- c(0,50, 1024, 1024)

See package documentation for complete information.http://cran.r-project.org/web/packages/rgl/rgl.pdf

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 35: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generate data for parabola

varAdf<-data.frame(priceA=c(seq(1,10,0.1)),salesA=0

,revenueA=0)

varAdf$salesA <- 100 - 10*varAdf$priceA;

varAdf$revenueA <- varAdf$priceA/12*varAdf$salesA;

varBdf<-data.frame(priceB=c(seq(1,10,0.1)),salesB=0

,revenueB=0)

varBdf$salesB <- 200 - 40*varBdf$priceB;

varBdf$revenueB <- varBdf$priceB/12*varBdf$salesB;

multiDf <- merge(varAdf,varBdf,all=TRUE)

multiDf$total_revenue <- multiDf$revenueA

+ multiDf$revenueB

multiDf$plane <- "revenue"Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 36: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generate table for color gradation

glDf <- multiDf[multiDf$priceB <= 5.0 &

multiDf$total_revenue >= 0 &

multiDf$plane=="revenue",]

#

# Create an interpolated dataframe

#

interpDf <- interp(glDf$priceA

,glDf$priceB

,glDf$total_revenue)

zcolorLUT <- rainbow(max(interpDf$z + 1))

zcolor <- zcolorLUT[interpDf$z]

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 37: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generate the parabola

persp3d(interpDf$x,interpDf$y,interpDf$z

,col=zcolor

,alpha=0.75 # make transparent

,axes=FALSE

,xlab=""

,ylab=""

,zlab="")

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 38: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

With mouse, adjust perspective...save/load

loadView <- dget("src/R/savedRglViewpoint")

par3d(loadView)

#

#

saveView <- par3d(c("userMatrix", "zoom", "FOV"));

dput(saveView,file="src/R/savedRglViewpoint")

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 39: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Code to generate a plane

constraintA <- -factorA/10 + 10

z <- matrix(seq(0,max(interpDf$z)

,len=length(interpDf$x))

,length(interpDf$x)

,length(interpDf$y))

x <- matrix(constraintA

,length(interpDf$x)

,length(interpDf$y))

y <- t(matrix(seq(0,max(interpDf$y)

,len=length(interpDf$y))

,length(interpDf$x)

,length(interpDf$y)))

persp3d(x,y,z,add=TRUE,alpha=0.5)Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 40: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Generate titles

title3d(main=mainTitle,pos=c(2, 3, 42)

, color="black")

title3d(xlab=labelA, pos=c(3, 6, 0)

, color="black")

title3d(ylab=labelB, pos=c(10, 3, 0)

, color="black")

title3d(zlab=labelC, pos=c(0, 5, 20)

, color="black")

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 41: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Save the output

rgl.snapshot("r_graphics_survey_rgl_example.png"

,fmt="png")

rgl.postscript("r_graphics_survey_rgl_example.eps")

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 42: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Code to generate a movie

Function spin3d defaults to 5 RPM...12 seconds for a completerotation

movie3d(spin3d(),duration=12)

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 43: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Code to generate a web page that can be accessedfrom web or PowerPoint

writeWebGL(dir="images/webGL"

,filename=file.path("images/webGL"

,"index.html"))

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 44: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Imagemagick is useful converting graphics files

A bash script within R

> system("for FILE in images/*.pdf;

do convert ${FILE%.*}.pdf ${FILE%.*}.eps;

done;")

Convert a vCard from EPS that printer needs to PDF thatTexMaker needs for this presentation

$ convert Bruce_W_Moore.eps Bruce_W_Moore.pdf

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 45: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Imagemagick can also create an animated GIF file

convert -delay 300 -loop 0 -morph 5

../../images/r_graphics_survey_ggplot2_qplot*.jpg

../../images/r_graphics_survey_animated.gif

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 46: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Imagemagick can also join images in a montage forcomparison

montage -background blue -geometry +6+6

../../images/r_graphics_survey_ggplot2_qplot.jpg

../../images/r_graphics_survey_ggplot2_qplot_color.jpg

../../images/r_graphics_survey_montage.pdf

If working with EPS, there may be other tools available.

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 47: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

QR codes are handy for business cards, posters etc.

QR codes have many uses

vCards on back of business cardsURLs in brochures, ads, posters

qrencode is a good command line package

Part of most distributionsLatest includes EPS supporthttp://fukuchi.org/works/qrencode/

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 48: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

vCard generates very dense QR code

iPhone 5s will read QR code from 724 byte vCard

967 byte vCard fails to read

-s Size of pixel

-d Resolution of image

-t Output file type

qrencode -s 6 -d 600 -t EPS -o Bruce_W_Moore.eps

< Bruce_W_Moore.vcf

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 49: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

URL generates sparse QR code

Read data from command line

qrencode -o link.png

http://www.mooresoftwareservices.com/images/documents/

r_graphics_survey_2014_02_22.pdf

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 50: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Summary of key features

Gnuplot R plot ggplot2 lattice rgl

functions YInteractive y1 Y3D Y Y YFonts (easy) Y Y Y YPDF Y Y Y YEPS Y Y Y Y y2

PNG Y Y Y Y Yparallel axes Y Ymapping y y Y Y Y

1Limited to identifying observation ID2rgl has EPS support, but it is not robust.

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 51: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Contact Info

Bruce Moore(972) 652-0254

[email protected]

https://mooresoftwareservices.com

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 52: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Presentation URL

https://mooresoftwareservices.com/images/

documents/r_graphics_survey_2014_02_22.pdf

Survey of R Graphics Tools Bruce Moore, D. Eng.

Page 53: Survey of R Graphics Tools - bwmtechblog.net · Survey of R Graphics Tools Bruce Moore, D. Eng. Introduction Gnuplot R plot R ggplot2 R lattice R rgl ImageMagick qrencode Summary

Survey of RGraphicsTools

Bruce Moore,D. Eng.

Introduction

Gnuplot

R plot

R ggplot2

R lattice

R rgl

ImageMagick

qrencode

Summary

Google+ Profile for Author Linking

by Bruce Moore

Survey of R Graphics Tools Bruce Moore, D. Eng.