SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Downloaden Sie, um offline zu lesen
Stat405
    Polishing your plots for presentation



                             Hadley Wickham
Wednesday, 4 November 2009
1. Thanksgiving
              2. Finish off exploratory graphics
              3. Communication graphics
              4. Scales
              5. Themes



Wednesday, 4 November 2009
Thanksgiving


                  No class Wed 25 November.
                  No assignment due over Thanksgiving.




Wednesday, 4 November 2009
50
                ●
                   ● ●●
                ● ● ●●       ● ●
                             ●● ●●
                ● ● ● ●●             ●●
                 ● ● ●● ● ● ● ● ●   ●         ●●●
     45
               ●     ●● ●           ● ●
                                  ● ● ● ●● ●
                                   ●● ● ●
                ●    ●●                ●
                                  ● ● ●●    ●● ●
                                ●● ● ●●● ● ● ●●
                                              ●
               ●
               ●●● ● ● ●●       ● ●● ● ● ●●●
                                   ●
                                ● ● ● ● ●●
                                    ●● ●● ●●
                                                             % cancelled

                ●         ●●        ● ● ●●   ●
                                             ●                    0.0
                                            ●●
     40                                                       ●




                 ●       ●
                         ●●      ● ●        ●
                ●● ● ●
                 ●             ● ●    ● ●●●
                                     ● ●●●●●                  ●   0.2

                ● ●● ●          ●●      ●●
                                      ●● ●●
                                                             ●    0.4

     35          ●● ● ● ● ● ●●● ● ● ●●
                  ●
                  ●● ●         ●       ●● ●                  ●    0.6
                  ●●
                   ●● ●      ● ● ● ●●●● ●
                                    ●● ●
                                ●●●● ● ●●●●                  ●    0.8
                    ●     ● ●●● ● ● ●●
                                 ●●
                              ●● ●●●●● ●●                    ●    1.0
     30                        ●●● ●●● ●
                               ●●●● ●
                                         ●
                              ●●        ●●
                                         ●
                                        ●●
                               ●
                               ●         ●●
     25
                                         ●
                      −120   −110   −100   −90   −80   −70




Wednesday, 4 November 2009
Your turn
                  Perform identify the data and layers in the
                  flight delays data, then write the ggplot2
                  code to create it.
                  library(ggplot2)
                  library(maps)
                  usa <- map_data("state")
                  feb13 <- read.csv("delays-feb-13-2007.csv")



Wednesday, 4 November 2009
usa <- map_data("state")

     ggplot(feb13, aes(long, lat)) +
       geom_point(aes(size = 1), colour = "white") +
       geom_polygon(aes(group = group), data = usa,
         colour = "grey70", fill = NA) +
       geom_point(aes(size = ncancelw / ntot),
         colour = alpha("black", 1/2))

     # Polishing: up next
     last_plot() +
       scale_area("% cancelled", to = c(1, 8),
         breaks = seq(0, 1, by = 0.2), limits = c(0, 1))
       scale_x_continuous("", limits = c(-125, -67)),
       scale_y_continuous("", limits = c(24, 50))
Wednesday, 4 November 2009
troops <- read.csv("minard-troops.csv")
     cities <- read.csv("minard-cities.csv")

     ggplot(cities, aes(long, lat)) +
       geom_path(aes(size = survivors, colour = direction,
         group = interaction(group, direction)), data = troops) +
       geom_text(aes(label = city), hjust = 0, vjust = 1, size = 4)

     # Polish appearance
     last_plot() +
       scale_x_continuous("", limits = c(24, 39)) +
       scale_y_continuous("") +
       scale_colour_manual(values = c("grey50","red")) +
       scale_size(to = c(1, 10))



Wednesday, 4 November 2009
Communication graphics

                  When you need to communicate your
                  findings, you need to spend a lot of time
                  polishing your graphics to eliminate
                  distractions and focus on the story.
                  Now it’s time to pay attention to the small
                  stuff: labels, colour choices, tick marks...



Wednesday, 4 November 2009
Context
Wednesday, 4 November 2009
Consumption
Wednesday, 4 November 2009
Tools

                  Scales. Used to override default
                  perceptual mappings, and tune
                  parameters of axes and legends.
                  Themes: control presentation of
                  non-data elements.



Wednesday, 4 November 2009
Scales
                  Control how data is mapped to perceptual
                  properties, and produce guides (axes and
                  legends) which allow us to read the plot.
                  Important parameters: name, breaks &
                  labels, limits.
                  Naming scheme: scale_aesthetic_name.
                  All default scales have name continuous or
                  discrete.


Wednesday, 4 November 2009
# Default scales
     scale_x_continuous()
     scale_y_discrete()
     scale_colour_discrete()

     # Custom scales
     scale_colour_hue()
     scale_x_log10()
     scale_fill_brewer()

     # Scales with parameters
     scale_x_continuous("X Label", limits = c(1, 10))
     scale_colour_gradient(low = "blue", high = "red")


Wednesday, 4 November 2009
p <- qplot(cyl, displ, data = mpg)

     # First argument (name) controls axis label
     p + scale_y_continuous("Displacement (l)")
     p + scale_x_continuous("Cylinders")

     # Breaks and labels control tick marks
     p + scale_x_continuous(breaks = c(4, 6, 8))
     p + scale_x_continuous(breaks = c(4, 6, 8),
       labels = c("small", "medium", "big"))

     #    Limits control range of data
     p    + scale_y_continuous(limits = c(1, 8))
     #    same as:
     p    + ylim(1, 8)
Wednesday, 4 November 2009
Your turn
              qplot(carat, price, data = diamonds, geom = "hex")

              Manipulate the fill colour legend to:
                  • Change the title to “Count"
                  • Display breaks at 1000, 3500 & 7000
                  • Add commas to the keys (e.g. 1,000)
                  • Set the limit for the scale from 0 to 8000.


Wednesday, 4 November 2009
p <- qplot(carat, price, data = diamonds, geom = "hex")

     # First argument (name) controls legend title
     p + scale_fill_continuous("Count")

     # Breaks and labels control legend keys
     p + scale_fill_continuous(breaks = c(1000, 3500, 7000))
     p + scale_fill_continuous(breaks = c(0, 4000, 8000))

     # Why don't 0 and 8000 have colours?
     p + scale_fill_continuous(breaks = c(0, 4000, 8000),
       limits = c(0, 8000))

     # Can use labels to make more human readable
     breaks <- c(0, 2000, 4000, 6000, 8000)
     labels <- format(breaks, big.mark = ",")
     p + scale_fill_continuous(breaks = breaks, labels = labels,
        limits = c(0, 8000))

Wednesday, 4 November 2009
p <- qplot(color, carat, data = diamonds)

     # Basically the same for discrete variables
     p + scale_x_discrete("Color")

     # Except limits is now a character vector
     p + scale_x_discrete(limits = c("D", "E", "F"))

     # Should work for boxplots too
     qplot(color, carat, data = diamonds,
       geom = "boxplot") +
       scale_x_discrete(limits = c("D", "E", "F"))
     # But currently a bug :(


Wednesday, 4 November 2009
Alternate scales
                  Can also override the default choice of
                  scales. You are most likely to want to do
                  this with colour, as it is the most
                  important aesthetic after position.
                  Need to know a little theory to be able to
                  use it effectively: colour spaces & colour
                  blindness.


Wednesday, 4 November 2009
Colour spaces
                  Most familiar is rgb: defines colour as
                  mixture of red, green and blue. Matches
                  the physics of eye, but the brain does a
                  lot of post-processing, so it’s hard to
                  directly perceive these components.
                  A more useful colour space is hcl:
                  hue, chroma and luminance


Wednesday, 4 November 2009
hue
                             luminance




                                         chroma
Wednesday, 4 November 2009
hue

                             Demo of real
                             shape in 3d
                             luminance




                                         chroma
Wednesday, 4 November 2009
Default colour scales

                  Discrete: evenly spaced hues of equal
                  chroma and luminance. No colour
                  appears more important than any other.
                  Does not imply order.
                  Continuous: evenly spaced hues
                  between two colours.



Wednesday, 4 November 2009
Alternatives


                  Discrete: brewer
                  Continuous: gradient2, gradientn




Wednesday, 4 November 2009
Color brewer

                  Cynthia Brewer applied basics principles
                  and then rigorously tested to produce
                  selection of good palettes, particularly
                  tailored for maps: http://colorbrewer2.org/
                  Can use cut_interval() or cut_number()
                  to convert continuous to discrete.



Wednesday, 4 November 2009
# Fancy looking trigonometric function
     vals <- seq(-4 * pi, 4 * pi, len = 50)
     df <- expand.grid(x = vals, y = vals)
     df$r <- with(df, sqrt(x ^ 2 + y ^ 2))
     df$z <- with(df, cos(r ^ 2) * exp(- r / 6))
     df$z_cut <- cut_interval(df$z, 9)

     (p1 <-             qplot(x, y, data = df, fill = z,
       geom             = "tile"))
     (p2 <-             qplot(x, y, data = df, fill = z_cut,
       geom             = "tile"))



Wednesday, 4 November 2009
p1 + scale_fill_gradient(low = "white",
       high = "black")

     # Highlight deviations
     p1 + scale_fill_gradient2()
     p1 + scale_fill_gradient2(breaks = seq(-1, 1,
       by = 0.25), limits = c(-1, 1))
     p1 + scale_fill_gradient2(mid = "white",
       low = "black", high = "black")

     p2 + scale_fill_brewer(pal = "Blues")



Wednesday, 4 November 2009
Colour blindness

                  7-10% of men are red-green colour
                  “blind”. (Many other rarer types of colour
                  blindness)
                  Solutions: avoid red-green contrasts; use
                  redundant mappings; test. I like color
                  oracle: http://colororacle.cartography.ch



Wednesday, 4 November 2009
Your turn

                  Read through the examples for
                  scale_colour_brewer,
                  scale_colour_gradient2 and
                  scale_colour_gradientn.
                  Experiment!



Wednesday, 4 November 2009
Other resources

                  A. Zeileis, K. Hornik, and P. Murrell.
                  Escaping RGBland: Selecting colors for
                  statistical graphics. Computational
                  Statistics & Data Analysis, 2008.
                  http://statmath.wu-wien.ac.at/~zeileis/papers/
                  Zeileis+Hornik+Murrell-2008.pdf.




Wednesday, 4 November 2009
Themes



Wednesday, 4 November 2009
Visual appearance
                  So far have only discussed how to get the
                  data displayed the way you want,
                  focussing on the essence of the plot.
                  Themes give you a huge amount of
                  control over the appearance of the plot,
                  the choice of background colours, fonts
                  and so on.


Wednesday, 4 November 2009
# Two built in themes. The default:
     qplot(carat, price, data = diamonds)

     # And a theme with a white background:
     qplot(carat, price, data = diamonds) + theme_bw()

     # Use theme_set if you want it to apply to every
     # future plot.
     theme_set(theme_bw())

     # This is the best way of seeing all the default
     # options
     theme_bw()
     theme_grey()

Wednesday, 4 November 2009
Elements
                  You can also make your own theme, or
                  modify and existing.
                  Themes are made up of elements which
                  can be one of: theme_line, theme_segment,
                  theme_text, theme_rect, theme_blank
                  Gives you a lot of control over plot
                  appearance.


Wednesday, 4 November 2009
Elements
                  Axis: axis.line, axis.text.x, axis.text.y,
                  axis.ticks, axis.title.x, axis.title.y
                  Legend: legend.background, legend.key,
                  legend.text, legend.title
                  Panel: panel.background, panel.border,
                  panel.grid.major, panel.grid.minor
                  Strip: strip.background, strip.text.x,
                  strip.text.y


Wednesday, 4 November 2009
p <- qplot(displ, hwy, data = mpg) +
       opts(title = "Bigger engines are less efficient")

     # To modify a plot
     p
     p + opts(plot.title     =
       theme_text(size =     12, face = "bold"))
     p + opts(plot.title     = theme_text(colour = "red"))
     p + opts(plot.title     = theme_text(angle = 45))
     p + opts(plot.title     = theme_text(hjust = 1))




Wednesday, 4 November 2009
Your turn
                  Fix the overlapping y labels on this plot:
                  qplot(reorder(model, hwy), hwy, data =
                  mpg)
                  Rotate the labels on these strips so they
                  are easier to read.
                  qplot(hwy, reorder(model, hwy), data =
                  mpg) + facet_grid(manufacturer ~ .,
                  scales = "free", space = "free")


Wednesday, 4 November 2009
Feedback
                 http://hadley.wufoo.com/forms/
                        stat405-feedback/




Wednesday, 4 November 2009

Weitere ähnliche Inhalte

Ähnlich wie 20 Polishing

ひけらかし会(visualization)
ひけらかし会(visualization)ひけらかし会(visualization)
ひけらかし会(visualization)
moai kids
 
A Large-Scale Study of Test Coverage Evolution
A Large-Scale Study of Test Coverage EvolutionA Large-Scale Study of Test Coverage Evolution
A Large-Scale Study of Test Coverage Evolution
jon_bell
 
Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)
Hadley Wickham
 
Optimal Nudging. Presentation UD.
Optimal Nudging. Presentation UD.Optimal Nudging. Presentation UD.
Optimal Nudging. Presentation UD.
r-uribe
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
Peter Kofler
 
Efficient Tabling of Structured Data Using Indexing and Program Transformation
Efficient Tabling of Structured Data Using Indexing and Program TransformationEfficient Tabling of Structured Data Using Indexing and Program Transformation
Efficient Tabling of Structured Data Using Indexing and Program Transformation
Christian Have
 

Ähnlich wie 20 Polishing (20)

01 Intro
01 Intro01 Intro
01 Intro
 
The Volcano/Cascades Optimizer
The Volcano/Cascades OptimizerThe Volcano/Cascades Optimizer
The Volcano/Cascades Optimizer
 
Unit Testing Tool Competition-Eighth Round
Unit Testing Tool Competition-Eighth RoundUnit Testing Tool Competition-Eighth Round
Unit Testing Tool Competition-Eighth Round
 
PLOTCON NYC: New Open Viz in R
PLOTCON NYC: New Open Viz in RPLOTCON NYC: New Open Viz in R
PLOTCON NYC: New Open Viz in R
 
No BS Data Salon #3: Probabilistic Sketching
No BS Data Salon #3: Probabilistic SketchingNo BS Data Salon #3: Probabilistic Sketching
No BS Data Salon #3: Probabilistic Sketching
 
ひけらかし会(visualization)
ひけらかし会(visualization)ひけらかし会(visualization)
ひけらかし会(visualization)
 
Advanced Cartography for the Web
Advanced Cartography for the WebAdvanced Cartography for the Web
Advanced Cartography for the Web
 
A Large-Scale Study of Test Coverage Evolution
A Large-Scale Study of Test Coverage EvolutionA Large-Scale Study of Test Coverage Evolution
A Large-Scale Study of Test Coverage Evolution
 
Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)Model Visualisation (with ggplot2)
Model Visualisation (with ggplot2)
 
Count-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdfCount-min sketch to Infinity.pdf
Count-min sketch to Infinity.pdf
 
Python for PHP developers
Python for PHP developersPython for PHP developers
Python for PHP developers
 
Optimal Nudging. Presentation UD.
Optimal Nudging. Presentation UD.Optimal Nudging. Presentation UD.
Optimal Nudging. Presentation UD.
 
Prof. Jim Bezdek: Every Picture Tells a Story — Visual Cluster Analysis
Prof. Jim Bezdek: Every Picture Tells a Story — Visual Cluster AnalysisProf. Jim Bezdek: Every Picture Tells a Story — Visual Cluster Analysis
Prof. Jim Bezdek: Every Picture Tells a Story — Visual Cluster Analysis
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
 
17 Sampling Dist
17 Sampling Dist17 Sampling Dist
17 Sampling Dist
 
Efficient Tabling of Structured Data Using Indexing and Program Transformation
Efficient Tabling of Structured Data Using Indexing and Program TransformationEfficient Tabling of Structured Data Using Indexing and Program Transformation
Efficient Tabling of Structured Data Using Indexing and Program Transformation
 
Quines—Programming your way back to where you were
Quines—Programming your way back to where you wereQuines—Programming your way back to where you were
Quines—Programming your way back to where you were
 
20131212 - Sydney - Garvan Institute - Human Genetics and Big Data
20131212 - Sydney - Garvan Institute - Human Genetics and Big Data20131212 - Sydney - Garvan Institute - Human Genetics and Big Data
20131212 - Sydney - Garvan Institute - Human Genetics and Big Data
 
Advanced Procedural Rendering in DirectX11 - CEDEC 2012
Advanced Procedural Rendering in DirectX11 - CEDEC 2012 Advanced Procedural Rendering in DirectX11 - CEDEC 2012
Advanced Procedural Rendering in DirectX11 - CEDEC 2012
 
graphical-copy-130308123000-phpapp02.pptx
graphical-copy-130308123000-phpapp02.pptxgraphical-copy-130308123000-phpapp02.pptx
graphical-copy-130308123000-phpapp02.pptx
 

Mehr von Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
24 modelling
24 modelling24 modelling
24 modelling
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
10 simulation
10 simulation10 simulation
10 simulation
 
10 simulation
10 simulation10 simulation
10 simulation
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

20 Polishing

  • 1. Stat405 Polishing your plots for presentation Hadley Wickham Wednesday, 4 November 2009
  • 2. 1. Thanksgiving 2. Finish off exploratory graphics 3. Communication graphics 4. Scales 5. Themes Wednesday, 4 November 2009
  • 3. Thanksgiving No class Wed 25 November. No assignment due over Thanksgiving. Wednesday, 4 November 2009
  • 4. 50 ● ● ●● ● ● ●● ● ● ●● ●● ● ● ● ●● ●● ● ● ●● ● ● ● ● ● ● ●●● 45 ● ●● ● ● ● ● ● ● ●● ● ●● ● ● ● ●● ● ● ● ●● ●● ● ●● ● ●●● ● ● ●● ● ● ●●● ● ● ●● ● ●● ● ● ●●● ● ● ● ● ● ●● ●● ●● ●● % cancelled ● ●● ● ● ●● ● ● 0.0 ●● 40 ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●●● ● ●●●●● ● 0.2 ● ●● ● ●● ●● ●● ●● ● 0.4 35 ●● ● ● ● ● ●●● ● ● ●● ● ●● ● ● ●● ● ● 0.6 ●● ●● ● ● ● ● ●●●● ● ●● ● ●●●● ● ●●●● ● 0.8 ● ● ●●● ● ● ●● ●● ●● ●●●●● ●● ● 1.0 30 ●●● ●●● ● ●●●● ● ● ●● ●● ● ●● ● ● ●● 25 ● −120 −110 −100 −90 −80 −70 Wednesday, 4 November 2009
  • 5. Your turn Perform identify the data and layers in the flight delays data, then write the ggplot2 code to create it. library(ggplot2) library(maps) usa <- map_data("state") feb13 <- read.csv("delays-feb-13-2007.csv") Wednesday, 4 November 2009
  • 6. usa <- map_data("state") ggplot(feb13, aes(long, lat)) + geom_point(aes(size = 1), colour = "white") + geom_polygon(aes(group = group), data = usa, colour = "grey70", fill = NA) + geom_point(aes(size = ncancelw / ntot), colour = alpha("black", 1/2)) # Polishing: up next last_plot() + scale_area("% cancelled", to = c(1, 8), breaks = seq(0, 1, by = 0.2), limits = c(0, 1)) scale_x_continuous("", limits = c(-125, -67)), scale_y_continuous("", limits = c(24, 50)) Wednesday, 4 November 2009
  • 7. troops <- read.csv("minard-troops.csv") cities <- read.csv("minard-cities.csv") ggplot(cities, aes(long, lat)) + geom_path(aes(size = survivors, colour = direction, group = interaction(group, direction)), data = troops) + geom_text(aes(label = city), hjust = 0, vjust = 1, size = 4) # Polish appearance last_plot() + scale_x_continuous("", limits = c(24, 39)) + scale_y_continuous("") + scale_colour_manual(values = c("grey50","red")) + scale_size(to = c(1, 10)) Wednesday, 4 November 2009
  • 8. Communication graphics When you need to communicate your findings, you need to spend a lot of time polishing your graphics to eliminate distractions and focus on the story. Now it’s time to pay attention to the small stuff: labels, colour choices, tick marks... Wednesday, 4 November 2009
  • 11. Tools Scales. Used to override default perceptual mappings, and tune parameters of axes and legends. Themes: control presentation of non-data elements. Wednesday, 4 November 2009
  • 12. Scales Control how data is mapped to perceptual properties, and produce guides (axes and legends) which allow us to read the plot. Important parameters: name, breaks & labels, limits. Naming scheme: scale_aesthetic_name. All default scales have name continuous or discrete. Wednesday, 4 November 2009
  • 13. # Default scales scale_x_continuous() scale_y_discrete() scale_colour_discrete() # Custom scales scale_colour_hue() scale_x_log10() scale_fill_brewer() # Scales with parameters scale_x_continuous("X Label", limits = c(1, 10)) scale_colour_gradient(low = "blue", high = "red") Wednesday, 4 November 2009
  • 14. p <- qplot(cyl, displ, data = mpg) # First argument (name) controls axis label p + scale_y_continuous("Displacement (l)") p + scale_x_continuous("Cylinders") # Breaks and labels control tick marks p + scale_x_continuous(breaks = c(4, 6, 8)) p + scale_x_continuous(breaks = c(4, 6, 8), labels = c("small", "medium", "big")) # Limits control range of data p + scale_y_continuous(limits = c(1, 8)) # same as: p + ylim(1, 8) Wednesday, 4 November 2009
  • 15. Your turn qplot(carat, price, data = diamonds, geom = "hex") Manipulate the fill colour legend to: • Change the title to “Count" • Display breaks at 1000, 3500 & 7000 • Add commas to the keys (e.g. 1,000) • Set the limit for the scale from 0 to 8000. Wednesday, 4 November 2009
  • 16. p <- qplot(carat, price, data = diamonds, geom = "hex") # First argument (name) controls legend title p + scale_fill_continuous("Count") # Breaks and labels control legend keys p + scale_fill_continuous(breaks = c(1000, 3500, 7000)) p + scale_fill_continuous(breaks = c(0, 4000, 8000)) # Why don't 0 and 8000 have colours? p + scale_fill_continuous(breaks = c(0, 4000, 8000), limits = c(0, 8000)) # Can use labels to make more human readable breaks <- c(0, 2000, 4000, 6000, 8000) labels <- format(breaks, big.mark = ",") p + scale_fill_continuous(breaks = breaks, labels = labels, limits = c(0, 8000)) Wednesday, 4 November 2009
  • 17. p <- qplot(color, carat, data = diamonds) # Basically the same for discrete variables p + scale_x_discrete("Color") # Except limits is now a character vector p + scale_x_discrete(limits = c("D", "E", "F")) # Should work for boxplots too qplot(color, carat, data = diamonds, geom = "boxplot") + scale_x_discrete(limits = c("D", "E", "F")) # But currently a bug :( Wednesday, 4 November 2009
  • 18. Alternate scales Can also override the default choice of scales. You are most likely to want to do this with colour, as it is the most important aesthetic after position. Need to know a little theory to be able to use it effectively: colour spaces & colour blindness. Wednesday, 4 November 2009
  • 19. Colour spaces Most familiar is rgb: defines colour as mixture of red, green and blue. Matches the physics of eye, but the brain does a lot of post-processing, so it’s hard to directly perceive these components. A more useful colour space is hcl: hue, chroma and luminance Wednesday, 4 November 2009
  • 20. hue luminance chroma Wednesday, 4 November 2009
  • 21. hue Demo of real shape in 3d luminance chroma Wednesday, 4 November 2009
  • 22. Default colour scales Discrete: evenly spaced hues of equal chroma and luminance. No colour appears more important than any other. Does not imply order. Continuous: evenly spaced hues between two colours. Wednesday, 4 November 2009
  • 23. Alternatives Discrete: brewer Continuous: gradient2, gradientn Wednesday, 4 November 2009
  • 24. Color brewer Cynthia Brewer applied basics principles and then rigorously tested to produce selection of good palettes, particularly tailored for maps: http://colorbrewer2.org/ Can use cut_interval() or cut_number() to convert continuous to discrete. Wednesday, 4 November 2009
  • 25. # Fancy looking trigonometric function vals <- seq(-4 * pi, 4 * pi, len = 50) df <- expand.grid(x = vals, y = vals) df$r <- with(df, sqrt(x ^ 2 + y ^ 2)) df$z <- with(df, cos(r ^ 2) * exp(- r / 6)) df$z_cut <- cut_interval(df$z, 9) (p1 <- qplot(x, y, data = df, fill = z, geom = "tile")) (p2 <- qplot(x, y, data = df, fill = z_cut, geom = "tile")) Wednesday, 4 November 2009
  • 26. p1 + scale_fill_gradient(low = "white", high = "black") # Highlight deviations p1 + scale_fill_gradient2() p1 + scale_fill_gradient2(breaks = seq(-1, 1, by = 0.25), limits = c(-1, 1)) p1 + scale_fill_gradient2(mid = "white", low = "black", high = "black") p2 + scale_fill_brewer(pal = "Blues") Wednesday, 4 November 2009
  • 27. Colour blindness 7-10% of men are red-green colour “blind”. (Many other rarer types of colour blindness) Solutions: avoid red-green contrasts; use redundant mappings; test. I like color oracle: http://colororacle.cartography.ch Wednesday, 4 November 2009
  • 28. Your turn Read through the examples for scale_colour_brewer, scale_colour_gradient2 and scale_colour_gradientn. Experiment! Wednesday, 4 November 2009
  • 29. Other resources A. Zeileis, K. Hornik, and P. Murrell. Escaping RGBland: Selecting colors for statistical graphics. Computational Statistics & Data Analysis, 2008. http://statmath.wu-wien.ac.at/~zeileis/papers/ Zeileis+Hornik+Murrell-2008.pdf. Wednesday, 4 November 2009
  • 31. Visual appearance So far have only discussed how to get the data displayed the way you want, focussing on the essence of the plot. Themes give you a huge amount of control over the appearance of the plot, the choice of background colours, fonts and so on. Wednesday, 4 November 2009
  • 32. # Two built in themes. The default: qplot(carat, price, data = diamonds) # And a theme with a white background: qplot(carat, price, data = diamonds) + theme_bw() # Use theme_set if you want it to apply to every # future plot. theme_set(theme_bw()) # This is the best way of seeing all the default # options theme_bw() theme_grey() Wednesday, 4 November 2009
  • 33. Elements You can also make your own theme, or modify and existing. Themes are made up of elements which can be one of: theme_line, theme_segment, theme_text, theme_rect, theme_blank Gives you a lot of control over plot appearance. Wednesday, 4 November 2009
  • 34. Elements Axis: axis.line, axis.text.x, axis.text.y, axis.ticks, axis.title.x, axis.title.y Legend: legend.background, legend.key, legend.text, legend.title Panel: panel.background, panel.border, panel.grid.major, panel.grid.minor Strip: strip.background, strip.text.x, strip.text.y Wednesday, 4 November 2009
  • 35. p <- qplot(displ, hwy, data = mpg) + opts(title = "Bigger engines are less efficient") # To modify a plot p p + opts(plot.title = theme_text(size = 12, face = "bold")) p + opts(plot.title = theme_text(colour = "red")) p + opts(plot.title = theme_text(angle = 45)) p + opts(plot.title = theme_text(hjust = 1)) Wednesday, 4 November 2009
  • 36. Your turn Fix the overlapping y labels on this plot: qplot(reorder(model, hwy), hwy, data = mpg) Rotate the labels on these strips so they are easier to read. qplot(hwy, reorder(model, hwy), data = mpg) + facet_grid(manufacturer ~ ., scales = "free", space = "free") Wednesday, 4 November 2009
  • 37. Feedback http://hadley.wufoo.com/forms/ stat405-feedback/ Wednesday, 4 November 2009