SlideShare a Scribd company logo
1 of 46
Download to read offline
Visualisation in R
                       Hadley Wickham
            Assistant Professor / Dobelman Family Junior Chair
                 Department of Statistics / Rice University



July 2010
Sunday, 25 July 2010
HE LL O
                        my name is




                       Hadley
Sunday, 25 July 2010
http://had.co.nz/
                        vanderbilt-vis


Sunday, 25 July 2010
1. Preview of today
                2. About ggplot2
                3. More resources
                4. Diving in




Sunday, 25 July 2010
Fuel economy
                       Basic graphics


Sunday, 25 July 2010
●




                                          ●

                             40


                                      ●
                                      ●

                             35       ●
                                      ●
                                  ●   ●
                                  ●                       ●
                                              ●       ●●

                             30       ●       ●       ●
                                  ●   ●       ●   ●   ●●                              ●
                       hwy




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

                             25       ●                   ●       ●   ●●              ●        ●                                ●                        ●
                                                      ●●      ●●      ●       ●                     ●                                                            ●
                                                          ●       ●                            ●    ●   ●       ●                        ●
                                                              ●       ●       ●                ●                ●
                                                                                               ●                ●

                             20                               ●                                     ●                           ●●
                                                                                  ●        ●        ●           ●●              ●
                                                                                           ●        ●   ●   ●                       ●   ●●
                                                                              ●●                   ●●   ●       ●●      ●   ●       ●    ●       ●           ●
                                                                                                                ●●          ●       ●

                             15                                                                     ●           ●●          ●●●          ●   ●
                                                                                                                                ●                    ●


                                                                                                                    ●



                                              2                       3                             4                   5                        6               7
                                                                                                   displ
Sunday, 25 July 2010
●




                                          ●

                             40


                                      ●
                                      ●

                             35       ●
                                      ●
                                  ● ●                                                                                                              class
                                  ●                   ●
                                              ●   ●●
                                                                                                                                                    ●   2seater
                             30       ● ●         ●                                                                                                 ●   compact
                                  ● ● ● ● ●●                                    ●
                                                                                                                                                    ●   midsize
                       hwy




                                              ●       ●                 ● ●             ●
                                              ● ● ●●                  ● ● ●             ●                                                           ●   minivan
                                      ● ● ● ●●                ● ●●              ●● ● ●                                   ●             ●

                             25       ●               ●       ● ●●              ●       ●                         ●                    ●            ●   pickup
                                                  ●● ●● ●               ●                    ●                                                 ●
                                                      ●       ●                         ● ● ●        ●                   ●
                                                                                                                                                    ●   subcompact
                                                          ●       ●     ●               ●            ●                                              ●   suv
                                                                                        ●            ●

                             20                           ●                                  ●                    ●●
                                                                            ●       ●        ●       ●●           ●
                                                                                    ●        ● ● ●                    ● ●●
                                                                        ●●                  ●● ●     ●●      ● ● ●       ●     ●           ●
                                                                                                     ●●          ● ●

                             15                                                              ●       ●●          ●●●     ● ●
                                                                                                                  ●                ●


                                                                                                         ●



                                          2                       3                         4                5               6                 7
                                                                                        displ
Sunday, 25 July 2010
Diamond prices
                   Displaying large data


Sunday, 25 July 2010
4000




         3000
 count




         2000




         1000




            0

                       56   58   60    62     64   66   68   70
                                      depth
Sunday, 25 July 2010
●   ●   ●
                                             ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●   ●
                                     ●
                                     ●   ●
                                         ●   ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●     ●
                             ●       ●   ●   ●
                                             ●   ●   ●   ●   ●    ●
                                                                  ●   ●   ●
                               ●     ●   ●
                                         ●   ●   ●
                                                 ●   ●   ●   ●    ●   ●
                                     ●   ●   ●       ●   ●   ●    ●
                             ● ●     ●
                                     ●   ●
                                         ●
                                         ●
                                             ●
                                             ●
                                                 ●
                                                 ●
                                                 ●   ●
                                                     ●
                                                     ●
                                                         ●
                                                         ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●
                                                                  ●
                                                                      ●
                                                                      ●   ● ●
                                     ●   ●   ●   ●
                                                 ●   ●
                                                     ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●   ●
                               ●     ●   ●
                                         ●   ●
                                             ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●   ●    ●   ●   ●
                                     ●   ●
                                         ●   ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●
                                                                  ●   ●   ●
                               ●     ●
                                     ●   ●
                                         ●   ●   ●
                                                 ●   ●   ●
                                                         ●   ●
                                                             ●    ●   ●   ●
                                     ●
                                     ●   ●   ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●
                                                                      ●   ● ●
                                     ●   ●   ●   ●   ●   ●   ●
                                                             ●    ●
                                                                  ●   ●           ●
                                ●    ●   ●
                                         ●
                                         ●   ●
                                             ●   ●
                                                 ●   ●   ●   ●    ●   ●     ●
                                                                            ●     ●
                                     ●   ●   ●
                                             ●   ●
                                                 ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●   ●
                                ●
                                ●    ●   ●   ●   ●   ●   ●
                                                         ●   ●    ●   ●
                                                                      ●   ● ●
                                ●    ●   ●
                                         ●   ●
                                             ●   ●   ●
                                                     ●   ●
                                                         ●
                                                         ●   ●
                                                             ●    ●   ●   ●
                            ●   ●
                              ● ●    ●   ●   ●   ●
                                                 ●   ●
                                                     ●   ●   ●
                                                             ●    ●   ●
                                ●    ●   ●   ●
                                             ●
                                             ●   ●
                                                 ●   ●   ●   ●    ●   ●           ●
                                ●    ●
                                     ●   ●
                                         ●   ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●           ●●
                                                                                  ●
                                ●    ●
                                     ●   ●   ●   ●   ●   ●   ●
                                                             ●    ●   ●
                                ●    ●   ●
                                         ●
                                         ●   ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●   ●
                                                             ●    ●
                                                                  ●   ●
                                                                      ●
                                     ●   ●   ●       ●
                                                     ●   ●   ●            ● ●
         15000                  ●
                                ●
                                     ●
                                     ●
                                     ●
                                         ●
                                         ●
                                         ●
                                         ●
                                             ●
                                             ●
                                             ●
                                                 ●
                                                 ●
                                                 ●
                                                 ●
                                                     ●
                                                     ●
                                                     ●
                                                         ●
                                                         ●
                                                         ●
                                                         ●
                                                             ●
                                                             ●
                                                             ●
                                                                  ●
                                                                  ●
                                                                  ●
                                                                      ●
                                                                      ●
                                                                      ●   ●
                                                                          ●
                                                                            ●         ●
                                                                                      ●
                                     ●   ●
                                         ●   ●
                                             ●   ●   ●   ●   ●    ●
                                                                  ●   ●   ●
                              ●      ●
                                     ●   ●
                                         ●   ●
                                             ●
                                             ●   ●
                                                 ●   ●
                                                     ●   ●
                                                         ●        ●   ●
                                     ●
                                     ●   ●   ●   ●   ●   ●        ●
                                                                  ●
                                                                  ●   ●           ●
                                     ●
                                     ●   ●   ●   ●
                                                 ●   ●            ●
                                     ●   ●
                                         ●
                                         ●   ●
                                             ●   ●
                                                 ●   ●
                                                     ●            ●
                                                                  ●       ●
                                 ●   ●
                                     ●   ●   ●   ●   ●            ●   ●   ●
                                 ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●   ●
                                                 ●                ●
                                                                  ●   ●
                                                                      ●
                                                                      ●   ●       ●●●
                                 ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●   ●
                                                 ●                ●
                                                                  ●   ●           ●
                                 ●   ●
                                     ●   ●   ●   ●
                                                 ●                ●
                                                                  ●   ●       ●
                                     ●
                                     ●   ●
                                         ●   ●   ●
                                                 ●                ●
                                                                  ●   ●   ●         ●
                                         ●   ●   ●                ●
                                                                  ●   ●
                                                                      ●   ●
                                 ●
                                     ●
                                     ●
                                     ●
                                         ●
                                         ●   ●
                                             ●
                                             ●
                                                 ●
                                                 ●
                                                 ●                ●
                                                                  ●   ●
                                                                      ●   ●       ●●●     ●
                                     ●
                                     ●   ●
                                         ●   ●
                                             ●   ●
                                                 ●                ●
                                                                  ●   ●
                                                                      ●   ●
                                                                          ●
                                     ●   ●   ●
                                             ●   ●                ●
                                                                  ●   ●   ●   ●           ●
                                     ●
                                     ●   ●
                                         ●   ●
                                             ●
                                             ●   ●
                                                 ●                    ●
                                     ●   ●   ●
                                             ●   ●                    ●   ●           ●
                                 ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●                        ●   ●   ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●                        ●
                                                                      ●   ●   ●   ●
                                 ●
                                 ●   ●   ●   ●                            ●   ●
                                     ●   ●   ●                            ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●                                ●
                                                                              ●   ●● ●        ●
                                     ●
                                     ●   ●   ●                            ●   ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●                                ●   ●
                                 ●
                                 ●   ●   ●   ●
                                             ●                            ●         ● ●
                                 ●
                                 ●   ●
                                     ●   ●
                                         ●   ●
                                             ●                            ●
                                 ●   ●   ●
                                         ●
                                         ●   ●
                                             ●                                      ●
         10000                   ●
                                 ●
                                     ●
                                     ●
                                     ●
                                         ●
                                         ●
                                         ●
                                             ●                                    ●
 price




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




         5000




                       50                                    60                               70   80   90
                                                                                   table
Sunday, 25 July 2010
15000



                                                   count
                                                       1000
                                                       2000
         10000                                         3000
 price




                                                       4000
                                                       5000
                                                       6000
                                                       7000

         5000




                       1   2           3   4   5
                               carat
Sunday, 25 July 2010
US baby names
                        US baby names

                       Data manipulation and
                          transformation


Sunday, 25 July 2010
6.4




              6.2




              6.0
 avg_length




                                                                     sex
                                                                           boy
                                                                           girl
              5.8




              5.6




              5.4

                    1880   1900   1920   1940   1960   1980   2000
                                         year
Sunday, 25 July 2010
0.95




        0.90




        0.85

                                                                  sex
 prop




                                                                        boy
        0.80                                                            girl




        0.75




        0.70




                 1880   1900   1920   1940   1960   1980   2000
                                      year
Sunday, 25 July 2010
Polishing your plots



Sunday, 25 July 2010
1. Scales: used to override default
                       perceptual mappings, and tune
                       parameters of axes and legends.

                2. Themes: control presentation of
                       non-data elements.

                3. Saving your work: to include in
                   reports, presentations, etc.



Sunday, 25 July 2010
Sunday, 25 July 2010
ggplot2



Sunday, 25 July 2010
About ggplot2
                       Graphical grammar (domain specific
                       language), based on “The Grammar of
                       Graphics” by Leland Wilkinson.
                       Specify what you want, not how to create it.
                       Many fiddly details taken care of.
                       “Instead of spending time making your graph
                       look pretty, you can focus on creating a graph
                       that bests reveals the messages in your data.”


Sunday, 25 July 2010
Useful resources
                       http://had.co.nz/ggplot2
                       http://had.co.nz/ggplot2/book
                       http://groups.google.com/group/ggplot2
                       http://learnr.wordpress.com
                       http://ggplot2.wik.is



Sunday, 25 July 2010
Learning a new
                       language is hard!
Sunday, 25 July 2010
Scatterplot basics
                install.packages("ggplot2")
                library(ggplot2)

                ?mpg
                head(mpg)
                str(mpg)
                summary(mpg)

                qplot(displ, hwy, data = mpg)



Sunday, 25 July 2010
Scatterplot basics
                install.packages("ggplot2")
                library(ggplot2)

                ?mpg
                head(mpg)
                                    In ggplot2, we
                str(mpg)           always explicitly
                summary(mpg)       specify the data

                qplot(displ, hwy, data = mpg)



Sunday, 25 July 2010
●




                                          ●

                             40


                                      ●
                                      ●

                             35       ●
                                      ●
                                  ●   ●
                                  ●                       ●
                                              ●       ●●

                             30       ●       ●       ●
                                  ●   ●       ●   ●   ●●                              ●
                       hwy




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

                             25       ●                   ●       ●   ●●              ●        ●                                ●                        ●
                                                      ●●      ●●      ●       ●                     ●                                                            ●
                                                          ●       ●                            ●    ●   ●       ●                        ●
                                                              ●       ●       ●                ●                ●
                                                                                               ●                ●

                             20                               ●                                     ●                           ●●
                                                                                  ●        ●        ●           ●●              ●
                                                                                           ●        ●   ●   ●                       ●   ●●
                                                                              ●●                   ●●   ●       ●●      ●   ●       ●    ●       ●           ●
                                                                                                                ●●          ●       ●

                             15                                                                     ●           ●●          ●●●          ●   ●
                                                                                                                                ●                    ●


                                                                                                                    ●



                                              2                       3                             4                   5                        6               7
qplot(displ, hwy, data = mpg)
                          displ
Sunday, 25 July 2010
Additional variables

                       Can display additional variables with
                       aesthetics (like shape, colour, size) or
                       facetting (small multiples displaying
                       different subsets)




Sunday, 25 July 2010
●




                                          ●

                             40


                                      ●
                                      ●

                             35       ●
                                      ●
                                  ● ●                                                                                                              class
                                  ●                   ●
                                              ●   ●●
                                                                                                                                                    ●   2seater
                             30       ● ●         ●                                                                                                 ●   compact
                                  ● ● ● ● ●●                                    ●
                                                                                                                                                    ●   midsize
                       hwy




                                              ●       ●                 ● ●             ●
                                              ● ● ●●                  ● ● ●             ●                                                           ●   minivan
                                      ● ● ● ●●                ● ●●              ●● ● ●                                   ●             ●

                             25       ●               ●       ● ●●              ●       ●                         ●                    ●            ●   pickup
                                                  ●● ●● ●               ●                    ●                                                 ●
                                                      ●       ●                         ● ● ●        ●                   ●
                                                                                                                                                    ●   subcompact
                                                          ●       ●     ●               ●            ●                                              ●   suv
                                                                                        ●            ●

                             20                           ●                                  ●                    ●●
                                                                            ●       ●        ●       ●●           ●
                                                                                    ●        ● ● ●                    ● ●●
                                                                        ●●                  ●● ●     ●●      ● ● ●       ●     ●           ●
                                                                                                     ●●          ● ●

                             15                                                              ●       ●●          ●●●     ● ●
                                                                                                                  ●                ●


                                                                                                         ●



                                          2                       3                         4                5               6                 7
qplot(displ, hwy, colour = class, data = mpg)
                      displ
Sunday, 25 July 2010
●




                                          ●

                             40


                                      ●
                                      ●

                             35       ●
                                      ●
                                  ● ●                                                                                                              class
                                  ●                   ●
                                              ●   ●●
                                                                                                                                                    ●   2seater
                             30       ● ●         ●                                                                                                 ●   compact
                                  ● ● ● ● ●●                                    ●
                                                                                                                                                    ●   midsize
                       hwy




                                              ●       ●                 ● ●             ●
                                              ● ● ●●                  ● ● ●             ●                                                           ●   minivan
                                      ● ● ● ●●                ● ●●              ●● ● ●                                   ●             ●

                             25       ●               ●       ● ●●              ●       ●                         ●                    ●            ●   pickup
                                                  ●● ●● ●               ●                    ●                                                 ●
                                                      ●       ●                         ● ● ●        ●                   ●
                                                                                                                                                    ●   subcompact
                                                          ●       ●     ●               ●            ●                                              ●   suv
                                                                                        ●            ●

                             20                           ●                                  ●                    ●●
                                                                            ●       ●        ●       ●●           ●
                                                                                    ●        ● ● ●                    ● ●●                       Legend chosen and
                                                                                                                                               displayed automatically.
                                                                        ●●                  ●● ●     ●●      ● ● ●       ●     ●           ●
                                                                                                     ●●          ● ●

                             15                                                              ●       ●●          ●●●     ● ●
                                                                                                                  ●                ●


                                                                                                         ●



                                          2                       3                         4                5               6                 7
qplot(displ, hwy, colour = class, data = mpg)
                      displ
Sunday, 25 July 2010
Your turn
                       Experiment with colour, size, and shape
                       aesthetics.
                       What’s the difference between discrete or
                       continuous variables?
                       What happens when you combine
                       multiple aesthetics?



Sunday, 25 July 2010
Discrete        Continuous

                        Rainbow of       Gradient from
          Colour
                         colours          red to blue

                                         Linear mapping
                        Discrete size
               Size                      between radius
                           steps
                                            and value

                       Different shape
           Shape                          Doesn’t work
                           for each

Sunday, 25 July 2010
Faceting

                       Small multiples displaying different
                       subsets of the data.
                       Useful for exploring conditional
                       relationships. Useful for large data.




Sunday, 25 July 2010
Your turn
                       qplot(displ, hwy, data = mpg) +
                       facet_grid(. ~ cyl)
                       qplot(displ, hwy, data = mpg) +
                       facet_grid(drv ~ .)
                       qplot(displ, hwy, data = mpg) +
                       facet_grid(drv ~ cyl)
                       qplot(displ, hwy, data = mpg) +
                       facet_wrap(~ class)


Sunday, 25 July 2010
Summary

                       facet_grid(): 2d grid, rows ~ cols, . for
                       no split
                       facet_wrap(): 1d ribbon wrapped into 2d
                       Scales argument controls whether
                       position scales are fixed or free.



Sunday, 25 July 2010
What’s the                                                                               ●   ●




                             40   problem with                                                                    ●




                                  this plot?                                                     ●   ●
                                                                                                              ●


                             35                                                                           ●
                                                                                                          ●
                                                                                                 ●            ●
                                                                                             ●   ●   ●
                                                                                ●    ●   ●   ●

                             30                                                      ●   ●       ●
                                                                        ●   ●   ●    ●   ●   ●
                       hwy




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

                             25                            ●    ●   ●   ●   ●   ●
                                                           ●    ●   ●   ●
                                                           ●    ●       ●
                                                           ●    ●   ●
                                                           ●

                             20                        ●   ●    ●
                                                   ●   ●   ●
                                               ●   ●   ●   ●
                                           ●   ●   ●   ●   ●
                                           ●   ●   ●

                             15            ●
                                           ●


                                  ●



                                      10                   15                   20                   25               30       35
qplot(cty, hwy, data = mpg)
                       cty
Sunday, 25 July 2010
●   ●




                                                                                                               ●

                             40


                                                                                                           ●
                                                                                                 ●
                                                                                             ●
                                                                                                      ●●
                             35
                                                                                                       ●
                                                                                            ●              ●
                                                                                        ●    ● ●
                                                                                              ●
                                                                            ●      ●
                                                                                  ●● ● ●
                                                                                    ●
                                                                                            ●
                             30                                                  ●
                                                                                    ●
                                                                                      ●
                                                                     ●
                                                                     ●●          ●● ● ●
                                                                                  ●●
                                                                                   ●
                                                                                  ●●
                                                                           ● ● ●● ●●    ●
                                                                      ●     ●● ●
                       hwy




                                                                         ●
                                                                         ●●     ●
                                                                  ● ●● ●● ●    ● ●
                                                                           ● ●  ● ●
                                                                       ● ● ●
                                                                ● ● ●● ●● ●
                                                                       ● ●
                                                                       ● ●         ●
                                                             ●● ●     ●● ●
                                                                ●● ● ● ●
                                                                       ● ●      ●
                                                        ● ●         ● ●● ●●
                                                                          ●
                             25                             ● ●● ● ●     ●    ●
                                                         ●      ● ●
                                                               ● ●● ●
                                                                   ●
                                                         ●        ●
                                                                  ●● ●
                                                           ●● ●
                                                              ●
                                                          ● ●
                                                           ●        ●●
                                                           ● ●
                                                            ●
                                                               ● ●
                                                         ●
                                                           ●
                                                              ●
                             20                     ●● ● ● ●●
                                                     ●
                                                     ●    ● ●
                                                  ●● ●    ●●
                                                           ●
                                               ● ●● ●  ● ●
                                                        ●
                                            ● ● ●● ● ●
                                            ● ●
                                                  ●
                                         ●   ● ● ● ●● ● ●
                                         ●
                                        ●●      ●●●●●● ●
                                                ●
                                                ●●     ●
                                         ●      ● ● ●●●
                                                   ●
                                         ● ● ●●● ●
                                          ●
                             15         ●●
                                        ●●
                                        ●●
                                         ●
                                         ●
                                        ●
                                         ●

                                  ●
                                  ●●
                                  ●●



                                       10               15                 20                    25                30       35
qplot(cty, hwy, data = mpg, geom = "jitter")
                       cty
Sunday, 25 July 2010
●   ●




                                                                                                               ●

                             40


                                                                                                           ●
                                                                                                 ●
                                                                                             ●
                                                                                                      ●●
                             35
                                                                                                       ●
                                                                                            ●              ●
                                                                                        ●    ● ●
                                                                                              ●
                                                                            ●      ●
                                                                                  ●● ● ●
                                                                                    ●
                                                                                            ●
                             30                                                  ●
                                                                                    ●
                                                                                      ●
                                                                     ●
                                                                     ●●          ●● ● ●
                                                                                  ●●
                                                                                   ●
                                                                                  ●●
                                                                           ● ● ●● ●●    ●
                                                                      ●     ●● ●
                       hwy




                                                                         ●
                                                                         ●●     ●
                                                                  ● ●● ●● ●    ● ●
                                                                           ● ●  ● ●
                                                                       ● ● ●
                                                                ● ● ●● ●● ●
                                                                       ● ●
                                                                       ● ●         ●
                                                             ●● ●     ●● ●
                                                                ●● ● ● ●
                                                                       ● ●      ●
                                                        ● ●         ● ●● ●●
                                                                          ●
                             25                             ● ●● ● ●     ●    ●
                                                         ●      ● ●
                                                               ● ●● ●
                                                                   ●
                                                         ●        ●
                                                                  ●● ●
                                                           ●● ●
                                                              ●
                                                          ● ●
                                                           ●        ●●
                                                           ● ●
                                                            ●
                                                               ● ●
                                                         ●
                                                           ●
                                                              ●
                             20                     ●● ● ● ●●
                                                     ●
                                                     ●    ● ●
                                                  ●● ●    ●●
                                                           ●
                                               ● ●● ●  ● ●
                                                        ●
                                            ● ● ●● ● ●
                                            ● ●
                                                  ●
                                         ●   ● ● ● ●● ● ●
                                         ●
                                        ●●      ●●●●●● ●
                                                ●
                                                ●●     ●
                                         ●      ● ● ●●●
                                                   ●
                                         ● ● ●●● ●
                                          ●
                             15         ●●
                                        ●●
                                        ●●
                                         ●
                                         ●
                                        ●
                                         ●

                                  ●
                                  ●●
                                                                                                       geom controls
                                  ●●
                                                                                                       “type” of plot
                                       10               15                 20                    25                30       35
qplot(cty, hwy, data = mpg, geom = "jitter")
                       cty
Sunday, 25 July 2010
●                                       ●




                                                                       ●

       40


                               ●
                                                                       ●

       35                      ●
                                                                       ●
                               ●                                       ●
                                         ●                             ●
                               ●         ●

       30                      ●         ●
                               ●         ●                             ●
 hwy




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

       25              ●       ●         ●                             ●         ●
                       ●       ●         ●         ●                   ●         ●
                       ●       ●         ●         ●                   ●         ●
                                                   ●        ●          ●         ●
                                                   ●                   ●

       20                                                   ●          ●         ●
                                                            ●                    ●
                                                            ●                    ●
                                                   ●        ●                    ●
                                                            ●                    ●

       15                                                   ●                    ●
                                                                                 ●


                                                            ●                    ●



                  2seater   compact   midsize   minivan   pickup   subcompact   suv
qplot(class, hwy, data = mpg)
                          class
Sunday, 25 July 2010
How could        ●                                       ●




              we improve
                                                                       ●

       40


              this plot?       ●
                                                                       ●

       35                      ●
                                                                       ●



               Brainstorm      ●


                               ●
                                         ●
                                         ●
                                                                       ●
                                                                       ●


       30
               for 1 minute.
                               ●         ●
                               ●         ●                             ●
 hwy




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

       25              ●       ●         ●                             ●         ●
                       ●       ●         ●         ●                   ●         ●
                       ●       ●         ●         ●                   ●         ●
                                                   ●        ●          ●         ●
                                                   ●                   ●

       20                                                   ●          ●         ●
                                                            ●                    ●
                                                            ●                    ●
                                                   ●        ●                    ●
                                                            ●                    ●

       15                                                   ●                    ●
                                                                                 ●


                                                            ●                    ●



                  2seater   compact   midsize   minivan   pickup   subcompact   suv
qplot(class, hwy, data = mpg)
                          class
Sunday, 25 July 2010
●           ●




                                                                             ●

       40


                                                                                         ●
                                                                             ●

       35                                                                                ●
                                                                             ●
                                                                             ●           ●
                                                                  ●          ●
                                                                  ●                      ●

       30                                                         ●                      ●
                                                                  ●          ●           ●
 hwy




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

       25                    ●                      ●             ●          ●           ●
                             ●       ●              ●             ●          ●           ●
                             ●       ●              ●             ●          ●           ●
                       ●     ●       ●                                       ●
                                     ●                                       ●

       20              ●     ●                                               ●
                       ●     ●
                       ●     ●
                       ●     ●       ●
                       ●     ●

       15              ●     ●
                             ●


                       ●     ●



                   pickup   suv   minivan        2seater       midsize   subcompact   compact
                                         reorder(class, hwy)
Sunday, 25 July 2010
●           ●




                                                                        ●

       40


                                                                                    ●
                                                                        ●

       35                                                                           ●
                                                                        ●
                                                                        ●           ●
                                                             ●          ●
                                                             ●                      ●

       30                                                    ●                      ●
                                                             ●          ●           ●
 hwy




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

       25                     ●                    ●         ●          ●           ●
                              ●          ●         ●         ●          ●           ●
                              ●          ●         ●         ●          ●           ●
                       ●      ●          ●                              ●
                                         ●                              ●

       20              ●      ●                                         ●
                       ●      ●
                       ●      ●
                       ●      ●          ●
                       ●      ●

       15              ●      ●

                  Incredibly useful
                              ●



                     technique!
                       ●      ●



                   pickup    suv      minivan   2seater   midsize   subcompact   compact
qplot(reorder(class, hwy), hwy, data = mpg)
                      reorder(class, hwy)
Sunday, 25 July 2010
●                            ●




                                                                                                                                                                        ●

       40

                                                                                                                                                                                    ●

                                                                                                                                                            ●
                                                                                                                                           ●
                                                                                                                                                                                         ●    ●
       35
                                                                                                                                                   ●
                                                                                                                                                                                ●
                                                                                                                                                                    ●
                                                                                                                         ●                 ●       ●
                                                                                                                    ●                                           ●
                                                                                                                    ●                                                                   ●          ●
                                                                                                                     ●       ●                                                           ●
                                                                                                                                                                                ●
       30                                                                                                                     ●   ●                                                               ●

                                                                                                                          ● ● ●       ●●           ●       ●●                  ● ● ● ●
                                                                                                                                                                               ●
                                                                                                                                                                            ● ●●
                                                                                                                                                                             ●
                                                                                                                                           ●                        ●       ●●  ●
                                                                                                                              ●                                     ●             ●
                                                                                                                         ● ● ●                 ●
 hwy




                                                                                                                            ●                                               ●                ● ●
                                                                                                                                                                                             ● ●
                                                                    ●                                                ● ●     ●●                                         ●   ●
                                                        ●                                               ●             ●● ●
                                                                                                                      ●        ●
                                                                                                                                 ●         ●●              ●●                   ●        ●
                                                                                                            ●       ● ●
                                                                                                                    ● ●
                                                                                                                    ●         ●●●          ● ●                                   ●        ●     ●●
                                                                                                                    ●                                               ●                          ●
                                                                                                                                                   ●                            ● ●
       25                                           ●       ●                                     ●                          ● ●●
                                                                                                                                                                                         ●●
                                                                                                                                                                                         ●
                                                                                                                                                                                               ●
                                                                                                                                                                                               ●
                                                ●                            ●                ●                                                    ●                 ●              ●    ●
                                                                                                                ●                              ●
                                                                                     ●●                              ●                                              ●
                                                                                                            ●                ●
                                                                ●                         ●                                                    ●                                               ●
                                                                         ●
                                   ●                    ●                                 ●                                                                     ●
                                                                                 ●        ●                                                                      ●
                                                                         ●
                                                                                                                                                       ●
                       ●       ●
       20                          ●
                                           ●     ●
                                               ● ●          ●●      ●                                                                                           ●
                    ●                                       ●●      ●●
                  ●●       ●                ●                       ●●
                                                  ●      ●
                                       ●      ●
                    ●                         ●● ● ●●●
                     ●     ●
                           ●    ●          ● ● ● ●●
                       ●  ●     ●          ●●● ●     ●     ●                     ●
                   ● ● ●
                    ●                          ●● ●     ●  ●
                   ●● ●        ●                       ● ●
                         ●
                  ●                           ● ●
       15         ● ●        ●               ● ●        ● ●
                                                 ●
                                                 ●


                   ●           ●   ●                 ●          ●




                       pickup                        suv                     minivan                  2seater            midsize           subcompact                           compact
qplot(reorder(class, hwy),reorder(class, hwy) = mpg, geom = "jitter")
                            hwy, data
Sunday, 25 July 2010
●           ●




                                                                        ●

       40


                                                                                    ●


       35                                                                           ●




       30
 hwy




                                 ●
                                 ●

       25                        ●
                                 ●
                                 ●
                         ●


       20


                                         ●


       15


                         ●       ●



                       pickup   suv   minivan   2seater   midsize   subcompact   compact
qplot(reorder(class, hwy), hwy, data hwy)mpg, geom = "boxplot")
                           reorder(class, =
Sunday, 25 July 2010
●
                                                                                                                                                       ●                            ●
                                                                                                                                                                                    ●




                                                                                                                                                       ●           ●

       40


                                                                                                                                                                                    ● ●
                                                                                                                                                   ● ●

       35                                                                                                                                                                  ●        ● ●
                                                                                                                                                       ●
                                                                                                                                                                                           ●
                                                                                                                                                                   ●
                                                                                                                                ●             ● ● ●
                                                                                                               ●                                                           ●                 ●
                                                                                                                       ●        ●●                                                       ●
       30                                                                                                      ●           ●    ●                                              ●
                                                                                                                                                                              ●●
                                                                                                                                                                                        ●●
                                                                                                                                ●
                                                                                                                               ● ●                                 ●                      ●●
                                                                                                                                          ●   ●●●                           ●● ●●       ● ●●
                                                                                                                                   ●
                                                                                                                       ●       ●               ●                           ●
 hwy




                                                                                                                                ●
                                                                                                                                ●                              ●
                                                                                                               ●                 ●             ●                                    ●● ●●
                                                         ● ●                                                                                                               ●           ●
                                                                                                                   ●     ●            ●                                                 ●
                                                 ●                                                             ●        ●
                                                                                                                        ● ●          ●    ●   ●●●                  ●              ●●
                                                         ●                                             ●               ●●            ●
                                                                                                                                    ●●                                        ●      ●●
                                                                                                                                                                                      ●
                                                                                               ●                   ●    ●
                                                                                                                        ●           ●       ●   ●●                              ●
                                                                                                           ●
       25                                    ●           ●         ●                                               ●                ●                                        ● ●● ●
                                                                                                                                     ●
                                                                                                                                     ●                             ●       ●            ●
                                                         ●             ●                                   ●                              ●                ●
                                                                                                                                                           ●                         ●●
                                                                       ●       ●   ●                                   ●                                       ●
                                                               ●                                                                                                             ●
                                             ●           ●                                         ●               ●                                       ●
                                                                       ●                   ●
                           ●                                               ●
                           ●                             ●                 ●
                                                                                           ●                                                       ●           ●
                                                                                                                                                                       ●
                                                                                       ●
                                   ●          ●
       20          ●       ●               ●● ●
                                            ●        ●
                                                     ●                                                                                                 ●
                                                    ●● ● ●●
                  ●                ●● ●    ● ●       ●    ●
                                    ●
                                    ●          ●   ● ●  ●
                                                 ●
                                                 ●     ●
                              ●                 ●      ●                       ●
                  ● ●●
                     ●     ● ●●            ●●●●● ● ● ●
                                             ●
                                              ● ●● ● ● ● ●
                                                 ●                             ●
                       ● ● ●                           ●
                        ●                      ●
                   ●   ●
                       ●    ●
                          ●                  ●       ●
       15          ●     ●    ●              ●        ●●           ●
                                                     ●
                                                     ●

                                   ●             ●
                       ●       ●                         ●
                                                         ●
                                       ●



qplot(reorder(class,minivan
       pickup suv
                      hwy), 2seater data = subcompact
                                  hwy, midsize mpg,                                                                                                                            compact
  geom = c("jitter", "boxplot"))
                         reorder(class, hwy)
Sunday, 25 July 2010
Your turn

                       Read the help for reorder. Redraw the
                       previously plots with class ordered by
                       median hwy.
                       How would you put the jittered points on
                       top of the boxplots?




Sunday, 25 July 2010
Aside: coding strategy

                       At the end of each interactive session, you
                       want a summary of everything you did. Two
                       options:
                       1. Save everything you did with savehistory()
                       then remove the unimportant bits.
                       2. Build up the important bits as you go.
                       (this is how I work)


Sunday, 25 July 2010
Sunday, 25 July 2010
This work is licensed under the Creative
       Commons Attribution-Noncommercial 3.0 United
       States License. To view a copy of this license,
       visit http://creativecommons.org/licenses/by-nc/
       3.0/us/ or send a letter to Creative Commons,
       171 Second Street, Suite 300, San Francisco,
       California, 94105, USA.




Sunday, 25 July 2010

More Related Content

Similar to 1 basics

Over Visie, Missie En Strategie
Over Visie, Missie En StrategieOver Visie, Missie En Strategie
Over Visie, Missie En Strategie
Guus Vos
 
About Vision, Mission And Strategy
About Vision, Mission And StrategyAbout Vision, Mission And Strategy
About Vision, Mission And Strategy
Guus Vos
 
Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1
rusersla
 
Modul mulus bahagian c sjk (modul murid)
Modul mulus bahagian c sjk (modul murid)Modul mulus bahagian c sjk (modul murid)
Modul mulus bahagian c sjk (modul murid)
Anparasu
 
Modul mulus bahagian c sjk (modul guru)
Modul mulus bahagian c sjk (modul guru)Modul mulus bahagian c sjk (modul guru)
Modul mulus bahagian c sjk (modul guru)
Anparasu
 
Modul mulus bahagian c sk (modul murid)
Modul mulus bahagian c sk (modul murid)Modul mulus bahagian c sk (modul murid)
Modul mulus bahagian c sk (modul murid)
Anparasu
 
Modul mulus bahagian c sk (modul guru)
Modul mulus bahagian c sk (modul guru)Modul mulus bahagian c sk (modul guru)
Modul mulus bahagian c sk (modul guru)
Anparasu
 
正誤表 p39
正誤表 p39正誤表 p39
正誤表 p39
zafiro555
 
Fairisle knitting
Fairisle knittingFairisle knitting
Fairisle knitting
zafiro555
 
研修企画書11 12term voda-カヤック
研修企画書11 12term voda-カヤック研修企画書11 12term voda-カヤック
研修企画書11 12term voda-カヤック
aiesecsfc_icx2011
 
研修企画書11-12term voda-カヤック
研修企画書11-12term voda-カヤック研修企画書11-12term voda-カヤック
研修企画書11-12term voda-カヤック
aiesecsfc_icx2011
 

Similar to 1 basics (20)

02 large
02 large02 large
02 large
 
13 Bivariate
13 Bivariate13 Bivariate
13 Bivariate
 
01 intro
01 intro01 intro
01 intro
 
Over Visie, Missie En Strategie
Over Visie, Missie En StrategieOver Visie, Missie En Strategie
Over Visie, Missie En Strategie
 
About Vision, Mission And Strategy
About Vision, Mission And StrategyAbout Vision, Mission And Strategy
About Vision, Mission And Strategy
 
Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1Los Angeles R users group - July 12 2011 - Part 1
Los Angeles R users group - July 12 2011 - Part 1
 
Modul mulus bahagian c sjk (modul murid)
Modul mulus bahagian c sjk (modul murid)Modul mulus bahagian c sjk (modul murid)
Modul mulus bahagian c sjk (modul murid)
 
Modul mulus bahagian c sjk (modul guru)
Modul mulus bahagian c sjk (modul guru)Modul mulus bahagian c sjk (modul guru)
Modul mulus bahagian c sjk (modul guru)
 
Modul mulus bahagian c sk (modul murid)
Modul mulus bahagian c sk (modul murid)Modul mulus bahagian c sk (modul murid)
Modul mulus bahagian c sk (modul murid)
 
Modul mulus bahagian c sk (modul guru)
Modul mulus bahagian c sk (modul guru)Modul mulus bahagian c sk (modul guru)
Modul mulus bahagian c sk (modul guru)
 
17 Sampling Dist
17 Sampling Dist17 Sampling Dist
17 Sampling Dist
 
21 Ml
21 Ml21 Ml
21 Ml
 
08 Continuous
08 Continuous08 Continuous
08 Continuous
 
08 Continuous
08 Continuous08 Continuous
08 Continuous
 
正誤表 p39
正誤表 p39正誤表 p39
正誤表 p39
 
Fairisle knitting
Fairisle knittingFairisle knitting
Fairisle knitting
 
研修企画書11 12term voda-カヤック
研修企画書11 12term voda-カヤック研修企画書11 12term voda-カヤック
研修企画書11 12term voda-カヤック
 
How People Use Facebook -- And Why It Matters
How People Use Facebook -- And Why It MattersHow People Use Facebook -- And Why It Matters
How People Use Facebook -- And Why It Matters
 
研修企画書11-12term voda-カヤック
研修企画書11-12term voda-カヤック研修企画書11-12term voda-カヤック
研修企画書11-12term voda-カヤック
 
Barley environmental association - Plant & Animal Genome 2018
Barley environmental association - Plant & Animal Genome 2018Barley environmental association - Plant & Animal Genome 2018
Barley environmental association - Plant & Animal Genome 2018
 

More from Ajay Ohri

More from Ajay Ohri (20)

Introduction to R ajay Ohri
Introduction to R ajay OhriIntroduction to R ajay Ohri
Introduction to R ajay Ohri
 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
 
Social Media and Fake News in the 2016 Election
Social Media and Fake News in the 2016 ElectionSocial Media and Fake News in the 2016 Election
Social Media and Fake News in the 2016 Election
 
Pyspark
PysparkPyspark
Pyspark
 
Download Python for R Users pdf for free
Download Python for R Users pdf for freeDownload Python for R Users pdf for free
Download Python for R Users pdf for free
 
Install spark on_windows10
Install spark on_windows10Install spark on_windows10
Install spark on_windows10
 
Ajay ohri Resume
Ajay ohri ResumeAjay ohri Resume
Ajay ohri Resume
 
Statistics for data scientists
Statistics for  data scientistsStatistics for  data scientists
Statistics for data scientists
 
National seminar on emergence of internet of things (io t) trends and challe...
National seminar on emergence of internet of things (io t)  trends and challe...National seminar on emergence of internet of things (io t)  trends and challe...
National seminar on emergence of internet of things (io t) trends and challe...
 
Tools and techniques for data science
Tools and techniques for data scienceTools and techniques for data science
Tools and techniques for data science
 
How Big Data ,Cloud Computing ,Data Science can help business
How Big Data ,Cloud Computing ,Data Science can help businessHow Big Data ,Cloud Computing ,Data Science can help business
How Big Data ,Cloud Computing ,Data Science can help business
 
Training in Analytics and Data Science
Training in Analytics and Data ScienceTraining in Analytics and Data Science
Training in Analytics and Data Science
 
Tradecraft
Tradecraft   Tradecraft
Tradecraft
 
Software Testing for Data Scientists
Software Testing for Data ScientistsSoftware Testing for Data Scientists
Software Testing for Data Scientists
 
Craps
CrapsCraps
Craps
 
A Data Science Tutorial in Python
A Data Science Tutorial in PythonA Data Science Tutorial in Python
A Data Science Tutorial in Python
 
How does cryptography work? by Jeroen Ooms
How does cryptography work?  by Jeroen OomsHow does cryptography work?  by Jeroen Ooms
How does cryptography work? by Jeroen Ooms
 
Using R for Social Media and Sports Analytics
Using R for Social Media and Sports AnalyticsUsing R for Social Media and Sports Analytics
Using R for Social Media and Sports Analytics
 
Kush stats alpha
Kush stats alpha Kush stats alpha
Kush stats alpha
 
Analyze this
Analyze thisAnalyze this
Analyze this
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

1 basics

  • 1. Visualisation in R Hadley Wickham Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University July 2010 Sunday, 25 July 2010
  • 2. HE LL O my name is Hadley Sunday, 25 July 2010
  • 3. http://had.co.nz/ vanderbilt-vis Sunday, 25 July 2010
  • 4. 1. Preview of today 2. About ggplot2 3. More resources 4. Diving in Sunday, 25 July 2010
  • 5. Fuel economy Basic graphics Sunday, 25 July 2010
  • 6. ● 40 ● ● 35 ● ● ● ● ● ● ● ●● 30 ● ● ● ● ● ● ● ●● ● hwy ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ●● ● ● ● ● 25 ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ●● ● ● 15 ● ●● ●●● ● ● ● ● ● 2 3 4 5 6 7 displ Sunday, 25 July 2010
  • 7. ● 40 ● ● 35 ● ● ● ● class ● ● ● ●● ● 2seater 30 ● ● ● ● compact ● ● ● ● ●● ● ● midsize hwy ● ● ● ● ● ● ● ●● ● ● ● ● ● minivan ● ● ● ●● ● ●● ●● ● ● ● ● 25 ● ● ● ●● ● ● ● ● ● pickup ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● subcompact ● ● ● ● ● ● suv ● ● 20 ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ●● ● ● 15 ● ●● ●●● ● ● ● ● ● 2 3 4 5 6 7 displ Sunday, 25 July 2010
  • 8. Diamond prices Displaying large data Sunday, 25 July 2010
  • 9. 4000 3000 count 2000 1000 0 56 58 60 62 64 66 68 70 depth Sunday, 25 July 2010
  • 10. ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15000 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 10000 ● ● ● ● ● ● ● ● ● ● price ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 5000 50 60 70 80 90 table Sunday, 25 July 2010
  • 11. 15000 count 1000 2000 10000 3000 price 4000 5000 6000 7000 5000 1 2 3 4 5 carat Sunday, 25 July 2010
  • 12. US baby names US baby names Data manipulation and transformation Sunday, 25 July 2010
  • 13. 6.4 6.2 6.0 avg_length sex boy girl 5.8 5.6 5.4 1880 1900 1920 1940 1960 1980 2000 year Sunday, 25 July 2010
  • 14. 0.95 0.90 0.85 sex prop boy 0.80 girl 0.75 0.70 1880 1900 1920 1940 1960 1980 2000 year Sunday, 25 July 2010
  • 16. 1. Scales: used to override default perceptual mappings, and tune parameters of axes and legends. 2. Themes: control presentation of non-data elements. 3. Saving your work: to include in reports, presentations, etc. Sunday, 25 July 2010
  • 19. About ggplot2 Graphical grammar (domain specific language), based on “The Grammar of Graphics” by Leland Wilkinson. Specify what you want, not how to create it. Many fiddly details taken care of. “Instead of spending time making your graph look pretty, you can focus on creating a graph that bests reveals the messages in your data.” Sunday, 25 July 2010
  • 20. Useful resources http://had.co.nz/ggplot2 http://had.co.nz/ggplot2/book http://groups.google.com/group/ggplot2 http://learnr.wordpress.com http://ggplot2.wik.is Sunday, 25 July 2010
  • 21. Learning a new language is hard! Sunday, 25 July 2010
  • 22. Scatterplot basics install.packages("ggplot2") library(ggplot2) ?mpg head(mpg) str(mpg) summary(mpg) qplot(displ, hwy, data = mpg) Sunday, 25 July 2010
  • 23. Scatterplot basics install.packages("ggplot2") library(ggplot2) ?mpg head(mpg) In ggplot2, we str(mpg) always explicitly summary(mpg) specify the data qplot(displ, hwy, data = mpg) Sunday, 25 July 2010
  • 24. ● 40 ● ● 35 ● ● ● ● ● ● ● ●● 30 ● ● ● ● ● ● ● ●● ● hwy ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ●● ● ●● ●● ● ● ● ● 25 ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ●● ● ● 15 ● ●● ●●● ● ● ● ● ● 2 3 4 5 6 7 qplot(displ, hwy, data = mpg) displ Sunday, 25 July 2010
  • 25. Additional variables Can display additional variables with aesthetics (like shape, colour, size) or facetting (small multiples displaying different subsets) Sunday, 25 July 2010
  • 26. ● 40 ● ● 35 ● ● ● ● class ● ● ● ●● ● 2seater 30 ● ● ● ● compact ● ● ● ● ●● ● ● midsize hwy ● ● ● ● ● ● ● ●● ● ● ● ● ● minivan ● ● ● ●● ● ●● ●● ● ● ● ● 25 ● ● ● ●● ● ● ● ● ● pickup ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● subcompact ● ● ● ● ● ● suv ● ● 20 ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● ●● ●● ● ●● ● ● ● ● ● ● ●● ● ● 15 ● ●● ●●● ● ● ● ● ● 2 3 4 5 6 7 qplot(displ, hwy, colour = class, data = mpg) displ Sunday, 25 July 2010
  • 27. ● 40 ● ● 35 ● ● ● ● class ● ● ● ●● ● 2seater 30 ● ● ● ● compact ● ● ● ● ●● ● ● midsize hwy ● ● ● ● ● ● ● ●● ● ● ● ● ● minivan ● ● ● ●● ● ●● ●● ● ● ● ● 25 ● ● ● ●● ● ● ● ● ● pickup ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● subcompact ● ● ● ● ● ● suv ● ● 20 ● ● ●● ● ● ● ●● ● ● ● ● ● ● ●● Legend chosen and displayed automatically. ●● ●● ● ●● ● ● ● ● ● ● ●● ● ● 15 ● ●● ●●● ● ● ● ● ● 2 3 4 5 6 7 qplot(displ, hwy, colour = class, data = mpg) displ Sunday, 25 July 2010
  • 28. Your turn Experiment with colour, size, and shape aesthetics. What’s the difference between discrete or continuous variables? What happens when you combine multiple aesthetics? Sunday, 25 July 2010
  • 29. Discrete Continuous Rainbow of Gradient from Colour colours red to blue Linear mapping Discrete size Size between radius steps and value Different shape Shape Doesn’t work for each Sunday, 25 July 2010
  • 30. Faceting Small multiples displaying different subsets of the data. Useful for exploring conditional relationships. Useful for large data. Sunday, 25 July 2010
  • 31. Your turn qplot(displ, hwy, data = mpg) + facet_grid(. ~ cyl) qplot(displ, hwy, data = mpg) + facet_grid(drv ~ .) qplot(displ, hwy, data = mpg) + facet_grid(drv ~ cyl) qplot(displ, hwy, data = mpg) + facet_wrap(~ class) Sunday, 25 July 2010
  • 32. Summary facet_grid(): 2d grid, rows ~ cols, . for no split facet_wrap(): 1d ribbon wrapped into 2d Scales argument controls whether position scales are fixed or free. Sunday, 25 July 2010
  • 33. What’s the ● ● 40 problem with ● this plot? ● ● ● 35 ● ● ● ● ● ● ● ● ● ● ● 30 ● ● ● ● ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● 10 15 20 25 30 35 qplot(cty, hwy, data = mpg) cty Sunday, 25 July 2010
  • 34. ● ● 40 ● ● ● ●● 35 ● ● ● ● ● ● ● ● ● ●● ● ● ● ● 30 ● ● ● ● ●● ●● ● ● ●● ● ●● ● ● ●● ●● ● ● ●● ● hwy ● ●● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ●● ●● ● 25 ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● 20 ●● ● ● ●● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●●●●● ● ● ●● ● ● ● ● ●●● ● ● ● ●●● ● ● 15 ●● ●● ●● ● ● ● ● ● ●● ●● 10 15 20 25 30 35 qplot(cty, hwy, data = mpg, geom = "jitter") cty Sunday, 25 July 2010
  • 35. ● ● 40 ● ● ● ●● 35 ● ● ● ● ● ● ● ● ● ●● ● ● ● ● 30 ● ● ● ● ●● ●● ● ● ●● ● ●● ● ● ●● ●● ● ● ●● ● hwy ● ●● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ●● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ●● ●● ● 25 ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● 20 ●● ● ● ●● ● ● ● ● ●● ● ●● ● ● ●● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ●●●●●● ● ● ●● ● ● ● ● ●●● ● ● ● ●●● ● ● 15 ●● ●● ●● ● ● ● ● ● ●● geom controls ●● “type” of plot 10 15 20 25 30 35 qplot(cty, hwy, data = mpg, geom = "jitter") cty Sunday, 25 July 2010
  • 36. ● ● 40 ● ● 35 ● ● ● ● ● ● ● ● 30 ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● 2seater compact midsize minivan pickup subcompact suv qplot(class, hwy, data = mpg) class Sunday, 25 July 2010
  • 37. How could ● ● we improve ● 40 this plot? ● ● 35 ● ● Brainstorm ● ● ● ● ● ● 30 for 1 minute. ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● 2seater compact midsize minivan pickup subcompact suv qplot(class, hwy, data = mpg) class Sunday, 25 July 2010
  • 38. ● ● 40 ● ● 35 ● ● ● ● ● ● ● ● 30 ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● pickup suv minivan 2seater midsize subcompact compact reorder(class, hwy) Sunday, 25 July 2010
  • 39. ● ● 40 ● ● 35 ● ● ● ● ● ● ● ● 30 ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● Incredibly useful ● technique! ● ● pickup suv minivan 2seater midsize subcompact compact qplot(reorder(class, hwy), hwy, data = mpg) reorder(class, hwy) Sunday, 25 July 2010
  • 40. ● ● 40 ● ● ● ● ● 35 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 30 ● ● ● ● ● ● ●● ● ●● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ●●● ● ● ● ● ●● ● ● ● ● ● ● 25 ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ●● ● ● ● ●● ●● ●● ● ● ●● ● ● ● ● ● ●● ● ●●● ● ● ● ● ● ● ● ●● ● ● ● ●●● ● ● ● ● ● ● ● ● ●● ● ● ● ●● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● ● ● ● ● ● ● ● ● ● pickup suv minivan 2seater midsize subcompact compact qplot(reorder(class, hwy),reorder(class, hwy) = mpg, geom = "jitter") hwy, data Sunday, 25 July 2010
  • 41. ● ● 40 ● 35 ● 30 hwy ● ● 25 ● ● ● ● 20 ● 15 ● ● pickup suv minivan 2seater midsize subcompact compact qplot(reorder(class, hwy), hwy, data hwy)mpg, geom = "boxplot") reorder(class, = Sunday, 25 July 2010
  • 42. ● ● ● ● ● 40 ● ● ● ● 35 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● 30 ● ● ● ● ●● ●● ● ● ● ● ●● ● ●●● ●● ●● ● ●● ● ● ● ● ● hwy ● ● ● ● ● ● ●● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●●● ● ●● ● ● ●● ● ●● ● ●● ● ● ● ● ● ● ● ●● ● ● 25 ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ●● ● ● ● ● ● ●● ● ●● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ●●●●● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● qplot(reorder(class,minivan pickup suv hwy), 2seater data = subcompact hwy, midsize mpg, compact geom = c("jitter", "boxplot")) reorder(class, hwy) Sunday, 25 July 2010
  • 43. Your turn Read the help for reorder. Redraw the previously plots with class ordered by median hwy. How would you put the jittered points on top of the boxplots? Sunday, 25 July 2010
  • 44. Aside: coding strategy At the end of each interactive session, you want a summary of everything you did. Two options: 1. Save everything you did with savehistory() then remove the unimportant bits. 2. Build up the important bits as you go. (this is how I work) Sunday, 25 July 2010
  • 46. This work is licensed under the Creative Commons Attribution-Noncommercial 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/ 3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. Sunday, 25 July 2010