ggplot2
play

ggplot2 Dr. Jennifer (Jenny) Bryan Department of Statistics and - PowerPoint PPT Presentation

ggplot2 Dr. Jennifer (Jenny) Bryan Department of Statistics and Michael Smith Laboratories University of British Columbia use in another Digression: Rs formula syntax intro?


  1. ggplot2 Dr. Jennifer (Jenny) Bryan Department of Statistics and Michael Smith Laboratories University of British Columbia

  2. use in another Digression: R’s formula syntax intro? http://cran.r-project.org/doc/manuals/R-intro.html#Formulae-for-statistical-models y ~ x “y twiddle x” In modelling functions, says y is response or dependent variable and x is the predictor or covariate or independent variable. More generally, the right-hand side can be much more complicated. In many plotting functions, esp. lattice, this says to plot y against x.

  3. “A picture is worth a thousand words”

  4. 1986 Challenger space shuttle disaster Favorite example of Edward Tufte http://msnbcmedia1.msn.com/j/msnbc/Components/Photos/050709/050609_columbia_hmed_6p.hmedium.jpg

  5. “A picture is worth a thousand words”

  6. “A picture is worth a thousand words” Siddhartha R. Dalal; Edward B. Fowlkes; Bruce Hoadley. Risk Analysis of the Space Shuttle: Pre-Challenger Prediction of Failure. JASA, Vol. 84, No. 408 (Dec., 1989), pp. 945-957. Access via JSTOR.

  7. Edward Tufte http://www.edwardtufte.com BOOK: Visual Explanations: Images and Quantities, Evidence and Narrative Ch. 5 deals with the Challenger disaster That chapter is available for $7 as a downloadable booklet: http://www.edwardtufte.com/tufte/books_textb

  8. “A picture is worth a thousand words” Always, always, always plot the data. Replace (or complement) ‘typical’ tables of data or statistical results with figures that are more compelling and accessible. Whenever possible, generate figures that overlay / juxtapose observed data and analytical results, e.g. the ‘fit’.

  9. base or traditional graphics vs lattice package ships with R, but must load with library(lattice) vs ggplot2 package must be installed and loaded install.packages(“ggplot2”, dependencies = TRUE) library(ggplot2)

  10. Two main goals for statistical graphics • To facilitate comparisons. • To identify trends. lattice and ggplot2 graphics are simply better than traditional graphics for achieving these goals

  11. lattice 10^2.5 10^3.5 10^4.5 10^2.5 10^3.5 10^4.5 1962 1977 1992 2007 ● ● ● ● ● ● ● 80 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 70 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● “multi-panel conditioning” ● ● ● ● ● Europe Europe Europe Europe ● ● ● ● ● ● 60 ● 50 40 lifeExp ~ gdpPercap | continent * year 30 1962 1977 1992 2007 ● ● ● 80 ● ● ● ● ● ● ● ● ● Life expectancy at birth (years) ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 70 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 60 Asia ● Asia ● ● Asia ● Asia ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 50 ● ● ● ● Assignment 1: Best Set of Graphs ● ● ● ● ● ● ● ● ● ● ● ● ● ● 40 ● ● ● ● 30 ● 1962 1977 1992 2007 ● 80 Life Expectancy at Birth (yrs) Life Expectancy at Birth (yrs) ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 70 Americas Americas ● ● Americas ● Americas ● Year of 1950 Year of 1955 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 60 ● ● ● ● ● ● ● ● ● ● ● ● ● 55 70 ● 50 ● ● ● ● ● ● 65 ● ● 40 30 50 40 1962 1977 1992 2007 80 2000 6000 10000 14000 0 5000 10000 15000 ● ● ● ● ● ● ● ● 70 ● ● ● ● ● ● ● Income per Person Income per Person Africa Africa ● Africa ● Africa ● ● ● ● 60 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 50 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Life Expectancy at Birth (yrs) Life Expectancy at Birth (yrs) ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 40 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Year of 1960 Year of 1965 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 30 ● 30 50 70 10^2.5 10^3.5 10^4.5 10^2.5 10^3.5 10^4.5 65 Income per person (GDP/capita, inflation − adjusted $) 55 0 5000 10000 15000 0 5000 10000 15000 20000 Income per Person Income per Person Life Expectancy at Birth (yrs) Life Expectancy at Birth (yrs) Year of 1970 Year of 1975 70 64 70 64 0 5000 10000 20000 0 5000 10000 20000 Income per Person Income per Person Life Expectancy at Birth (yrs) Life Expectancy at Birth (yrs) Year of 1980 Year of 1985 76 base 72 70 66 0 5000 15000 25000 10000 15000 20000 25000 30000 Income per Person Income per Person

  12. ggplot2 “facetting” ggplot(...) + ... + facet_wrap(~ continent)

  13. 1000 10000 1992 2007 ● ● ● ● ● ● ● ● ● ● ● ● ● ● 80 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 70 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 60 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 50 ● ● ● ● ● ● ● ● ● ● ● ● ● ● Life expectancy at birth (years) ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 40 ● ● ● 30 ● 1962 1977 80 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 70 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 60 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 50 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Africa ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● Americas ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 40 ● ● ● ● ● ● ● ● ● Asia ● ● ● ● ● ● ● ● ● ● ● Europe ● ● ● ● ● ● ● Oceania 30 ● 1000 10000 Income per person (GDP/capita, inflation − adjusted $) lattice “groups and superposition” lifeExp ~ gdpPercap | year, group = country

  14. ggplot2 “aesthetic mapping” ggplot(...) + ... + aes(fill = country)

  15. week one .... ggplot2 / lattice quality of output base time invested * figure is totally fabricated but, I claim, still true

  16. after you’ve climbed the steepest part of the learning curve ... ggplot2 / lattice quality of output base time invested * figure is totally fabricated but, I claim, still true

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend