SlideShare a Scribd company logo
1 of 60
Download to read offline
Introduciton to R Graphics with ggplot2


                                Harvard MIT Data Center


                                    May 10, 2013



                             The Institute
                             for Quantitative Social Science
                             at Harvard University



(Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   1 / 60
Outline

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   2 / 60
Introduction


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   3 / 60
Introduction


Class Files And Administrative Details



      User name: dataclass
      Password: dataclass
      Copy Rgraphics folder from shared drive to your desktop
      Class Structure and Organization
             Ask questions at any time. Really!
             Collaboration is encouraged
             This is your class! Special requests are encouraged
      This is an intermediate R course
             Assumes working knowledge of R
             Relatively fast-paced
             Focus is on ggplot2 graphics–other packages will not be covered




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   4 / 60
Introduction


Starting A The End
 My goal: by the end of the workshop you will be able to reproduce this
 graphic from the Economist:




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   5 / 60
Introduction


Why ggplot2?



     Advantages of ggplot2
            Consistent underlying grammar of graphics (Wilkinson, 2005)
            Plot specification at a high level of abstraction
            Very flexible
            Theme system for polishing plot appearance
            Active maintenance and development–getting better all the time
            Many users, active mailing list
     Things you cannot do With ggplot2
            3-dimensional graphics
            Graph-theory type graphs (nodes/edges layout)




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   6 / 60
Introduction


What Is The Grammar Of Graphics?



     The basic idea: independently specify plot building blocks
     Anatomy of a plot:
            data
            aesthetic mapping
            geometric object
            statistical transformations
            scales
            coordinate system
            position adjustments
            faceting




 (Harvard MIT Data Center)    Introduciton to R Graphics with ggplot2   May 10, 2013   7 / 60
Introduction


Example data I: mtcars

  > print(head(mtcars, 4))
                  mpg cyl disp hp drat   wt qsec vs am gear carb
  Mazda RX4      21.0   6 160 110 3.90 2.62 16.5 0 1      4    4
  Mazda RX4 Wag 21.0    6 160 110 3.90 2.88 17.0 0 1      4    4
  Datsun 710     22.8   4 108 93 3.85 2.32 18.6 1 1       4    1
  Hornet 4 Drive 21.4   6 258 110 3.08 3.21 19.4 1 0      3    1


  mpg     Miles/(US) gallon
  cyl     Number of cylinders
  disp    Displacement (cu.in.)
  hp      Gross horsepower
  drat    Rear axle ratio
  wt      Weight (lb/1000)
  qsec    1/4 mile time
  vs      V/S
  am      Transmission (0 = automatic, 1 = manual)
  gear    Number of forward gears
  carb    Number of carburetors




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   8 / 60
Introduction


ggplot2 VS Base Graphics




     Compared to base graphics, ggplot2
            is more verbose for simple / canned graphics
            is less verbose for complex / custom graphics
            does not have methods (data should always be in a data.frame)
            uses a different system for adding plot elements




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   9 / 60
Introduction


ggplot2 VS Base Graphics


 Base graphics VS ggplot for simple graphs:

                                               ggplot(mtcars, aes(x = mpg)) +
hist(mtcars$mpg)
                                                 geom_histogram(binwidth = 5)




   (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   10 / 60
Introduction


ggplot2 VS Base Graphics

 Base graphics VS ggplot for complex graphs:

par(mar = c(4,4,.1,.1))
plot(mpg ~ hp,
     data=subset(mtcars, am==1),               ggplot(mtcars, aes(x=hp,
     xlim=c(50, 450),ylim=c(5, 40))                          y=mpg,
points(mpg ~ hp, col="red",                                  color=factor(am)))+
       data=subset(mtcars, am==0))             geom_point()
legend(350, 40,
       c("1", "0"), title="am",
       col=c("black", "red"),
       pch=c(1, 1))
                                               #




   (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   11 / 60
Geometric Objects And Aesthetics


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   12 / 60
Geometric Objects And Aesthetics


Aesthetic Mapping



     In ggplot land aesthetic means "something you can see"
     Examples include:
            position (i.e., on the x and y axes)
            color ("outside" color)
            fill ("inside" color)
            shape (of points)
            linetype
            size
     Each type of geom accepts only a subset of all aesthetics–refer to the
     geom help pages to see what mappings each geom accepts
     Aesthetic mappings are set with the aes() function




 (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   13 / 60
Geometric Objects And Aesthetics


Geometic Objects (geom)


     Geometric objects are the actual marks we put on a plot
     Examples include:
            points (geom_point, for scatter plots, dot plots, etc)
            lines (geom_line, for time series, trend lines, etc)
            boxplot (geom_boxplot, for, well, boxplots!)
     A plot must have at least one geom; there is no upper limit
     Add a geom to a plot using the + operator
  > geoms can help.search("geom_", package = "ggplot2")
     You <- get a list of available geometric objects:
  > geoms$matches[1:4, 1:2]
       topic          title
  [1,] "geom_abline" "Line specified by slope and intercept."
  [2,] "geom_area"    "Area plot."
  [3,] "geom_bar"     "Bars, rectangles with bases on x-axis"
  [4,] "geom_bin2d" "Add heatmap of 2d bin counts."




 (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   14 / 60
Geometric Objects And Aesthetics


Points (Scatterplot)



      Now that we know about geometric objects and aesthetic mapping, we
      can make a ggplot

  ggplot(mtcars, aes(x = hp, y = mpg)) +                      ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point()                                                geom_point(aes(y=log10(mpg)))




  (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2       May 10, 2013   15 / 60
Geometric Objects And Aesthetics


Lines (Prediction Line)
      A plot constructed with ggplot can have more than one geom
      Our hp vs mpg plot could use a regression line:

        mtcars$pred.mpg <- predict(lm(mpg ~ hp, data = mtcars))

        p1 <- ggplot(mtcars, aes(x = hp, y = mpg))

        p1 + geom_point(aes(color = wt)) +
          geom_line(aes(y = pred.mpg))




  (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   16 / 60
Geometric Objects And Aesthetics


Smoothers

     Not all geometric objects are simple shapes–the smooth geom includes a
     line and a ribbon

       p2 <- ggplot(mtcars, aes(x = hp, y = mpg))

       p2 + geom_point(aes(color = wt)) +
         geom_smooth()




 (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   17 / 60
Geometric Objects And Aesthetics


Text (Label Points)

      Each geom accepts a particualar set of mappings–for example
      geom_text() accepts a labels mapping

        p2 + geom_point(aes(color = wt)) +
          geom_smooth() +
          geom_text(aes(label=rownames(mtcars)), size=2)




  (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   18 / 60
Geometric Objects And Aesthetics


Aesthetic Mapping VS Assignment

     Note that variables are mapped to aesthetics with the aes() function,
     while fixed aesthetics are set outside the aes() call
     This sometimes leads to confusion, as in this example:

       ggplot(mtcars, aes(x = hp, y = mpg)) +
         geom_point(aes(size = 2),# incorrect! 2 is not a variable
                    color="red") # this is fine -- all points red




 (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   19 / 60
Geometric Objects And Aesthetics


Mapping Variables To Other Aesthetics
      Other aesthetics are mapped in the same way as x and y in the previous
      example

        ggplot(mtcars, aes(x = hp, y = mpg)) +
          geom_point(aes(color=wt, shape = factor(am)))




  (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   20 / 60
Geometric Objects And Aesthetics


Exercise I




   1   Create a scatter plot with displacement on the x axis and horse power
       on the y axis
   2   Color the points in the previous plot blue
   3   Color the points in the previous plot according to miles per gallon
   4   Exercise I prototype :noexport:




  (Harvard MIT Data Center)         Introduciton to R Graphics with ggplot2   May 10, 2013   21 / 60
Statistical Transformations


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)           Introduciton to R Graphics with ggplot2   May 10, 2013   22 / 60
Statistical Transformations


Statistical Transformations

      Some plot types (such as scatterplots) do not require
      transformations–each point is plotted at x and y coordinates equal to
      the original value
      Other plots, such as boxplots, histograms, prediction lines etc. require
      statistical transformations
             For a boxplot the y values must be transformed to the median and
             1.5(IQR)
             For a smoother smother the y values must be transformed into predicted
             values
      Each geom has a default statistic, but these can be changed
  > args(geom_bar) the default statistic for geom_bar is stat_bin
     For example,
  function (mapping = NULL, data = NULL, stat = "bin", position = "stack",
       ...)
  NULL
  > # ?stat_bin
  >



  (Harvard MIT Data Center)           Introduciton to R Graphics with ggplot2   May 10, 2013   23 / 60
Statistical Transformations


Setting Statistical Transformation Arguments


      Arguments to stat_ functions are passed through geom_ functions
      Slightly annoying because in order to change it you have to first
      determine which stat the geom uses, then determine the arguments to
      that stat

  ggplot(mtcars, aes(x = mpg)) +                                ggplot(mtcars, aes(x = mpg)) +
    geom_bar()                                                    geom_bar(stat = "bin", binwidth=4)




  (Harvard MIT Data Center)           Introduciton to R Graphics with ggplot2       May 10, 2013   24 / 60
Statistical Transformations


Changing The Statistical Transformation

        Sometimes the default statistical transformation is not what you need
    > (mtc.sum <-case with pre-summarized mtcars["gear"], FUN=mean))
       Often the aggregate(mtcars["mpg"], data
      gear mpg
    1    3 16.1
    2    4 24.5
    3    5 21.4


> ggplot(mtc.sum, aes(x=gear, y=mpg)) +
    geom_bar()                                                    ggplot(mtc.sum, aes(x=gear, y=mpg)) +
Mapping a variable to y and also                                    geom_bar(stat="identity")
using stat="bin".
Error in pmin(y, 0) : object
’y’ not found




.


    (Harvard MIT Data Center)           Introduciton to R Graphics with ggplot2       May 10, 2013   25 / 60
Statistical Transformations


Exercise II




   1   Create boxplots of mpg by gear
   2   Overlay points on top of the box plots
   3   Create a scatter plot of weight vs. horsepower
   4   Overlay a linear regression line on top of the scatter plot




  (Harvard MIT Data Center)           Introduciton to R Graphics with ggplot2   May 10, 2013   26 / 60
Scales


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   27 / 60
Scales


Scales: Controlling Aesthetic Mapping




      In ggplot2 scales include
             position
             color and fill
             size
             shape
             line type
      Modified with scale_<aesthetic>_<type>




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   28 / 60
Scales


Common Scale Arguments




     name: the first argument gives the axis or legend title
     limits: the minimum and maximum of the scale
     breaks: the points along the scale where labels should appear
     labels: the labels that appear at each break




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   29 / 60
Scales


Scale Modification Examples

       p6 <- ggplot(mtcars, aes(x = factor(gear), y = mpg))
       p6 + geom_point(aes(color = wt))




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   30 / 60
Scales


Scale breaks and labels

        p7 <- p6 + geom_point(aes(color = wt)) +
          scale_x_discrete("Number of Gears",
                           breaks = c("3", "4", "5"),
                           labels = c("Three", "Four", "Five"))
        p7 + scale_color_continuous("Weight",
                                 breaks = with(mtcars, c(min(wt), median(wt), max(wt
                                 labels = c("Light", "Medium", "Heavy"))




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   31 / 60
Scales


Scale breaks and labels

        p7 + scale_color_continuous("Weight",
                                 breaks = with(mtcars, c(min(wt), median(wt), max(wt
                                 labels = c("Light", "Medium", "Heavy"),
                                 low = "black",
                                 high = "gray80")




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   32 / 60
Scales


Using different color scales
        p7 + scale_color_gradient2("Weight",
                                  breaks = with(mtcars, c(min(wt), median(wt), max(w
                                  labels = c("Light", "Medium", "Heavy"),
                                  low = "blue",
                                  mid = "black",
                                  high = "red",
                                  midpoint = median(mtcars$wt))




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   33 / 60
Scales


Scale Modification Examples

       p8 <- ggplot(mtcars, aes(x = factor(gear), y = mpg))
       p8 + geom_point(aes(size = wt))




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   34 / 60
Scales


Scale range

        p8 + geom_point(aes(size = wt)) +
          scale_size_continuous("Weight",
                                range = c(2, 10))




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   35 / 60
Scales


Available Scales


      Partial combination matrix of available scales
                     Scale             Types            Examples
                     scale_color_      identity         scale_fill_continuous
                     scale_fill_        manual           scale_color_discrete
                     scale_size_       continuous       scale_size_manual
                                       discrete         scale_size_discrete
                     scale_shape_      discrete         scale_shape_discrete
                     scale_linetype_   identity         scale_shape_manual
                                       manual           scale_linetype_discrete
                     scale_x_          continuous       scale_x_continuous
                     scale_y_          discrete         scale_y_discrete
                                       reverse          scale_x_log
                                       log              scale_y_reverse
                                       date             scale_x_date
                                       datetime         scale_y_datetime




  (Harvard MIT Data Center)     Introduciton to R Graphics with ggplot2           May 10, 2013   36 / 60
Scales


Exercise III




   1   Experiment with color, size, and shape aesthetics / scales
   2   What happens when you map more than one aesthetic to a variable?
   3   Which aesthetics are good for continuous variables? Which work better
       for discrete variables?




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   37 / 60
Faceting


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   38 / 60
Faceting


Faceting




      Faceting is ggplot2 parlance for small multiples
      The idea is to create separate graphs for subsets of data
      ggplot2 offers two functions for creating small multiples:
          1   facet_wrap(): define subsets as the levels of a single grouping variable
          2   facet_grid(): define subsets as the crossing of two grouping variables
      Facilitates comparison among plots, not just of geoms within a plot




  (Harvard MIT Data Center)    Introduciton to R Graphics with ggplot2   May 10, 2013   39 / 60
Faceting


Example Data II: Titanic
  > titanic <- read.csv(paste("http://biostat.mc.vanderbilt.edu/",
  +                           "wiki/pub/Main/",
  +                           "DataSets/titanic3.csv", sep = ""))
  >


                             variable    description
                             pclass      Passenger Class
                             survival    Survival
                             name        Name
                             sex         Sex
                             age         Age
                             sibsp       Number of Siblings/Spouses Aboard
                             parch       Number of Parents/Children Aboard
                             ticket      Ticket Number
                             fare        Passenger Fare
                             cabin       Cabin
                             embarked    Port of Embarkation
                             boat        Lifeboat
                             body        Body Identification Number
                             home.dest   Home/Destination




 (Harvard MIT Data Center)          Introduciton to R Graphics with ggplot2   May 10, 2013   40 / 60
Faceting


What happened on the Titanic?




     Start by transforming the data into a more usable form:
  > library(plyr)
  > titanic$age.g <- as.integer(cut(titanic$age, 10))
  > td <- ddply(titanic, c("pclass", "age.g", "sex"),
  +              summarize,
  +              ps = length(survived[survived == 1])/length(survived))
  >




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   41 / 60
Faceting


What happened on the Titanic?
     Basic scatter plot:

       p8 <- ggplot(td, aes(x = age.g, y = ps))
       p8 + geom_point()




     Why do we have two clusters of points?
 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   42 / 60
Faceting


What happened on the Titanic?

     Use the techniques we already know (aesthetic mapping):

       p8 <- ggplot(td, aes(x = age.g, y = ps))
       p8 + geom_point(aes(color = pclass, shape = sex))




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   43 / 60
Faceting


What happened on the Titanic?

     Use faceting in one dimension

       p8 + geom_point(aes(shape = sex)) +
       facet_wrap(~ pclass)




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   44 / 60
Faceting


What happened on the Titanic?

     Use faceting in two dimensions

       p8 + geom_point() +
       facet_grid(sex ~ pclass)




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   45 / 60
Themes


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   46 / 60
Themes


Themes



     The ggplot2 theme system handles non-data plot elements such as
            Axis labels
            Plot background
            Facet label backround
            Legend appearance
     Two built-in themes:
            theme_gray() (default)
            theme_bw()
            More available on the wiki:
https://github.com/hadley/ggplot2/wiki/Themes




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   47 / 60
Themes


Overriding theme defaults
      Specific theme elements can be overridden using theme()
      Example:

        p7 + theme(plot.background = element_rect(
                    fill = "white",
                    colour = "gray40"))




      You can see available options by printing theme_gray() or theme_bw()
  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   48 / 60
Themes


Creating and saving new themes

     You can create new themes, as in the following example:

       theme_new <- theme_bw() +
         theme(text=element_text(size = 12, family = ""),
               axis.text.x = element_text(colour = "red"),
               panel.background = element_rect(fill = "pink"))

       p7 + theme_new




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   49 / 60
The #1 FAQ


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   50 / 60
The #1 FAQ


Map Aesthetic To Different Columns
 The most frequently asked question goes something like this: I have two
 variables in my data.frame, and I’d like to plot them as separate points, with
 different color depending on which variable it is. How do I do that?

  ggplot(mtcars, aes(x=wt)) +
    geom_point(aes(y=disp), color="red") + library(reshape2)
    geom_point(aes(y=hp), color="blue")    mtc.m <- melt(mtcars,
                                                          measure.vars=c("mpg",
                                                                         "hp"))
                                           ggplot(mtc.m,
                                                  aes(x=wt,
                                                      y=value,
  #                                                   color=variable)) +
                                             geom_point()




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   51 / 60
Putting It All Together


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)        Introduciton to R Graphics with ggplot2   May 10, 2013   52 / 60
Putting It All Together


Challenge: Recreate This Economist Graph




        Data
 The data is available in the EconomistData.csv file. Original sources are
 http://www.transparency.org/content/download/64476/1031428
 http://hdrstats.undp.org/en/indicators/display_cf_xls_indicator.cfm?indicator_id=103106&lang=en
   (Harvard MIT Data Center)              Introduciton to R Graphics with ggplot2                  May 10, 2013   53 / 60
Putting It All Together


Challenge Solution
   1   Load the data:
 dat <- read.csv("dataSets/EconomistData.csv")
   1   Create basic scatter plot

            pc1 <- ggplot(dat, aes(x = CPI, y = HDI, color = Region))
            (pc1 <- pc1 + geom_point(shape = 1))




        #




  (Harvard MIT Data Center)        Introduciton to R Graphics with ggplot2   May 10, 2013   54 / 60
Putting It All Together


Challenge Solution

 Add labels
        label.these <- c("Congo", "Sudan", "Afghanistan", "Greece", "China",
                         "India", "Rwanda", "Spain", "France", "United States",
                         "Japan", "Norway", "Singapore")
        (pc2 <- pc1 +
         geom_text(aes(label = Country),
                   color = "black", size = 3, hjust = 1.1,
                   data = dat[dat$Country %in% label.these, ]))




  (Harvard MIT Data Center)        Introduciton to R Graphics with ggplot2   May 10, 2013   55 / 60
Putting It All Together


Challenge Solution

 Add smoothing line
            (pc3 <- pc2 +
             geom_smooth(aes(group = 1),
                         method = "lm",
                         color = "black",
                         formula = y~ poly(x, 2),
                         se = FALSE))
        #




  (Harvard MIT Data Center)        Introduciton to R Graphics with ggplot2   May 10, 2013   56 / 60
Putting It All Together


Challenge Solution

 Finishing touches
        (pc4 <- pc3 + theme_bw() +
          scale_x_continuous("Corruption Perceptions Index, 2011n(10 = least corrup
          scale_y_continuous("Human Development Index, 2011n(1 = best)") +
          theme(legend.position = "top", legend.direction = "horizontal"))




  (Harvard MIT Data Center)        Introduciton to R Graphics with ggplot2   May 10, 2013   57 / 60
Wrap-up


Topic

 1     Introduction

 2     Geometric Objects And Aesthetics

 3     Statistical Transformations

 4     Scales

 5     Faceting

 6     Themes

 7     The #1 FAQ

 8     Putting It All Together

 9     Wrap-up


     (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   58 / 60
Wrap-up


Help Us Make This Workshop Even Better!




     Please take a moment to fill out a very short feedback form
     These workshops exist for you – tell us what you need!
     http://tinyurl.com/R-graphics-feedback




 (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   59 / 60
Wrap-up


Additional resources



      ggplot2 resources
             Mailing list: http://groups.google.com/group/ggplot2
             Wiki: https://github.com/hadley/ggplot2/wiki
             Website: http://had.co.nz/ggplot2/
             StackOverflow:
             http://stackoverflow.com/questions/tagged/ggplot
      IQSS resources
             Research technology consulting:
             http://projects.iq.harvard.edu/rtc
             Workshops:
             http://projects.iq.harvard.edu/rtc/filter_by/workshops




  (Harvard MIT Data Center)   Introduciton to R Graphics with ggplot2   May 10, 2013   60 / 60

More Related Content

What's hot

Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis Peter Reimann
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using RVictoria López
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olapSalah Amean
 
Hadoop File system (HDFS)
Hadoop File system (HDFS)Hadoop File system (HDFS)
Hadoop File system (HDFS)Prashant Gupta
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R StudioRupak Roy
 
Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)Mustafa Sherazi
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization Sourabh Sahu
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with RShareThis
 
Data tidying with tidyr meetup
Data tidying with tidyr  meetupData tidying with tidyr  meetup
Data tidying with tidyr meetupMatthew Samelson
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In RRsquared Academy
 
Introduction to Data Visualization
Introduction to Data VisualizationIntroduction to Data Visualization
Introduction to Data VisualizationStephen Tracy
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenEdureka!
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualizationDr. Hamdan Al-Sabri
 
Exploratory Data Analysis
Exploratory Data AnalysisExploratory Data Analysis
Exploratory Data AnalysisUmair Shafique
 

What's hot (20)

Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Exploratory data analysis
Exploratory data analysis Exploratory data analysis
Exploratory data analysis
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
Map Reduce
Map ReduceMap Reduce
Map Reduce
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
Data Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olapData Mining:  Concepts and Techniques (3rd ed.)— Chapter _04 olap
Data Mining: Concepts and Techniques (3rd ed.) — Chapter _04 olap
 
Hadoop File system (HDFS)
Hadoop File system (HDFS)Hadoop File system (HDFS)
Hadoop File system (HDFS)
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)Clustering in data Mining (Data Mining)
Clustering in data Mining (Data Mining)
 
Python Seaborn Data Visualization
Python Seaborn Data Visualization Python Seaborn Data Visualization
Python Seaborn Data Visualization
 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
 
Data tidying with tidyr meetup
Data tidying with tidyr  meetupData tidying with tidyr  meetup
Data tidying with tidyr meetup
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
Introduction to Data Visualization
Introduction to Data VisualizationIntroduction to Data Visualization
Introduction to Data Visualization
 
Step By Step Guide to Learn R
Step By Step Guide to Learn RStep By Step Guide to Learn R
Step By Step Guide to Learn R
 
01 Data Mining: Concepts and Techniques, 2nd ed.
01 Data Mining: Concepts and Techniques, 2nd ed.01 Data Mining: Concepts and Techniques, 2nd ed.
01 Data Mining: Concepts and Techniques, 2nd ed.
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
R and Visualization: A match made in Heaven
R and Visualization: A match made in HeavenR and Visualization: A match made in Heaven
R and Visualization: A match made in Heaven
 
Exploratory data analysis data visualization
Exploratory data analysis data visualizationExploratory data analysis data visualization
Exploratory data analysis data visualization
 
Exploratory Data Analysis
Exploratory Data AnalysisExploratory Data Analysis
Exploratory Data Analysis
 

Viewers also liked

R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package ExamplesDr. Volkan OBAN
 
DATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGESDATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGESFatma ÇINAR
 
Data Visualization in R
Data Visualization in RData Visualization in R
Data Visualization in RSophia Banton
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & AdvancedSohom Ghosh
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)Dataspora
 
Iris data analysis example in R
Iris data analysis example in RIris data analysis example in R
Iris data analysis example in RDuyen Do
 
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117Myung-Hoe Huh
 
Engineering and Design Guides
Engineering and Design Guides   Engineering and Design Guides
Engineering and Design Guides Muhammad Saadullah
 
Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Dr. Volkan OBAN
 
Mini Innovation Lab: Community Foundations and Shared Data
Mini Innovation Lab:  Community Foundations and Shared DataMini Innovation Lab:  Community Foundations and Shared Data
Mini Innovation Lab: Community Foundations and Shared DataBeth Kanter
 
Grammar Of Graphics: past, present, future
Grammar Of Graphics: past, present, futureGrammar Of Graphics: past, present, future
Grammar Of Graphics: past, present, futureHadley Wickham
 
데이터 사이언티스트 키노트 Pt 20141008
데이터 사이언티스트 키노트 Pt 20141008데이터 사이언티스트 키노트 Pt 20141008
데이터 사이언티스트 키노트 Pt 20141008Myung-Hoe Huh
 
Introduzione a R
Introduzione a RIntroduzione a R
Introduzione a RMCalderisi
 
Final engineering graphics_xii_pdf_for_web
Final engineering graphics_xii_pdf_for_webFinal engineering graphics_xii_pdf_for_web
Final engineering graphics_xii_pdf_for_webCarl Witmore
 

Viewers also liked (20)

R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package Examples
 
DATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGESDATA VISUALIZATION WITH R PACKAGES
DATA VISUALIZATION WITH R PACKAGES
 
Data Visualization in R
Data Visualization in RData Visualization in R
Data Visualization in R
 
R programming Basic & Advanced
R programming Basic & AdvancedR programming Basic & Advanced
R programming Basic & Advanced
 
R programming
R programmingR programming
R programming
 
Class ppt intro to r
Class ppt intro to rClass ppt intro to r
Class ppt intro to r
 
An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)An Interactive Introduction To R (Programming Language For Statistics)
An Interactive Introduction To R (Programming Language For Statistics)
 
R learning by examples
R learning by examplesR learning by examples
R learning by examples
 
Iris data analysis example in R
Iris data analysis example in RIris data analysis example in R
Iris data analysis example in R
 
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117
법에서의 통계학 대학원 바이오정보통계학과 워크숍 20150117
 
[Week10] R graphics
[Week10] R graphics[Week10] R graphics
[Week10] R graphics
 
Engineering and Design Guides
Engineering and Design Guides   Engineering and Design Guides
Engineering and Design Guides
 
Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.Data Visualization with R.ggplot2 and its extensions examples.
Data Visualization with R.ggplot2 and its extensions examples.
 
Mini Innovation Lab: Community Foundations and Shared Data
Mini Innovation Lab:  Community Foundations and Shared DataMini Innovation Lab:  Community Foundations and Shared Data
Mini Innovation Lab: Community Foundations and Shared Data
 
Grammar Of Graphics: past, present, future
Grammar Of Graphics: past, present, futureGrammar Of Graphics: past, present, future
Grammar Of Graphics: past, present, future
 
ISOMETRIC DRAWING
ISOMETRIC DRAWINGISOMETRIC DRAWING
ISOMETRIC DRAWING
 
데이터 사이언티스트 키노트 Pt 20141008
데이터 사이언티스트 키노트 Pt 20141008데이터 사이언티스트 키노트 Pt 20141008
데이터 사이언티스트 키노트 Pt 20141008
 
Introduzione a R
Introduzione a RIntroduzione a R
Introduzione a R
 
R programming language
R programming languageR programming language
R programming language
 
Final engineering graphics_xii_pdf_for_web
Final engineering graphics_xii_pdf_for_webFinal engineering graphics_xii_pdf_for_web
Final engineering graphics_xii_pdf_for_web
 

Similar to Introduction to R Graphics with ggplot2

Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2jalle6
 
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstatssexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstatsAjay Ohri
 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with ggplot2Joaquim Silva
 
GIS 5103 – Fundamentals of GISLecture 83D GIS.docx
GIS 5103 – Fundamentals of GISLecture 83D GIS.docxGIS 5103 – Fundamentals of GISLecture 83D GIS.docx
GIS 5103 – Fundamentals of GISLecture 83D GIS.docxshericehewat
 
Scalable and Adaptive Graph Querying with MapReduce
Scalable and Adaptive Graph Querying with MapReduceScalable and Adaptive Graph Querying with MapReduce
Scalable and Adaptive Graph Querying with MapReduceKyong-Ha Lee
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationWesley Goi
 
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...Databricks
 
Simplifying Matplotlib by examples
Simplifying Matplotlib by examplesSimplifying Matplotlib by examples
Simplifying Matplotlib by examplesMohammed Ali İsmail
 
Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2yannabraham
 
[系列活動] Data exploration with modern R
[系列活動] Data exploration with modern R[系列活動] Data exploration with modern R
[系列活動] Data exploration with modern R台灣資料科學年會
 
r for data science 2. grammar of graphics (ggplot2) clean -ref
r for data science 2. grammar of graphics (ggplot2)  clean -refr for data science 2. grammar of graphics (ggplot2)  clean -ref
r for data science 2. grammar of graphics (ggplot2) clean -refMin-hyung Kim
 
Go generics. what is this fuzz about?
Go generics. what is this fuzz about?Go generics. what is this fuzz about?
Go generics. what is this fuzz about?Gabriel Habryn
 
A Generate-Test-Aggregate Parallel Programming Library on Spark
A Generate-Test-Aggregate Parallel Programming Library on SparkA Generate-Test-Aggregate Parallel Programming Library on Spark
A Generate-Test-Aggregate Parallel Programming Library on SparkYu Liu
 
A Subgraph Pattern Search over Graph Databases
A Subgraph Pattern Search over Graph DatabasesA Subgraph Pattern Search over Graph Databases
A Subgraph Pattern Search over Graph DatabasesIJMER
 
Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Sam Clifford
 
Chap 8. Optimization for training deep models
Chap 8. Optimization for training deep modelsChap 8. Optimization for training deep models
Chap 8. Optimization for training deep modelsYoung-Geun Choi
 
Formations & Deformations of Social Network Graphs
Formations & Deformations of Social Network GraphsFormations & Deformations of Social Network Graphs
Formations & Deformations of Social Network GraphsShalin Hai-Jew
 
Degree Module Breakdown
Degree Module BreakdownDegree Module Breakdown
Degree Module BreakdownDavid Horley
 

Similar to Introduction to R Graphics with ggplot2 (20)

Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2
 
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstatssexy maps comes to R - ggplot+ google maps= ggmap #rstats
sexy maps comes to R - ggplot+ google maps= ggmap #rstats
 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with ggplot2
 
Data visualization
Data visualizationData visualization
Data visualization
 
GIS 5103 – Fundamentals of GISLecture 83D GIS.docx
GIS 5103 – Fundamentals of GISLecture 83D GIS.docxGIS 5103 – Fundamentals of GISLecture 83D GIS.docx
GIS 5103 – Fundamentals of GISLecture 83D GIS.docx
 
Scalable and Adaptive Graph Querying with MapReduce
Scalable and Adaptive Graph Querying with MapReduceScalable and Adaptive Graph Querying with MapReduce
Scalable and Adaptive Graph Querying with MapReduce
 
Exploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience SpecialisationExploratory Analysis Part1 Coursera DataScience Specialisation
Exploratory Analysis Part1 Coursera DataScience Specialisation
 
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...
ADMM-Based Scalable Machine Learning on Apache Spark with Sauptik Dhar and Mo...
 
Simplifying Matplotlib by examples
Simplifying Matplotlib by examplesSimplifying Matplotlib by examples
Simplifying Matplotlib by examples
 
Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2Elegant Graphics for Data Analysis with ggplot2
Elegant Graphics for Data Analysis with ggplot2
 
[系列活動] Data exploration with modern R
[系列活動] Data exploration with modern R[系列活動] Data exploration with modern R
[系列活動] Data exploration with modern R
 
r for data science 2. grammar of graphics (ggplot2) clean -ref
r for data science 2. grammar of graphics (ggplot2)  clean -refr for data science 2. grammar of graphics (ggplot2)  clean -ref
r for data science 2. grammar of graphics (ggplot2) clean -ref
 
Go generics. what is this fuzz about?
Go generics. what is this fuzz about?Go generics. what is this fuzz about?
Go generics. what is this fuzz about?
 
A Generate-Test-Aggregate Parallel Programming Library on Spark
A Generate-Test-Aggregate Parallel Programming Library on SparkA Generate-Test-Aggregate Parallel Programming Library on Spark
A Generate-Test-Aggregate Parallel Programming Library on Spark
 
A Subgraph Pattern Search over Graph Databases
A Subgraph Pattern Search over Graph DatabasesA Subgraph Pattern Search over Graph Databases
A Subgraph Pattern Search over Graph Databases
 
06_A.pdf
06_A.pdf06_A.pdf
06_A.pdf
 
Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018
 
Chap 8. Optimization for training deep models
Chap 8. Optimization for training deep modelsChap 8. Optimization for training deep models
Chap 8. Optimization for training deep models
 
Formations & Deformations of Social Network Graphs
Formations & Deformations of Social Network GraphsFormations & Deformations of Social Network Graphs
Formations & Deformations of Social Network Graphs
 
Degree Module Breakdown
Degree Module BreakdownDegree Module Breakdown
Degree Module Breakdown
 

More from izahn

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SASizahn
 
Stata datman
Stata datmanStata datman
Stata datmanizahn
 
Graphing stata (2 hour course)
Graphing stata (2 hour course)Graphing stata (2 hour course)
Graphing stata (2 hour course)izahn
 
Introduction to Stata
Introduction to StataIntroduction to Stata
Introduction to Stataizahn
 
Stata statistics
Stata statisticsStata statistics
Stata statisticsizahn
 
Data management in Stata
Data management in StataData management in Stata
Data management in Stataizahn
 
R Regression Models with Zelig
R Regression Models with ZeligR Regression Models with Zelig
R Regression Models with Zeligizahn
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environmentizahn
 

More from izahn (8)

Introduction to SAS
Introduction to SASIntroduction to SAS
Introduction to SAS
 
Stata datman
Stata datmanStata datman
Stata datman
 
Graphing stata (2 hour course)
Graphing stata (2 hour course)Graphing stata (2 hour course)
Graphing stata (2 hour course)
 
Introduction to Stata
Introduction to StataIntroduction to Stata
Introduction to Stata
 
Stata statistics
Stata statisticsStata statistics
Stata statistics
 
Data management in Stata
Data management in StataData management in Stata
Data management in Stata
 
R Regression Models with Zelig
R Regression Models with ZeligR Regression Models with Zelig
R Regression Models with Zelig
 
Introduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing EnvironmentIntroduction to the R Statistical Computing Environment
Introduction to the R Statistical Computing Environment
 

Recently uploaded

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 

Recently uploaded (20)

[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 

Introduction to R Graphics with ggplot2

  • 1. Introduciton to R Graphics with ggplot2 Harvard MIT Data Center May 10, 2013 The Institute for Quantitative Social Science at Harvard University (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 1 / 60
  • 2. Outline 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 2 / 60
  • 3. Introduction Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 3 / 60
  • 4. Introduction Class Files And Administrative Details User name: dataclass Password: dataclass Copy Rgraphics folder from shared drive to your desktop Class Structure and Organization Ask questions at any time. Really! Collaboration is encouraged This is your class! Special requests are encouraged This is an intermediate R course Assumes working knowledge of R Relatively fast-paced Focus is on ggplot2 graphics–other packages will not be covered (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 4 / 60
  • 5. Introduction Starting A The End My goal: by the end of the workshop you will be able to reproduce this graphic from the Economist: (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 5 / 60
  • 6. Introduction Why ggplot2? Advantages of ggplot2 Consistent underlying grammar of graphics (Wilkinson, 2005) Plot specification at a high level of abstraction Very flexible Theme system for polishing plot appearance Active maintenance and development–getting better all the time Many users, active mailing list Things you cannot do With ggplot2 3-dimensional graphics Graph-theory type graphs (nodes/edges layout) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 6 / 60
  • 7. Introduction What Is The Grammar Of Graphics? The basic idea: independently specify plot building blocks Anatomy of a plot: data aesthetic mapping geometric object statistical transformations scales coordinate system position adjustments faceting (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 7 / 60
  • 8. Introduction Example data I: mtcars > print(head(mtcars, 4)) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.62 16.5 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.88 17.0 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.21 19.4 1 0 3 1 mpg Miles/(US) gallon cyl Number of cylinders disp Displacement (cu.in.) hp Gross horsepower drat Rear axle ratio wt Weight (lb/1000) qsec 1/4 mile time vs V/S am Transmission (0 = automatic, 1 = manual) gear Number of forward gears carb Number of carburetors (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 8 / 60
  • 9. Introduction ggplot2 VS Base Graphics Compared to base graphics, ggplot2 is more verbose for simple / canned graphics is less verbose for complex / custom graphics does not have methods (data should always be in a data.frame) uses a different system for adding plot elements (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 9 / 60
  • 10. Introduction ggplot2 VS Base Graphics Base graphics VS ggplot for simple graphs: ggplot(mtcars, aes(x = mpg)) + hist(mtcars$mpg) geom_histogram(binwidth = 5) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 10 / 60
  • 11. Introduction ggplot2 VS Base Graphics Base graphics VS ggplot for complex graphs: par(mar = c(4,4,.1,.1)) plot(mpg ~ hp, data=subset(mtcars, am==1), ggplot(mtcars, aes(x=hp, xlim=c(50, 450),ylim=c(5, 40)) y=mpg, points(mpg ~ hp, col="red", color=factor(am)))+ data=subset(mtcars, am==0)) geom_point() legend(350, 40, c("1", "0"), title="am", col=c("black", "red"), pch=c(1, 1)) # (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 11 / 60
  • 12. Geometric Objects And Aesthetics Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 12 / 60
  • 13. Geometric Objects And Aesthetics Aesthetic Mapping In ggplot land aesthetic means "something you can see" Examples include: position (i.e., on the x and y axes) color ("outside" color) fill ("inside" color) shape (of points) linetype size Each type of geom accepts only a subset of all aesthetics–refer to the geom help pages to see what mappings each geom accepts Aesthetic mappings are set with the aes() function (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 13 / 60
  • 14. Geometric Objects And Aesthetics Geometic Objects (geom) Geometric objects are the actual marks we put on a plot Examples include: points (geom_point, for scatter plots, dot plots, etc) lines (geom_line, for time series, trend lines, etc) boxplot (geom_boxplot, for, well, boxplots!) A plot must have at least one geom; there is no upper limit Add a geom to a plot using the + operator > geoms can help.search("geom_", package = "ggplot2") You <- get a list of available geometric objects: > geoms$matches[1:4, 1:2] topic title [1,] "geom_abline" "Line specified by slope and intercept." [2,] "geom_area" "Area plot." [3,] "geom_bar" "Bars, rectangles with bases on x-axis" [4,] "geom_bin2d" "Add heatmap of 2d bin counts." (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 14 / 60
  • 15. Geometric Objects And Aesthetics Points (Scatterplot) Now that we know about geometric objects and aesthetic mapping, we can make a ggplot ggplot(mtcars, aes(x = hp, y = mpg)) + ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point() geom_point(aes(y=log10(mpg))) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 15 / 60
  • 16. Geometric Objects And Aesthetics Lines (Prediction Line) A plot constructed with ggplot can have more than one geom Our hp vs mpg plot could use a regression line: mtcars$pred.mpg <- predict(lm(mpg ~ hp, data = mtcars)) p1 <- ggplot(mtcars, aes(x = hp, y = mpg)) p1 + geom_point(aes(color = wt)) + geom_line(aes(y = pred.mpg)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 16 / 60
  • 17. Geometric Objects And Aesthetics Smoothers Not all geometric objects are simple shapes–the smooth geom includes a line and a ribbon p2 <- ggplot(mtcars, aes(x = hp, y = mpg)) p2 + geom_point(aes(color = wt)) + geom_smooth() (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 17 / 60
  • 18. Geometric Objects And Aesthetics Text (Label Points) Each geom accepts a particualar set of mappings–for example geom_text() accepts a labels mapping p2 + geom_point(aes(color = wt)) + geom_smooth() + geom_text(aes(label=rownames(mtcars)), size=2) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 18 / 60
  • 19. Geometric Objects And Aesthetics Aesthetic Mapping VS Assignment Note that variables are mapped to aesthetics with the aes() function, while fixed aesthetics are set outside the aes() call This sometimes leads to confusion, as in this example: ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(size = 2),# incorrect! 2 is not a variable color="red") # this is fine -- all points red (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 19 / 60
  • 20. Geometric Objects And Aesthetics Mapping Variables To Other Aesthetics Other aesthetics are mapped in the same way as x and y in the previous example ggplot(mtcars, aes(x = hp, y = mpg)) + geom_point(aes(color=wt, shape = factor(am))) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 20 / 60
  • 21. Geometric Objects And Aesthetics Exercise I 1 Create a scatter plot with displacement on the x axis and horse power on the y axis 2 Color the points in the previous plot blue 3 Color the points in the previous plot according to miles per gallon 4 Exercise I prototype :noexport: (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 21 / 60
  • 22. Statistical Transformations Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 22 / 60
  • 23. Statistical Transformations Statistical Transformations Some plot types (such as scatterplots) do not require transformations–each point is plotted at x and y coordinates equal to the original value Other plots, such as boxplots, histograms, prediction lines etc. require statistical transformations For a boxplot the y values must be transformed to the median and 1.5(IQR) For a smoother smother the y values must be transformed into predicted values Each geom has a default statistic, but these can be changed > args(geom_bar) the default statistic for geom_bar is stat_bin For example, function (mapping = NULL, data = NULL, stat = "bin", position = "stack", ...) NULL > # ?stat_bin > (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 23 / 60
  • 24. Statistical Transformations Setting Statistical Transformation Arguments Arguments to stat_ functions are passed through geom_ functions Slightly annoying because in order to change it you have to first determine which stat the geom uses, then determine the arguments to that stat ggplot(mtcars, aes(x = mpg)) + ggplot(mtcars, aes(x = mpg)) + geom_bar() geom_bar(stat = "bin", binwidth=4) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 24 / 60
  • 25. Statistical Transformations Changing The Statistical Transformation Sometimes the default statistical transformation is not what you need > (mtc.sum <-case with pre-summarized mtcars["gear"], FUN=mean)) Often the aggregate(mtcars["mpg"], data gear mpg 1 3 16.1 2 4 24.5 3 5 21.4 > ggplot(mtc.sum, aes(x=gear, y=mpg)) + geom_bar() ggplot(mtc.sum, aes(x=gear, y=mpg)) + Mapping a variable to y and also geom_bar(stat="identity") using stat="bin". Error in pmin(y, 0) : object ’y’ not found . (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 25 / 60
  • 26. Statistical Transformations Exercise II 1 Create boxplots of mpg by gear 2 Overlay points on top of the box plots 3 Create a scatter plot of weight vs. horsepower 4 Overlay a linear regression line on top of the scatter plot (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 26 / 60
  • 27. Scales Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 27 / 60
  • 28. Scales Scales: Controlling Aesthetic Mapping In ggplot2 scales include position color and fill size shape line type Modified with scale_<aesthetic>_<type> (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 28 / 60
  • 29. Scales Common Scale Arguments name: the first argument gives the axis or legend title limits: the minimum and maximum of the scale breaks: the points along the scale where labels should appear labels: the labels that appear at each break (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 29 / 60
  • 30. Scales Scale Modification Examples p6 <- ggplot(mtcars, aes(x = factor(gear), y = mpg)) p6 + geom_point(aes(color = wt)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 30 / 60
  • 31. Scales Scale breaks and labels p7 <- p6 + geom_point(aes(color = wt)) + scale_x_discrete("Number of Gears", breaks = c("3", "4", "5"), labels = c("Three", "Four", "Five")) p7 + scale_color_continuous("Weight", breaks = with(mtcars, c(min(wt), median(wt), max(wt labels = c("Light", "Medium", "Heavy")) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 31 / 60
  • 32. Scales Scale breaks and labels p7 + scale_color_continuous("Weight", breaks = with(mtcars, c(min(wt), median(wt), max(wt labels = c("Light", "Medium", "Heavy"), low = "black", high = "gray80") (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 32 / 60
  • 33. Scales Using different color scales p7 + scale_color_gradient2("Weight", breaks = with(mtcars, c(min(wt), median(wt), max(w labels = c("Light", "Medium", "Heavy"), low = "blue", mid = "black", high = "red", midpoint = median(mtcars$wt)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 33 / 60
  • 34. Scales Scale Modification Examples p8 <- ggplot(mtcars, aes(x = factor(gear), y = mpg)) p8 + geom_point(aes(size = wt)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 34 / 60
  • 35. Scales Scale range p8 + geom_point(aes(size = wt)) + scale_size_continuous("Weight", range = c(2, 10)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 35 / 60
  • 36. Scales Available Scales Partial combination matrix of available scales Scale Types Examples scale_color_ identity scale_fill_continuous scale_fill_ manual scale_color_discrete scale_size_ continuous scale_size_manual discrete scale_size_discrete scale_shape_ discrete scale_shape_discrete scale_linetype_ identity scale_shape_manual manual scale_linetype_discrete scale_x_ continuous scale_x_continuous scale_y_ discrete scale_y_discrete reverse scale_x_log log scale_y_reverse date scale_x_date datetime scale_y_datetime (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 36 / 60
  • 37. Scales Exercise III 1 Experiment with color, size, and shape aesthetics / scales 2 What happens when you map more than one aesthetic to a variable? 3 Which aesthetics are good for continuous variables? Which work better for discrete variables? (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 37 / 60
  • 38. Faceting Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 38 / 60
  • 39. Faceting Faceting Faceting is ggplot2 parlance for small multiples The idea is to create separate graphs for subsets of data ggplot2 offers two functions for creating small multiples: 1 facet_wrap(): define subsets as the levels of a single grouping variable 2 facet_grid(): define subsets as the crossing of two grouping variables Facilitates comparison among plots, not just of geoms within a plot (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 39 / 60
  • 40. Faceting Example Data II: Titanic > titanic <- read.csv(paste("http://biostat.mc.vanderbilt.edu/", + "wiki/pub/Main/", + "DataSets/titanic3.csv", sep = "")) > variable description pclass Passenger Class survival Survival name Name sex Sex age Age sibsp Number of Siblings/Spouses Aboard parch Number of Parents/Children Aboard ticket Ticket Number fare Passenger Fare cabin Cabin embarked Port of Embarkation boat Lifeboat body Body Identification Number home.dest Home/Destination (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 40 / 60
  • 41. Faceting What happened on the Titanic? Start by transforming the data into a more usable form: > library(plyr) > titanic$age.g <- as.integer(cut(titanic$age, 10)) > td <- ddply(titanic, c("pclass", "age.g", "sex"), + summarize, + ps = length(survived[survived == 1])/length(survived)) > (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 41 / 60
  • 42. Faceting What happened on the Titanic? Basic scatter plot: p8 <- ggplot(td, aes(x = age.g, y = ps)) p8 + geom_point() Why do we have two clusters of points? (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 42 / 60
  • 43. Faceting What happened on the Titanic? Use the techniques we already know (aesthetic mapping): p8 <- ggplot(td, aes(x = age.g, y = ps)) p8 + geom_point(aes(color = pclass, shape = sex)) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 43 / 60
  • 44. Faceting What happened on the Titanic? Use faceting in one dimension p8 + geom_point(aes(shape = sex)) + facet_wrap(~ pclass) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 44 / 60
  • 45. Faceting What happened on the Titanic? Use faceting in two dimensions p8 + geom_point() + facet_grid(sex ~ pclass) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 45 / 60
  • 46. Themes Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 46 / 60
  • 47. Themes Themes The ggplot2 theme system handles non-data plot elements such as Axis labels Plot background Facet label backround Legend appearance Two built-in themes: theme_gray() (default) theme_bw() More available on the wiki: https://github.com/hadley/ggplot2/wiki/Themes (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 47 / 60
  • 48. Themes Overriding theme defaults Specific theme elements can be overridden using theme() Example: p7 + theme(plot.background = element_rect( fill = "white", colour = "gray40")) You can see available options by printing theme_gray() or theme_bw() (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 48 / 60
  • 49. Themes Creating and saving new themes You can create new themes, as in the following example: theme_new <- theme_bw() + theme(text=element_text(size = 12, family = ""), axis.text.x = element_text(colour = "red"), panel.background = element_rect(fill = "pink")) p7 + theme_new (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 49 / 60
  • 50. The #1 FAQ Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 50 / 60
  • 51. The #1 FAQ Map Aesthetic To Different Columns The most frequently asked question goes something like this: I have two variables in my data.frame, and I’d like to plot them as separate points, with different color depending on which variable it is. How do I do that? ggplot(mtcars, aes(x=wt)) + geom_point(aes(y=disp), color="red") + library(reshape2) geom_point(aes(y=hp), color="blue") mtc.m <- melt(mtcars, measure.vars=c("mpg", "hp")) ggplot(mtc.m, aes(x=wt, y=value, # color=variable)) + geom_point() (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 51 / 60
  • 52. Putting It All Together Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 52 / 60
  • 53. Putting It All Together Challenge: Recreate This Economist Graph Data The data is available in the EconomistData.csv file. Original sources are http://www.transparency.org/content/download/64476/1031428 http://hdrstats.undp.org/en/indicators/display_cf_xls_indicator.cfm?indicator_id=103106&lang=en (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 53 / 60
  • 54. Putting It All Together Challenge Solution 1 Load the data: dat <- read.csv("dataSets/EconomistData.csv") 1 Create basic scatter plot pc1 <- ggplot(dat, aes(x = CPI, y = HDI, color = Region)) (pc1 <- pc1 + geom_point(shape = 1)) # (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 54 / 60
  • 55. Putting It All Together Challenge Solution Add labels label.these <- c("Congo", "Sudan", "Afghanistan", "Greece", "China", "India", "Rwanda", "Spain", "France", "United States", "Japan", "Norway", "Singapore") (pc2 <- pc1 + geom_text(aes(label = Country), color = "black", size = 3, hjust = 1.1, data = dat[dat$Country %in% label.these, ])) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 55 / 60
  • 56. Putting It All Together Challenge Solution Add smoothing line (pc3 <- pc2 + geom_smooth(aes(group = 1), method = "lm", color = "black", formula = y~ poly(x, 2), se = FALSE)) # (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 56 / 60
  • 57. Putting It All Together Challenge Solution Finishing touches (pc4 <- pc3 + theme_bw() + scale_x_continuous("Corruption Perceptions Index, 2011n(10 = least corrup scale_y_continuous("Human Development Index, 2011n(1 = best)") + theme(legend.position = "top", legend.direction = "horizontal")) (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 57 / 60
  • 58. Wrap-up Topic 1 Introduction 2 Geometric Objects And Aesthetics 3 Statistical Transformations 4 Scales 5 Faceting 6 Themes 7 The #1 FAQ 8 Putting It All Together 9 Wrap-up (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 58 / 60
  • 59. Wrap-up Help Us Make This Workshop Even Better! Please take a moment to fill out a very short feedback form These workshops exist for you – tell us what you need! http://tinyurl.com/R-graphics-feedback (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 59 / 60
  • 60. Wrap-up Additional resources ggplot2 resources Mailing list: http://groups.google.com/group/ggplot2 Wiki: https://github.com/hadley/ggplot2/wiki Website: http://had.co.nz/ggplot2/ StackOverflow: http://stackoverflow.com/questions/tagged/ggplot IQSS resources Research technology consulting: http://projects.iq.harvard.edu/rtc Workshops: http://projects.iq.harvard.edu/rtc/filter_by/workshops (Harvard MIT Data Center) Introduciton to R Graphics with ggplot2 May 10, 2013 60 / 60