Lattice and Other Graphics in R J H Maindonald Centre for Mathematics and Its applications Australian National University. J.H.Maindonald 2009.Permission is given to make copies for personal study and class use. Apr14,2009 Languages shape the way we think,and determine what we can think about. Benjamin Whorf. S has forever altered the way people analyze,visualize,and manipulate data...S is an elegant,widely accepted,and enduring software system,with conceptual integrity,thanks to the insight,taste,and effort of John Chambers. [From the citation for the 1998 Association for Computing Machinery Software award.]
Lattice and Other Graphics in R J H Maindonald Centre for Mathematics and Its Applications Australian National University. c J. H. Maindonald 2009. Permission is given to make copies for personal study and class use. April 4, 2009 Languages shape the way we think, and determine what we can think about. [Benjamin Whorf.] S has forever altered the way people analyze, visualize, and manipulate data... S is an elegant, widely accepted, and enduring software system, with conceptual integrity, thanks to the insight, taste, and effort of John Chambers. [From the citation for the 1998 Association for Computing Machinery Software award.]
2 John H.Maindonald,Centre for Mathematics Its Applications,Mathematical Sciences Institute, Australian National University,Canberra ACT 0200,Australia,john.maindonald@anu.edu.au http://www.maths.anu.edu.au/-johnm There will be occasional references to DAAGUR:Maindonald,J.H.Braun,J.B.2007.Data Analysis Graphics Using R.An Example- Based Approach.Cambridge University Press,Cambridge,UK,2007. http://www.maths.anu.edu.au/-johnm/r-book.html Useful Web Sites for Australasian R Users: CRAN (Comprehensive R Archive Network):http://cran.r-project.org To obtain R and associated packages,use the nearest mirror. http://mirror.aarnet.edu.au/pub/CRAN or http://cran.ms.unimelb.edu.au/. Windows,Linux,Unix and MacOS X versions are available,at no cost. R homepage:http://www.r-project.org/ Wikipedia:http://en.wikipedia.org/wiki/R_(programming_language) R-downunder:http://www.stat.auckland.ac.nz/mailman/listinfo/r-downunder For other useful web pages,click on the menu item R help,and look under Resources on the browser window that pops up. Source of Information on R Graphics: Helpful books on R graphics,with web sites that give code,are: Paul Murrell:R Graphics.Chapman and Hall/CRC 2006. (http://www.stat.auckland.ac.nz/-paul/RGraphics/rgraphics.html) Deepayan Sarkar:Lattice.Multivariate Data Visualization with R.Springer 2008. (http://lmdvr.r-forge.r-project.org). The CRAN Graphics task view(http://cran.ms.unimelb.edu.au/web/views/Graphics.html) has summary information on a rich variety of R graphics packages. Note also Hadley Wickham's forthcoming book on ggplot2.A draft is available from http://had. co.nz/ggplot2
2 John H. Maindonald, Centre for Mathematics & Its Applications, Mathematical Sciences Institute, Australian National University, Canberra ACT 0200, Australia, john.maindonald@anu.edu.au http://www.maths.anu.edu.au/~johnm There will be occasional references to DAAGUR: Maindonald, J. H. & Braun, J. B. 2007. Data Analysis & Graphics Using R. An ExampleBased Approach. Cambridge University Press, Cambridge, UK, 2007. http://www.maths.anu.edu.au/~johnm/r-book.html Useful Web Sites for Australasian R Users: CRAN (Comprehensive R Archive Network): http://cran.r-project.org To obtain R and associated packages, use the nearest mirror. http://mirror.aarnet.edu.au/pub/CRAN or http://cran.ms.unimelb.edu.au/. Windows, Linux, Unix and MacOS X versions are available, at no cost. R homepage: http://www.r-project.org/ Wikipedia: http://en.wikipedia.org/wiki/R_(programming_language) R-downunder: http://www.stat.auckland.ac.nz/mailman/listinfo/r-downunder For other useful web pages, click on the menu item R help, and look under Resources on the browser window that pops up. Source of Information on R Graphics: Helpful books on R graphics, with web sites that give code, are: Paul Murrell: R Graphics. Chapman and Hall/CRC 2006. (http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html) Deepayan Sarkar: Lattice. Multivariate Data Visualization with R. Springer 2008. (http://lmdvr.r-forge.r-project.org). The CRAN Graphics task view (http://cran.ms.unimelb.edu.au/web/views/Graphics.html) has summary information on a rich variety of R graphics packages. Note also Hadley Wickham’s forthcoming book on ggplot2. A draft is available from http://had. co.nz/ggplot2
Contents 1 Preliminaries 5 1.1 Installation of R and of R Packages.··.·。··.··················· 5 l.l.1 nstallation of packages from the command line..,··.···········, 5 l.2 The R Commander,································ 6 1.3 The R Commander GUI 6 2 Base Graphics 9 2 1 plot()and allied functions.:·。·.························· 9 2.l.1 Fine control-parameter settings...............·.·.·.····· 9 2.l.2 Adding points,lines and text-examples····················· 2.2 Plotting Mathematical Symbols..........·..··......·....·.... 12 2.3 Summary 12 2.4 Exercises 3 Lattice Graphics 15 3.1 Lattice Graphics 15 3.1.1 Groups within data,and/or columns in parallel ................. 18 3.l.2 Lattice Parameters and Graphics Features.,.·················· 19 3.1.3 Setting that are not available using simpleTheme() 20 3.l.4Keys-auto.key,key&legend.......................... 21 3.1.5 Panel functions and interaction with plots.. 2 3.l.6 Interaction with lattice plots-focus,interact,.unfocus.......·:···.. 22 3.1.7 Arbitrary placement of labels.···.·············· 2 3.l.8 Multiple graphs on a single graphics page····················· 23 3.l.9 Plots that Show Distributions.·.·.....·..·.·...。.. 24 4 The ggplot2 Package 27 4.2 Dynamic Graphics-the rgl package............................ 29 5 References and Bibliography 31 5.1 Books and Papers on R..······ 5.2Web-Based Information······· 1 5.3 Graphics.·。··········· 32 3
Contents 1 Preliminaries 5 1.1 Installation of R and of R Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.1 Installation of packages from the command line . . . . . . . . . . . . . . . . . . 5 1.2 The R Commander . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 The R Commander GUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Base Graphics 9 2.1 plot() and allied functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1.1 Fine control – parameter settings . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1.2 Adding points, lines and text – examples . . . . . . . . . . . . . . . . . . . . . 11 2.2 Plotting Mathematical Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 3 Lattice Graphics 15 3.1 Lattice Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.1.1 Groups within data, and/or columns in parallel . . . . . . . . . . . . . . . . . . 18 3.1.2 Lattice Parameters and Graphics Features . . . . . . . . . . . . . . . . . . . . . 19 3.1.3 Setting that are not available using simpleTheme() . . . . . . . . . . . . . . . 20 3.1.4 Keys – auto.key, key & legend . . . . . . . . . . . . . . . . . . . . . . . . . . 21 3.1.5 Panel functions and interaction with plots . . . . . . . . . . . . . . . . . . . . . 21 3.1.6 Interaction with lattice plots – focus, interact, unfocus . . . . . . . . . . . . . . 22 3.1.7 Arbitrary placement of labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.1.8 Multiple graphs on a single graphics page . . . . . . . . . . . . . . . . . . . . . 23 3.1.9 Plots that Show Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 4 The ggplot2 Package 27 4.1 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 4.2 Dynamic Graphics – the rgl package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 5 References and Bibliography 31 5.1 Books and Papers on R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.2 Web-Based Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 5.3 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3
4 CONTENTS
4 CONTENTS
Chapter 1 Preliminaries The "preliminaries"that are discussed here will extend to using the R Commander menu to draw graphs! 1.1 Installation of R and of R Packages Installation of R First download and install R from a CRAN site,e.g. http://cran.ms.unimelb.edu.au/or http://mirror.aarnet.edu.au/pub//CRAN/ Windows an MacOS X users should download the relevant executable. (e.g.R-2.7.0-win32.exe for Windows,or R-2.7.0.dmg for MacOS X), and open the downloaded file (e.g.,click on it)to start insallation Installation of R Packages (Windows MacOS X) Start R(e.g.,click on the R icon).Then use the relevant menu item to install packages via an internet connection. This is(usually)easier than downloading,then installing. Locating packages The CRAN task views may be a good first place to go. For installation,follow the instructions in the text box.For installing packages,Windows users will first need to specify a mirror.In Australia,specify the Australian mirror. A fresh install is typically required to take advantage of new major releases (e.g.moving from a 2.6 series release to a 2.7 series release)when they appear.For working through these notes,version 2.7.0 or later should be installed. 1.1.1 Installation of packages from the command line For packages where there are dependencies,installation from the command line may be an attractive way to go.First,start R,perhaps by clicking on an R icon.Make sure that you have a live internet connection. For the R Commander,enter: install.packages("Rcmdr",dependencies=TRUE) Doing the installation this way ensures that other packages that R Commander may want get installed at the same time.One of those packages is the rgl 3D graphics package that I will describe briefly. Other graphics packages that this installs are scatterplot3d,ucd(visualization of categorical data)and colorspace (for generation of color palettes,etc)
Chapter 1 Preliminaries The “preliminaries” that are discussed here will extend to using the R Commander menu to draw graphs! 1.1 Installation of R and of R Packages Installation of R First download and install R from a CRAN site, e.g. http://cran.ms.unimelb.edu.au/ or http://mirror.aarnet.edu.au/pub//CRAN/ Windows an MacOS X users should download the relevant executable, (e.g. R-2.7.0-win32.exe for Windows, or R-2.7.0.dmg for MacOS X), and open the downloaded file (e.g., click on it) to start insallation Installation of R Packages (Windows & MacOS X) Start R (e.g., click on the R icon). Then use the relevant menu item to install packages via an internet connection. This is (usually) easier than downloading, then installing. Locating packages The CRAN task views may be a good first place to go. For installation, follow the instructions in the text box. For installing packages, Windows users will first need to specify a mirror. In Australia, specify the Australian mirror. A fresh install is typically required to take advantage of new major releases (e.g. moving from a 2.6 series release to a 2.7 series release) when they appear. For working through these notes, version 2.7.0 or later should be installed. 1.1.1 Installation of packages from the command line For packages where there are dependencies, installation from the command line may be an attractive way to go. First, start R, perhaps by clicking on an R icon. Make sure that you have a live internet connection. For the R Commander, enter: install . packages ( " Rcmdr " , dependencies = TRUE ) Doing the installation this way ensures that other packages that R Commander may want get installed at the same time. One of those packages is the rgl 3D graphics package that I will describe briefly. Other graphics packages that this installs are scatterplot3d, vcd (visualization of categorical data) and colorspace (for generation of color palettes, etc). 5