Introduction
       The crux of ggplot2
         Specific example
Resources and conclusions




                    ggplot2

                Premal P. Vora
                fpv@psu.edu
               Penn State Harrisburg


           PhillyR Users’ Group




                     Vora    ggplot2
Introduction
                     The crux of ggplot2
                       Specific example
              Resources and conclusions


What and why?




    ggplot2 is an R package for creating statistical graphics.
    “Grammar of Graphics”, Leland Wilkinson, 2005, Springer.
    Philosophy of book
    Results of philosophy




                                   Vora    ggplot2
Introduction
                     The crux of ggplot2
                       Specific example
              Resources and conclusions


Tufte: “...may well be the best statistical graphic ever
drawn.”




                                   Vora    ggplot2
Introduction
                     The crux of ggplot2
                       Specific example
              Resources and conclusions


Who and how?



    ggplot2 – Hadley Wickham
    Requires R version 2.8 (but...get the latest version)
    In R environment:
      > install.packages(‘‘ggplot2’’)
      > library(ggplot2)




                                   Vora    ggplot2
Introduction
                       The crux of ggplot2
                         Specific example
                Resources and conclusions


Components of the grammar


     Layers
         Data and aesthetic mapping
         A statistical transformation
         A geometric object
         A position adjustment
     Scales
     Coordinate system
     Faceting




                                     Vora    ggplot2
Introduction
                       The crux of ggplot2
                         Specific example
                Resources and conclusions


Examples of components


     Layers
         Data and aesthetic mapping
         A statistical transformation
         A geometric object
         A position adjustment
     Scales
     Coordinate system
     Faceting




                                     Vora    ggplot2
Introduction
                   The crux of ggplot2
                     Specific example
            Resources and conclusions


Economics and finance data


            > real_deficit[1:5,]
              Year value
            1 1940 -36.3
            2 1941 -59.1
            3 1942 -219.1
            4 1943 -531.7
            5 1944 -501.1

            > real_surplus[6:10,]
               Year value
            6 1945    0.0
            7 1946    0.0
            8 1947 40.2
            9 1948 111.5
            10 1949   5.7

            > real_snp500_level[1:5,]
              Year    value
            1 1940 131.5920
            2 1941 103.9474
            3 1942 104.3803
            4 1943 113.7427
            5 1944 139.9368




                                    Vora   ggplot2
Introduction
                                 The crux of ggplot2
                                   Specific example
                          Resources and conclusions


Code + output I

  > p <- ggplot(data=real_deficit, aes(Year,value))
  > p <- p + geom_bar(stat=”identity”, fill=”Red”)
  > p



               0


           −200


           −400


           −600
   value




           −800


           −1000


           −1200


                   1940   1950        1960         1970         1980   1990   2000
                                                       Year




                                               Vora       ggplot2
Introduction
                                 The crux of ggplot2
                                   Specific example
                          Resources and conclusions


Code + output II

  > p <- p + geom_bar(data=real_surplus, aes(Year,value),
  stat=”identity”, fill=”Blue”)
  > p




             200

               0

           −200

           −400
   value




           −600

           −800

           −1000

           −1200


                   1940   1950        1960         1970         1980   1990   2000
                                                       Year




                                               Vora       ggplot2
Introduction
                                 The crux of ggplot2
                                   Specific example
                          Resources and conclusions


Code + output III

  > p <- p + geom_line(data=real_snp500_level, aes(Year,value))
  > p




            1500


            1000


             500
   value




               0


           −500


           −1000


                   1940   1950        1960         1970         1980   1990   2000
                                                       Year




                                               Vora       ggplot2
Introduction
                       The crux of ggplot2
                         Specific example
                Resources and conclusions


Further information



     Book: “ggplot2: Elegant Graphics for Data Analysis”,
     Wickham, Springer, 2009.
     Website: http://had.co.nz/ggplot2 has reference materials.
     Usenet group: http://groups.google.com/group/ggplot2 –
     “...relatively low traffic and is very friendly to new users.”
     (Wickham, 2009).




                                     Vora    ggplot2

Ggplot2

  • 1.
    Introduction The crux of ggplot2 Specific example Resources and conclusions ggplot2 Premal P. Vora fpv@psu.edu Penn State Harrisburg PhillyR Users’ Group Vora ggplot2
  • 2.
    Introduction The crux of ggplot2 Specific example Resources and conclusions What and why? ggplot2 is an R package for creating statistical graphics. “Grammar of Graphics”, Leland Wilkinson, 2005, Springer. Philosophy of book Results of philosophy Vora ggplot2
  • 3.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Tufte: “...may well be the best statistical graphic ever drawn.” Vora ggplot2
  • 4.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Who and how? ggplot2 – Hadley Wickham Requires R version 2.8 (but...get the latest version) In R environment: > install.packages(‘‘ggplot2’’) > library(ggplot2) Vora ggplot2
  • 5.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Components of the grammar Layers Data and aesthetic mapping A statistical transformation A geometric object A position adjustment Scales Coordinate system Faceting Vora ggplot2
  • 6.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Examples of components Layers Data and aesthetic mapping A statistical transformation A geometric object A position adjustment Scales Coordinate system Faceting Vora ggplot2
  • 7.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Economics and finance data > real_deficit[1:5,] Year value 1 1940 -36.3 2 1941 -59.1 3 1942 -219.1 4 1943 -531.7 5 1944 -501.1 > real_surplus[6:10,] Year value 6 1945 0.0 7 1946 0.0 8 1947 40.2 9 1948 111.5 10 1949 5.7 > real_snp500_level[1:5,] Year value 1 1940 131.5920 2 1941 103.9474 3 1942 104.3803 4 1943 113.7427 5 1944 139.9368 Vora ggplot2
  • 8.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Code + output I > p <- ggplot(data=real_deficit, aes(Year,value)) > p <- p + geom_bar(stat=”identity”, fill=”Red”) > p 0 −200 −400 −600 value −800 −1000 −1200 1940 1950 1960 1970 1980 1990 2000 Year Vora ggplot2
  • 9.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Code + output II > p <- p + geom_bar(data=real_surplus, aes(Year,value), stat=”identity”, fill=”Blue”) > p 200 0 −200 −400 value −600 −800 −1000 −1200 1940 1950 1960 1970 1980 1990 2000 Year Vora ggplot2
  • 10.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Code + output III > p <- p + geom_line(data=real_snp500_level, aes(Year,value)) > p 1500 1000 500 value 0 −500 −1000 1940 1950 1960 1970 1980 1990 2000 Year Vora ggplot2
  • 11.
    Introduction The crux of ggplot2 Specific example Resources and conclusions Further information Book: “ggplot2: Elegant Graphics for Data Analysis”, Wickham, Springer, 2009. Website: http://had.co.nz/ggplot2 has reference materials. Usenet group: http://groups.google.com/group/ggplot2 – “...relatively low traffic and is very friendly to new users.” (Wickham, 2009). Vora ggplot2