SlideShare ist ein Scribd-Unternehmen logo
1 von 54
Downloaden Sie, um offline zu lesen
ggplot2:
                            A backstage tour


                            Hadley Wickham
                            Assistant Professor /
                            Dobelman Family Junior Chair
                            Department of Statistics
                            Rice University




                            February 2012
Wednesday, February 8, 12
1. Why ggplot2?
                2. Sneak peek and new features
                3. Best practices
                4. Questions




Wednesday, February 8, 12
Poll: What graphics
                          system are you
                         currently using?


Wednesday, February 8, 12
Why ggplot2?


Wednesday, February 8, 12
2004
                                          WHC
                                                  ●

                                           02H    ●

                                           02M    ●

                                           12H    ●




                          0.2




                          0.1




                          0.0
                   whc




                         −0.1




                         −0.2




                         −0.3




                                20   40               60   80
                                            day




Wednesday, February 8, 12
Wednesday, February 8, 12
“Nothing is as practical as a good theory”
              —Kurt Lewin


              “[A good model] will bring together in a
              coherent way things that previously
              appeared unrelated and which also will
              provide a basis for dealing systematically
              with new situations”
              —David Cox



Wednesday, February 8, 12
A plot is made up of multiple layers.
     A layer consists of data, a set of
     mappings between variables and
     aesthetics, a geometric object and a
     statistical transformation
     Scales control the details of the mapping.
     All components are independent and
     reusable.

Wednesday, February 8, 12
Interesting ggplot example
                            Layered grammar + ggplot2




                                               James Cheshire, http://bit.ly/xqHhAs
Wednesday, February 8, 12
Charlotte Wickham, http://cwick.co.nz/
Wednesday, February 8, 12
David B Sparks, http://bit.ly/hn54NW
Wednesday, February 8, 12
Claudia Beleites, http://bit.ly/yNqlpz
Wednesday, February 8, 12
Poll: What resources are
                most helpful to you when
                improving your R skills?


Wednesday, February 8, 12
Learning ggplot2
                    ggplot2 mailing list
                    http://groups.google.com/group/ggplot2
                    stackoverflow
                    http://stackoverflow.com/tags/ggplot2
                    Lattice to ggplot2 conversion
                    http://learnr.wordpress.com/?s=lattice
                    Cookbook for common graphics
                    http://wiki.stdout.org/rcookbook/Graphs/
                    ggplot2 book
                    http://amzn.com/0387981403



Wednesday, February 8, 12
Sneak
                             peek
Wednesday, February 8, 12
Poll: Why do you use
                     visualisation?


Wednesday, February 8, 12
# Getting started


     # To get the CRAN version
     install.packages("ggplot2")


     # To get the development version
     install.packages("devtools")
     library(devtools)
     dev_mode() # don't overwrite your existing install
     install_github("ggplot2")




Wednesday, February 8, 12
Development version




                       CRAN version

Wednesday, February 8, 12
New geoms to deal with overplotting
       45
                               ●                                       ●




       40
            (by Winston Chang)                                         ●




                               ●
                                                                       ●

       35                      ●
                                                                       ●
                               ●                                       ●
                                         ●                             ●
                               ●         ●

       30                      ●         ●
                               ●         ●                             ●
 hwy




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

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

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

       15                                                   ●                    ●
                                                                                 ●


                                                            ●                    ●




                  2seater   compact   midsize   minivan   pickup   subcompact   suv
                                                class
Wednesday, February 8, 12
New geoms to deal with overplotting
       45
                               ●                                       ●




       40
            (by Winston Chang)                                         ●




                               ●
                                                                       ●

       35                      ●
                                                                       ●
                               ●                                       ●
                                         ●                             ●
                               ●         ●

       30                      ●         ●
                               ●         ●                             ●
 hwy




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

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

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

       15                                                   ●                    ●
                                                                                 ●


                                                            ●                    ●




                  2seater   compact   midsize   minivan   pickup   subcompact   suv
qplot(class, hwy, data = mpg)
                            class
Wednesday, February 8, 12
45                                                                                                                     ●
                                         ●



                                                                                                                                              ●

       40


                                     ●
                                                                                                                                          ● ●
       35                                     ●   ●
                                                                                                                                      ●
                                          ●                                                                                                       ●

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




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

                                                                                                                                                                              ●●

                                                                                                                  ●    ●                                             ●         ●
                                                                                                                       ●




                     2seater     compact                  midsize                 minivan                         pickup      subcompact                                     suv
qplot(class, hwy, data = mpg, geom = "jitter")
                            class
Wednesday, February 8, 12
45




       40




       35




       30
 hwy




       25




       20




       15




                  2seater   compact   midsize   minivan   pickup   subcompact   suv
qplot(class, hwy, data = mpg, geom = "violin")
                            class
Wednesday, February 8, 12
45
                              ●                                       ●

                                                                      ●
       40


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




                             ●    ●●●●                           ●●
                         ●●●●●●● ●●●●●                           ●        ●
                    ●●   ●●●●●●●
                             ●●●●●●●●●●●●●●                  ●●●●●●●●     ●
       25            ●   ●●●●●●● ●●●●                            ●       ●●
                     ●      ●●       ●   ●●●●                   ●●●●      ●
                     ●       ●       ●     ●●                    ●        ●
                                          ●●●             ●      ●●       ●
                                           ●                     ●
       20                                               ●●●      ●    ●●●●●●●
                                                       ●●●●          ●●●●●●●●●
                                                         ●●          ●●●●●●●●
                                                  ● ●●●●●●●●●●●
                                                              ●●●●●●●●●●●●●●●●
                                                       ●●●●●             ●●
       15                                              ●●●●            ●●●●●●
                                                                         ●●
                                                        ●●●              ●●
       10
                  2seater   compact   midsize   minivan   pickup   subcompact   suv
                                                class
Wednesday, February 8, 12
45
                            ●                           ●

                                                        ●
       40


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




                             ●    ●●●●                   ●●
                         ●●●●●●● ●●●●●                   ●        ●
                    ●●   ●●●●●●●
                             ●●●●●●●●●●●●●●          ●●●●●●●●     ●
       25            ●   ●●●●●●● ●●●●                    ●       ●●
                     ●      ●●       ●   ●●●●           ●●●●      ●
                     ●       ●       ●     ●●            ●        ●
                                          ●●●      ●     ●●       ●
                                           ●             ●
       20                                        ●●●     ●    ●●●●●●●
                                                ●●●●         ●●●●●●●●●
                                                  ●●         ●●●●●●●●
                                          ● ●●●●●●●●●●●
                                                      ●●●●●●●●●●●●●●●●
                                               ●●●●●             ●●
       15                                       ●●●●           ●●●●●●
                                                                 ●●
qplot(class, hwy, data =              mpg, geom = "dotplot", ●●
                                                 ●●●
  stackdir = "center", binaxis = "y",
  10
      2seater compact midsize minivan pickup         subcompact   suv
  stackratio = 1, binwidth = class
                                1)
Wednesday, February 8, 12
1.0
            Better legends                             ●                                                                 ●
                                                                                                                                                       ●                                                   ●




            (by Kohske Takahashi)         ●            ●
                                                                           ●                     ●                                                 ●
                                                                                                                                                       ●
                                                                                                                                                                           ●
                                                                                                                                                                                                   ●
                                                  ●
                                                                                                                                                                                       ●
                                                                                                                                                                               ●
                              ●                                                                                                                                                                        ●
     0.8                                                           ●       ●                                                          ●

                                                           ●                                                                                  ●
                                                                                                                                              ●            ●
                                                                                                                             ●
                                                                                                                                                                                                   ● ●
                                                                                                         ●                                                         ●                               ●
                                                                               ●
                                                                                                                                                                                           ●●
                                                                                                                                                                                            ●
                                                       ●
                                                                                                                                 ●●                                        ●
                                                                                                                 ●
                                                                                                                                                                       ●
                                                                                                                                                                                                                   colour
                  ●
     0.6                                               ●
                                                                                                                                                       ●                                                            ●   0.5
                                                                                                             ●
                                                               ●                        ●                                                                                              ●
                                              ●                                                                              ●
                                                                                                                                                                   ●
                                                                                                                                                                                                               ●    ●   1.0
                                                                                                                                                   ●
 y




                                                                                                                                                                                                           ●
                                  ●                                                                                                                                                                                 ●   1.5
                                  ●
                                                                                                                                                                                           ●                        ●   2.0
                                                           ● ●
                                                                                                                                  ●                                                                ●
                                                                                                     ●
     0.4                                                                                                             ●                                                                                              ●   2.5
                 ●                                     ●
                                                               ●
                          ●
                                                                                                                                          ●                                        ●
                                      ●
                      ●

                      ●                                ●
                                                                                                                                              ●
                                                      ●                        ●
     0.2                                  ●
                                              ●                                    ●                                                                                       ●
                                                   ●                                                         ●
                                                  ●                                ●
                                      ●                                                      ●                                                                                                 ●
                                                                                                                                                       ●
                  ●                                                                                  ●           ●
                                                                                                                                  ●            ●
                                                                                                                                              ●
                          ●                                    ●                                                                                               ●
                                                                                                             ●                                                                 ● ●
                                                                       ●
     0.0
                0.0                           0.2                                      0.4                               0.6                                       0.8                                     1.0
                                                                                                     x
Wednesday, February 8, 12
1.0
            Better legends                             ●                                                                 ●
                                                                                                                                                       ●                                                   ●




            (by Kohske Takahashi)         ●            ●
                                                                           ●                     ●                                                 ●
                                                                                                                                                       ●
                                                                                                                                                                           ●
                                                                                                                                                                                                   ●
                                                  ●
                                                                                                                                                                                       ●
                                                                                                                                                                               ●
                              ●                                                                                                                                                                        ●
     0.8                                                           ●       ●                                                          ●

                                                           ●                                                                                  ●
                                                                                                                                              ●            ●
                                                                                                                             ●
                                                                                                                                                                                                   ● ●
                                                                                                         ●                                                         ●                               ●
                                                                               ●
                                                                                                                                                                                           ●●
                                                                                                                                                                                            ●
                                                       ●
                                                                                                                                 ●●                                        ●
                                                                                                                 ●
                                                                                                                                                                       ●
                                                                                                                                                                                                                   colour
                  ●
     0.6                                               ●
                                                                                                                                                       ●                                                            ●   0.5
                                                                                                             ●
                                                               ●                        ●                                                                                              ●
                                              ●                                                                              ●
                                                                                                                                                                   ●
                                                                                                                                                                                                               ●    ●   1.0
                                                                                                                                                   ●
 y




                                                                                                                                                                                                           ●
                                  ●                                                                                                                                                                                 ●   1.5
                                  ●
                                                                                                                                                                                           ●                        ●   2.0
                                                           ● ●
                                                                                                                                  ●                                                                ●
                                                                                                     ●
     0.4                                                                                                             ●                                                                                              ●   2.5
                 ●                                     ●
                                                               ●
                          ●
                                                                                                                                          ●                                        ●
                                      ●
                      ●

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

df <- data.frame(x = runif(100), y = runif(100))
                                      ●                                                      ●                                                                                                 ●
                                                                                                                                                       ●
                  ●                                                                                  ●           ●
                                                                                                                                               ●

df$colour <- with(df, x ^ 2 + y + runif(100))
                                                                                                                                  ●           ●
                          ●                                    ●                                                                                               ●
                                                                                                             ●                                                                 ● ●
                                                                       ●
     0.0
                0.0                           0.2                                      0.4                               0.6                                       0.8                                     1.0
qplot(x, y, data = df, colour = colour)
                           x
Wednesday, February 8, 12
1.0                                                                                                                             ●                                               ●
                                                     ●                                                       ●


                                    ●                ●
                                                                                                                                      ●
                                                                                         ●                                       ●                        ●
                                                                     ●
                                                                                                                                                                                ●
                                                ●
                                                                                                                                                                    ●
                                                                                                                                                          ●
                            ●                                                                                                                                                   ●
     0.8                                                     ●       ●                                               ●

                                                     ●                                                                       ●
                                                                                                                             ●            ●
                                                                                                                 ●
                                                                                                                                                                                ●●
                                                                                                 ●                                                ●                             ●
                                                                         ●
                                                                                                                                                                        ●●
                                                                                                                                                                         ●
                                                    ●
                                                                                                                 ●●                                   ●
                                                                                                         ●
                 ●                                                                                                                                    ●
     0.6                                             ●
                                                                                                                                      ●
                                                                                                     ●
                                                         ●                       ●                                                                                  ●                    colour
                                            ●                                                                    ●                                                                   ●
                                                                                                                                  ●
                                                                                                                                                  ●                                       ●   0.5   ●   1.0   ●   1.5
 y




                                                                                                                                                                                    ●
                                ●

                                ●
                                                                                                                                                                                          ●   2.0   ●   2.5
                                                                                                                                                                        ●
                                                         ● ●
                                                                                                                     ●                                                          ●
                                                                                             ●
     0.4                                                                                                     ●
                ●                                    ●
                                                         ●
                        ●
                                                                                                                         ●                                      ●
                                    ●
                    ●

                     ●                               ●
                                                                                                                             ●
                                                    ●                    ●
     0.2                                ●
                                            ●                                ●                                                                            ●
                                                 ●                                                   ●
                                                ●                            ●
                                    ●                                                ●                                                                                      ●
                                                                                                                                      ●
                 ●                                                                           ●           ●
                                                                                                                     ●        ●
                                                                                                                             ●
                        ●                                ●                                                                                    ●
                                                                                                     ●                                                        ● ●
                                                                 ●
     0.0
qplot(x, y, data = df, colour = colour) +
     0.0    0.2    0.4    0.6     0.8    1.0
  guides(colour = guide_legend(nrow = 2, byrow = T))
                       x
Wednesday, February 8, 12
1.0                                                                                                                                             ●                                                   ●
                                                       ●                                                               ●


                                          ●            ●
                                                                                                                                                     ●
                                                                                               ●                                                 ●                       ●
                                                                           ●
                                                                                                                                                                                                 ●
                                                  ●
                                                                                                                                                                                     ●
                                                                                                                                                                             ●
                              ●                                                                                                                                                                      ●
     0.8                                                           ●       ●                                                        ●

                                                           ●                                                                                ●
                                                                                                                                            ●            ●
                                                                                                                           ●
                                                                                                                                                                                                 ●●
                                                                                                       ●                                                         ●                               ●
                                                                               ●
                                                                                                                                                                                         ●●
                                                                                                                                                                                          ●
                                                       ●
                                                                                                                               ●●                                        ●
                                                                                                               ●
                                                                                                                                                                     ●
                                                                                                                                                                                                                 colour
     0.6
                  ●
                                                       ●                                                                                                                                                             2.5
                                                                                                           ●                                         ●
                                                                                       ●
                                                               ●                                                                                                                     ●                               2.0
                                              ●                                                                            ●                                                                                 ●
                                                                                                                                                                 ●
                                                                                                                                                 ●                                                                   1.5
 y




                                                                                                                                                                                                         ●
                                  ●

                                  ●
                                                                                                                                                                                         ●
                                                                                                                                                                                                                     1.0
                                                           ● ●
                                                                                                                                ●                                                                ●
     0.4                                                                                           ●               ●                                                                                                 0.5
                 ●                                     ●
                                                               ●
                          ●
                                                                                                                                        ●                                        ●
                                      ●
                      ●

                      ●                                ●
                                                                                                                                            ●
                                                      ●                        ●
     0.2                                  ●
                                              ●                                    ●                                                                                     ●
                                                   ●                                                       ●
                                                  ●                                ●
                                      ●                                                    ●                                                                                                 ●
                                                                                                                                                     ●
                  ●                                                                                ●           ●
                                                                                                                                ●            ●
                                                                                                                                            ●
                          ●                                    ●                                                                                             ●
                                                                                                           ●                                                                 ● ●
                                                                       ●
     0.0
qplot(x, y, data = df, 0.4
      0.0     0.2
                         colour = colour)0.8+
                                0.6                                                                                                                                                                      1.0
  guides(colour = guide_colorbar())
                             x
Wednesday, February 8, 12
qplot(x, y, data = df, colour = colour, alpha = I(1/4))
Wednesday, February 8, 12
qplot(x, y, data = df, colour = colour, alpha = I(1/4)) +
  guides(colour = guide_legend(
    override.aes = list(alpha = 1, size = 2)))
Wednesday, February 8, 12
# Better layout

     df <- data.frame(x = 1:10, y = 10:1, colour = 1:2)
     qplot(x, y, data = df) + coord_fixed()
     qplot(x, y, data = df) + facet_wrap(~ colour)

     #     Internally, there has been a big rewrite of
     #     the facetting data processing and rendering
     #     systems. This lays the foundation for new
     #     features, and fixes some annoying long-standing
     #     bugs.




Wednesday, February 8, 12
# Speed improvements

     system.time(
       print(qplot(carat, price, data = diamonds))
     )

     # Includes new tools for figuring out what's
     # taking all the time
     benchplot(qplot(carat, price, data = diamonds))

     # See also geom_raster and geom_map

     #     Still a lot of work to do. The emphasis in
     #     ggplot2 is reducing the amount of thinking
     #     time by making it easier to go from the plot in
     #     your brain to the plot on the page.

Wednesday, February 8, 12
30s with geom_tile, 8s with annotation_raster
Wednesday, February 8, 12
library(ggplot2)
     library(reshape2)
     library(RgoogleMaps)
     library(ggmap)

     theft <- subset(crime, offense == "theft" & lat > 29 & lat < 30.2 &
       lon > -95.8)

     lonr <- range(theft$lon)
     latr <- range(theft$lat)

     h_map <- GetMap.bbox(lonr, latr, size = c(1024, 1024))
     h_raster <- as.raster(h_map$myTile)

     benchplot(ggplot(theft, aes(lon, lat)) +
       annotation_raster(h_raster, lonr[1], lonr[2], latr[1], latr[2]) +
       geom_density2d(colour = "black"))

     h_data <- melt(as.matrix(h_raster))
     h_data$lat <- seq(latr[2], latr[1], length = 640)[h_data$Var1]
     h_data$lon <- seq(lonr[1], lonr[2], length = 640)[h_data$Var2]

     benchplot(ggplot(theft, aes(lon, lat)) +
       geom_tile(aes(fill = value), data = h_data) +
       scale_fill_identity() +
       geom_density2d(colour = "black"))



Wednesday, February 8, 12
ggplot2 0.9 scheduled for
                              release on March 1




Wednesday, February 8, 12
Poll: How big
                            is your data?


Wednesday, February 8, 12
# Future work: big visualisation
     # (Sponsored by Revolution Analytics)


     # How can you make a plot of 100 million
     # observations?

     # In less that one minute.




Wednesday, February 8, 12
Wednesday, February 8, 12
Wednesday, February 8, 12
Wednesday, February 8, 12
Wednesday, February 8, 12
Wednesday, February 8, 12
~100,000 points
                            0.06 s to bin
                            0.20 s to convert
                            6.0 s to plot
Wednesday, February 8, 12
~1.2 million
                            ~100,000 points
                            10 s toto bin
                            0.06 s bin
                            0.20 s to convert
                            6.0 s to plot
Wednesday, February 8, 12
Best
                            practices
Wednesday, February 8, 12
Poll: How do you learn
                      about new packages?



Wednesday, February 8, 12
Package best
                              practices
                    • Namespace
                    • Documentation
                    • Unit tests
                    • Read the source!


                    • (ggplot2 not always the best example: it was
                      was my second R package - I have now written
                      around 30. I now know a lot more!)


Wednesday, February 8, 12
Wednesday, February 8, 12
# Namespaces

     library(ggplot2)
     ddply

     # Note that plyr, reshape etc aren't automatically
     # loaded. This is good development practice -
     # it's better to be explicit than implicit.

     # Look at the NAMESPACE file.




Wednesday, February 8, 12
export("%+%")
     export(aes_all)
     export(aes_auto)
     export(aes_string)
     export(aes)
     export(annotate)
     export(annotation_custom)
     export(annotation_map)
     export(annotation_raster)
     export(autoplot)
     export(benchplot)
     export(borders)
     export(continuous_scale)
     export(coord_cartesian)
     export(coord_equal)
     export(coord_fixed)
     export(coord_flip)
     export(coord_map)
     export(coord_polar)
     ...

Wednesday, February 8, 12
# Unit tests

     # Look in tests/ or inst/tests/

     library(testthat)
     test_package("ggplot2")




Wednesday, February 8, 12
# Documentation

     # Function level in man/
     ?geom_point
     ?facet_wrap
     package?ggplot2

     # Vignettes in inst/doc
     # (ggplot2 doesn't have any)

     # Publications
     citation("ggplot2")




Wednesday, February 8, 12
Questions

Wednesday, February 8, 12
Learning ggplot2
                    ggplot2 mailing list
                    http://groups.google.com/group/ggplot2
                    stackoverflow
                    http://stackoverflow.com/tags/ggplot2
                    Lattice to ggplot2 conversion
                    http://learnr.wordpress.com/?s=lattice
                    Cookbook for common graphics
                    http://wiki.stdout.org/rcookbook/Graphs/
                    ggplot2 book
                    http://amzn.com/0387981403



Wednesday, February 8, 12

Weitere ähnliche Inhalte

Mehr von Revolution Analytics

Speeding up R with Parallel Programming in the Cloud
Speeding up R with Parallel Programming in the CloudSpeeding up R with Parallel Programming in the Cloud
Speeding up R with Parallel Programming in the CloudRevolution Analytics
 
Migrating Existing Open Source Machine Learning to Azure
Migrating Existing Open Source Machine Learning to AzureMigrating Existing Open Source Machine Learning to Azure
Migrating Existing Open Source Machine Learning to AzureRevolution Analytics
 
Speed up R with parallel programming in the Cloud
Speed up R with parallel programming in the CloudSpeed up R with parallel programming in the Cloud
Speed up R with parallel programming in the CloudRevolution Analytics
 
Predicting Loan Delinquency at One Million Transactions per Second
Predicting Loan Delinquency at One Million Transactions per SecondPredicting Loan Delinquency at One Million Transactions per Second
Predicting Loan Delinquency at One Million Transactions per SecondRevolution Analytics
 
The Value of Open Source Communities
The Value of Open Source CommunitiesThe Value of Open Source Communities
The Value of Open Source CommunitiesRevolution Analytics
 
Building a scalable data science platform with R
Building a scalable data science platform with RBuilding a scalable data science platform with R
Building a scalable data science platform with RRevolution Analytics
 
The Business Economics and Opportunity of Open Source Data Science
The Business Economics and Opportunity of Open Source Data ScienceThe Business Economics and Opportunity of Open Source Data Science
The Business Economics and Opportunity of Open Source Data ScienceRevolution Analytics
 
Taking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the CloudTaking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the CloudRevolution Analytics
 
The Network structure of R packages on CRAN & BioConductor
The Network structure of R packages on CRAN & BioConductorThe Network structure of R packages on CRAN & BioConductor
The Network structure of R packages on CRAN & BioConductorRevolution Analytics
 
The network structure of cran 2015 07-02 final
The network structure of cran 2015 07-02 finalThe network structure of cran 2015 07-02 final
The network structure of cran 2015 07-02 finalRevolution Analytics
 
Simple Reproducibility with the checkpoint package
Simple Reproducibilitywith the checkpoint packageSimple Reproducibilitywith the checkpoint package
Simple Reproducibility with the checkpoint packageRevolution Analytics
 

Mehr von Revolution Analytics (20)

Speeding up R with Parallel Programming in the Cloud
Speeding up R with Parallel Programming in the CloudSpeeding up R with Parallel Programming in the Cloud
Speeding up R with Parallel Programming in the Cloud
 
Migrating Existing Open Source Machine Learning to Azure
Migrating Existing Open Source Machine Learning to AzureMigrating Existing Open Source Machine Learning to Azure
Migrating Existing Open Source Machine Learning to Azure
 
R in Minecraft
R in Minecraft R in Minecraft
R in Minecraft
 
The case for R for AI developers
The case for R for AI developersThe case for R for AI developers
The case for R for AI developers
 
Speed up R with parallel programming in the Cloud
Speed up R with parallel programming in the CloudSpeed up R with parallel programming in the Cloud
Speed up R with parallel programming in the Cloud
 
The R Ecosystem
The R EcosystemThe R Ecosystem
The R Ecosystem
 
R Then and Now
R Then and NowR Then and Now
R Then and Now
 
Predicting Loan Delinquency at One Million Transactions per Second
Predicting Loan Delinquency at One Million Transactions per SecondPredicting Loan Delinquency at One Million Transactions per Second
Predicting Loan Delinquency at One Million Transactions per Second
 
Reproducible Data Science with R
Reproducible Data Science with RReproducible Data Science with R
Reproducible Data Science with R
 
The Value of Open Source Communities
The Value of Open Source CommunitiesThe Value of Open Source Communities
The Value of Open Source Communities
 
The R Ecosystem
The R EcosystemThe R Ecosystem
The R Ecosystem
 
R at Microsoft (useR! 2016)
R at Microsoft (useR! 2016)R at Microsoft (useR! 2016)
R at Microsoft (useR! 2016)
 
Building a scalable data science platform with R
Building a scalable data science platform with RBuilding a scalable data science platform with R
Building a scalable data science platform with R
 
R at Microsoft
R at MicrosoftR at Microsoft
R at Microsoft
 
The Business Economics and Opportunity of Open Source Data Science
The Business Economics and Opportunity of Open Source Data ScienceThe Business Economics and Opportunity of Open Source Data Science
The Business Economics and Opportunity of Open Source Data Science
 
Taking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the CloudTaking R Analytics to SQL and the Cloud
Taking R Analytics to SQL and the Cloud
 
The Network structure of R packages on CRAN & BioConductor
The Network structure of R packages on CRAN & BioConductorThe Network structure of R packages on CRAN & BioConductor
The Network structure of R packages on CRAN & BioConductor
 
The network structure of cran 2015 07-02 final
The network structure of cran 2015 07-02 finalThe network structure of cran 2015 07-02 final
The network structure of cran 2015 07-02 final
 
Simple Reproducibility with the checkpoint package
Simple Reproducibilitywith the checkpoint packageSimple Reproducibilitywith the checkpoint package
Simple Reproducibility with the checkpoint package
 
R at Microsoft
R at MicrosoftR at Microsoft
R at Microsoft
 

Kürzlich hochgeladen

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 

Kürzlich hochgeladen (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

A Backstage Tour of ggplot2 with Hadley Wickham

  • 1. ggplot2: A backstage tour Hadley Wickham Assistant Professor / Dobelman Family Junior Chair Department of Statistics Rice University February 2012 Wednesday, February 8, 12
  • 2. 1. Why ggplot2? 2. Sneak peek and new features 3. Best practices 4. Questions Wednesday, February 8, 12
  • 3. Poll: What graphics system are you currently using? Wednesday, February 8, 12
  • 5. 2004 WHC ● 02H ● 02M ● 12H ● 0.2 0.1 0.0 whc −0.1 −0.2 −0.3 20 40 60 80 day Wednesday, February 8, 12
  • 7. “Nothing is as practical as a good theory” —Kurt Lewin “[A good model] will bring together in a coherent way things that previously appeared unrelated and which also will provide a basis for dealing systematically with new situations” —David Cox Wednesday, February 8, 12
  • 8. A plot is made up of multiple layers. A layer consists of data, a set of mappings between variables and aesthetics, a geometric object and a statistical transformation Scales control the details of the mapping. All components are independent and reusable. Wednesday, February 8, 12
  • 9. Interesting ggplot example Layered grammar + ggplot2 James Cheshire, http://bit.ly/xqHhAs Wednesday, February 8, 12
  • 11. David B Sparks, http://bit.ly/hn54NW Wednesday, February 8, 12
  • 13. Poll: What resources are most helpful to you when improving your R skills? Wednesday, February 8, 12
  • 14. Learning ggplot2 ggplot2 mailing list http://groups.google.com/group/ggplot2 stackoverflow http://stackoverflow.com/tags/ggplot2 Lattice to ggplot2 conversion http://learnr.wordpress.com/?s=lattice Cookbook for common graphics http://wiki.stdout.org/rcookbook/Graphs/ ggplot2 book http://amzn.com/0387981403 Wednesday, February 8, 12
  • 15. Sneak peek Wednesday, February 8, 12
  • 16. Poll: Why do you use visualisation? Wednesday, February 8, 12
  • 17. # Getting started # To get the CRAN version install.packages("ggplot2") # To get the development version install.packages("devtools") library(devtools) dev_mode() # don't overwrite your existing install install_github("ggplot2") Wednesday, February 8, 12
  • 18. Development version CRAN version Wednesday, February 8, 12
  • 19. New geoms to deal with overplotting 45 ● ● 40 (by Winston Chang) ● ● ● 35 ● ● ● ● ● ● ● ● 30 ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● 2seater compact midsize minivan pickup subcompact suv class Wednesday, February 8, 12
  • 20. New geoms to deal with overplotting 45 ● ● 40 (by Winston Chang) ● ● ● 35 ● ● ● ● ● ● ● ● 30 ● ● ● ● ● hwy ● ● ● ● ● ● ● ● ● ● ● ● 25 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ● ● ● ● ● ● ● ● 15 ● ● ● ● ● 2seater compact midsize minivan pickup subcompact suv qplot(class, hwy, data = mpg) class Wednesday, February 8, 12
  • 21. 45 ● ● ● 40 ● ● ● 35 ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ● 30 ● ● ● ● ● ● ● ● ●● ● ●●● ● ● ● ● ●●● hwy ● ● ● ● ● ● ● ● ● ● ● ●● ●● ● ●● ●●● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● ● ●●● ●● ● ● ● 25 ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 20 ● ● ● ● ●● ●●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ●● ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ●● ● ●● ● ● ● ●● ● ● ● ● ●● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● 15 ● ● ● ● ● ●● ● ● ● ● ● 2seater compact midsize minivan pickup subcompact suv qplot(class, hwy, data = mpg, geom = "jitter") class Wednesday, February 8, 12
  • 22. 45 40 35 30 hwy 25 20 15 2seater compact midsize minivan pickup subcompact suv qplot(class, hwy, data = mpg, geom = "violin") class Wednesday, February 8, 12
  • 23. 45 ● ● ● 40 ● ●● 35 ●● ● ● ● ● ●●● ●●● ●●●● 30 ●● ●● ●●●●●●●●●●●● ●●●●● ●●●●● hwy ● ●●●● ●● ●●●●●●● ●●●●● ● ● ●● ●●●●●●● ●●●●●●●●●●●●●● ●●●●●●●● ● 25 ● ●●●●●●● ●●●● ● ●● ● ●● ● ●●●● ●●●● ● ● ● ● ●● ● ● ●●● ● ●● ● ● ● 20 ●●● ● ●●●●●●● ●●●● ●●●●●●●●● ●● ●●●●●●●● ● ●●●●●●●●●●● ●●●●●●●●●●●●●●●● ●●●●● ●● 15 ●●●● ●●●●●● ●● ●●● ●● 10 2seater compact midsize minivan pickup subcompact suv class Wednesday, February 8, 12
  • 24. 45 ● ● ● 40 ● ●● 35 ●● ● ● ● ● ●●● ●●● ●●●● 30 ●● ●● ●●●●●●●●●●●● ●●●●● ●●●●● hwy ● ●●●● ●● ●●●●●●● ●●●●● ● ● ●● ●●●●●●● ●●●●●●●●●●●●●● ●●●●●●●● ● 25 ● ●●●●●●● ●●●● ● ●● ● ●● ● ●●●● ●●●● ● ● ● ● ●● ● ● ●●● ● ●● ● ● ● 20 ●●● ● ●●●●●●● ●●●● ●●●●●●●●● ●● ●●●●●●●● ● ●●●●●●●●●●● ●●●●●●●●●●●●●●●● ●●●●● ●● 15 ●●●● ●●●●●● ●● qplot(class, hwy, data = mpg, geom = "dotplot", ●● ●●● stackdir = "center", binaxis = "y", 10 2seater compact midsize minivan pickup subcompact suv stackratio = 1, binwidth = class 1) Wednesday, February 8, 12
  • 25. 1.0 Better legends ● ● ● ● (by Kohske Takahashi) ● ● ● ● ● ● ● ● ● ● ● ● ● 0.8 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● colour ● 0.6 ● ● ● 0.5 ● ● ● ● ● ● ● ● ● 1.0 ● y ● ● ● 1.5 ● ● ● 2.0 ● ● ● ● ● 0.4 ● ● 2.5 ● ● ● ● ● ● ● ● ● ● ● ● ● 0.2 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.0 0.0 0.2 0.4 0.6 0.8 1.0 x Wednesday, February 8, 12
  • 26. 1.0 Better legends ● ● ● ● (by Kohske Takahashi) ● ● ● ● ● ● ● ● ● ● ● ● ● 0.8 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●● ● ● ●● ● ● ● colour ● 0.6 ● ● ● 0.5 ● ● ● ● ● ● ● ● ● 1.0 ● y ● ● ● 1.5 ● ● ● 2.0 ● ● ● ● ● 0.4 ● ● 2.5 ● ● ● ● ● ● ● ● ● ● ● ● ● 0.2 ● ● ● ● ● ● ● ● df <- data.frame(x = runif(100), y = runif(100)) ● ● ● ● ● ● ● ● df$colour <- with(df, x ^ 2 + y + runif(100)) ● ● ● ● ● ● ● ● ● 0.0 0.0 0.2 0.4 0.6 0.8 1.0 qplot(x, y, data = df, colour = colour) x Wednesday, February 8, 12
  • 27. 1.0 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.8 ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ●● ● ● ● ● 0.6 ● ● ● ● ● ● colour ● ● ● ● ● ● 0.5 ● 1.0 ● 1.5 y ● ● ● ● 2.0 ● 2.5 ● ● ● ● ● ● 0.4 ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.2 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.0 qplot(x, y, data = df, colour = colour) + 0.0 0.2 0.4 0.6 0.8 1.0 guides(colour = guide_legend(nrow = 2, byrow = T)) x Wednesday, February 8, 12
  • 28. 1.0 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.8 ● ● ● ● ● ● ● ● ●● ● ● ● ● ●● ● ● ●● ● ● ● colour 0.6 ● ● 2.5 ● ● ● ● ● 2.0 ● ● ● ● ● 1.5 y ● ● ● ● 1.0 ● ● ● ● 0.4 ● ● 0.5 ● ● ● ● ● ● ● ● ● ● ● ● ● 0.2 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● 0.0 qplot(x, y, data = df, 0.4 0.0 0.2 colour = colour)0.8+ 0.6 1.0 guides(colour = guide_colorbar()) x Wednesday, February 8, 12
  • 29. qplot(x, y, data = df, colour = colour, alpha = I(1/4)) Wednesday, February 8, 12
  • 30. qplot(x, y, data = df, colour = colour, alpha = I(1/4)) + guides(colour = guide_legend( override.aes = list(alpha = 1, size = 2))) Wednesday, February 8, 12
  • 31. # Better layout df <- data.frame(x = 1:10, y = 10:1, colour = 1:2) qplot(x, y, data = df) + coord_fixed() qplot(x, y, data = df) + facet_wrap(~ colour) # Internally, there has been a big rewrite of # the facetting data processing and rendering # systems. This lays the foundation for new # features, and fixes some annoying long-standing # bugs. Wednesday, February 8, 12
  • 32. # Speed improvements system.time( print(qplot(carat, price, data = diamonds)) ) # Includes new tools for figuring out what's # taking all the time benchplot(qplot(carat, price, data = diamonds)) # See also geom_raster and geom_map # Still a lot of work to do. The emphasis in # ggplot2 is reducing the amount of thinking # time by making it easier to go from the plot in # your brain to the plot on the page. Wednesday, February 8, 12
  • 33. 30s with geom_tile, 8s with annotation_raster Wednesday, February 8, 12
  • 34. library(ggplot2) library(reshape2) library(RgoogleMaps) library(ggmap) theft <- subset(crime, offense == "theft" & lat > 29 & lat < 30.2 & lon > -95.8) lonr <- range(theft$lon) latr <- range(theft$lat) h_map <- GetMap.bbox(lonr, latr, size = c(1024, 1024)) h_raster <- as.raster(h_map$myTile) benchplot(ggplot(theft, aes(lon, lat)) + annotation_raster(h_raster, lonr[1], lonr[2], latr[1], latr[2]) + geom_density2d(colour = "black")) h_data <- melt(as.matrix(h_raster)) h_data$lat <- seq(latr[2], latr[1], length = 640)[h_data$Var1] h_data$lon <- seq(lonr[1], lonr[2], length = 640)[h_data$Var2] benchplot(ggplot(theft, aes(lon, lat)) + geom_tile(aes(fill = value), data = h_data) + scale_fill_identity() + geom_density2d(colour = "black")) Wednesday, February 8, 12
  • 35. ggplot2 0.9 scheduled for release on March 1 Wednesday, February 8, 12
  • 36. Poll: How big is your data? Wednesday, February 8, 12
  • 37. # Future work: big visualisation # (Sponsored by Revolution Analytics) # How can you make a plot of 100 million # observations? # In less that one minute. Wednesday, February 8, 12
  • 43. ~100,000 points 0.06 s to bin 0.20 s to convert 6.0 s to plot Wednesday, February 8, 12
  • 44. ~1.2 million ~100,000 points 10 s toto bin 0.06 s bin 0.20 s to convert 6.0 s to plot Wednesday, February 8, 12
  • 45. Best practices Wednesday, February 8, 12
  • 46. Poll: How do you learn about new packages? Wednesday, February 8, 12
  • 47. Package best practices • Namespace • Documentation • Unit tests • Read the source! • (ggplot2 not always the best example: it was was my second R package - I have now written around 30. I now know a lot more!) Wednesday, February 8, 12
  • 49. # Namespaces library(ggplot2) ddply # Note that plyr, reshape etc aren't automatically # loaded. This is good development practice - # it's better to be explicit than implicit. # Look at the NAMESPACE file. Wednesday, February 8, 12
  • 50. export("%+%") export(aes_all) export(aes_auto) export(aes_string) export(aes) export(annotate) export(annotation_custom) export(annotation_map) export(annotation_raster) export(autoplot) export(benchplot) export(borders) export(continuous_scale) export(coord_cartesian) export(coord_equal) export(coord_fixed) export(coord_flip) export(coord_map) export(coord_polar) ... Wednesday, February 8, 12
  • 51. # Unit tests # Look in tests/ or inst/tests/ library(testthat) test_package("ggplot2") Wednesday, February 8, 12
  • 52. # Documentation # Function level in man/ ?geom_point ?facet_wrap package?ggplot2 # Vignettes in inst/doc # (ggplot2 doesn't have any) # Publications citation("ggplot2") Wednesday, February 8, 12
  • 54. Learning ggplot2 ggplot2 mailing list http://groups.google.com/group/ggplot2 stackoverflow http://stackoverflow.com/tags/ggplot2 Lattice to ggplot2 conversion http://learnr.wordpress.com/?s=lattice Cookbook for common graphics http://wiki.stdout.org/rcookbook/Graphs/ ggplot2 book http://amzn.com/0387981403 Wednesday, February 8, 12