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 speciļ¬cation at a high level of abstraction
            Very ļ¬‚exible
            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 diļ¬€erent 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)
            ļ¬ll ("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 ļ¬xed 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 ļ¬rst
      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 ļ¬ll
             size
             shape
             line type
      Modiļ¬ed 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 ļ¬rst 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 Modiļ¬cation 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 diļ¬€erent 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 Modiļ¬cation 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_ļ¬ll_continuous
                     scale_ļ¬ll_        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 oļ¬€ers two functions for creating small multiples:
          1   facet_wrap(): deļ¬ne subsets as the levels of a single grouping variable
          2   facet_grid(): deļ¬ne 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 Identiļ¬cation 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
      Speciļ¬c 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 Diļ¬€erent 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
 diļ¬€erent 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 ļ¬le. 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 ļ¬ll 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/
             StackOverļ¬‚ow:
             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

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).pptxMalla Reddy University
Ā 
Data tidying with tidyr meetup
Data tidying with tidyr  meetupData tidying with tidyr  meetup
Data tidying with tidyr meetupMatthew Samelson
Ā 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with RShareThis
Ā 
Descriptive Statistics with R
Descriptive Statistics with RDescriptive Statistics with R
Descriptive Statistics with RKazuki Yoshida
Ā 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
Ā 
Introduction To Data Science Using R
Introduction To Data Science Using RIntroduction To Data Science Using R
Introduction To Data Science Using RANURAG SINGH
Ā 
Introduction to R
Introduction to RIntroduction to R
Introduction to RKazuki Yoshida
Ā 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With REdureka!
Ā 
Classification and Clustering
Classification and ClusteringClassification and Clustering
Classification and ClusteringEng Teong Cheah
Ā 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with PythonDavis David
Ā 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
Ā 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to RstudioOlga Scrivner
Ā 
R programming presentation
R programming presentationR programming presentation
R programming presentationAkshat Sharma
Ā 
CART ā€“ Classification & Regression Trees
CART ā€“ Classification & Regression TreesCART ā€“ Classification & Regression Trees
CART ā€“ Classification & Regression TreesHemant Chetwani
Ā 
Principal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesPrincipal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesAbhishekKumar4995
Ā 
Data preprocessing
Data preprocessingData preprocessing
Data preprocessingankur bhalla
Ā 
Lect5 principal component analysis
Lect5 principal component analysisLect5 principal component analysis
Lect5 principal component analysishktripathy
Ā 

What's hot (20)

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
Ā 
Data tidying with tidyr meetup
Data tidying with tidyr  meetupData tidying with tidyr  meetup
Data tidying with tidyr meetup
Ā 
Statistics for data science
Statistics for data science Statistics for data science
Statistics for data science
Ā 
Data analysis with R
Data analysis with RData analysis with R
Data analysis with R
Ā 
Descriptive Statistics with R
Descriptive Statistics with RDescriptive Statistics with R
Descriptive Statistics with R
Ā 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
Ā 
Cluster Analysis
Cluster AnalysisCluster Analysis
Cluster Analysis
Ā 
Introduction To Data Science Using R
Introduction To Data Science Using RIntroduction To Data Science Using R
Introduction To Data Science Using R
Ā 
Introduction to R
Introduction to RIntroduction to R
Introduction to R
Ā 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
Ā 
Classification and Clustering
Classification and ClusteringClassification and Clustering
Classification and Clustering
Ā 
Exploratory data analysis with Python
Exploratory data analysis with PythonExploratory data analysis with Python
Exploratory data analysis with Python
Ā 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
Ā 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to Rstudio
Ā 
R programming presentation
R programming presentationR programming presentation
R programming presentation
Ā 
CART ā€“ Classification & Regression Trees
CART ā€“ Classification & Regression TreesCART ā€“ Classification & Regression Trees
CART ā€“ Classification & Regression Trees
Ā 
Principal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT SlidesPrincipal Component Analysis (PCA) and LDA PPT Slides
Principal Component Analysis (PCA) and LDA PPT Slides
Ā 
Data Management in R
Data Management in RData Management in R
Data Management in R
Ā 
Data preprocessing
Data preprocessingData preprocessing
Data preprocessing
Ā 
Lect5 principal component analysis
Lect5 principal component analysisLect5 principal component analysis
Lect5 principal component analysis
Ā 

Viewers also liked

R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package ExamplesDr. Volkan OBAN
Ā 
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!
Ā 
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
Ā 
R learning by examples
R learning by examplesR learning by examples
R learning by examplesMichelle Darling
Ā 
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
Ā 
[Week10] R graphics
[Week10] R graphics[Week10] R graphics
[Week10] R graphicsneuroassociates
Ā 
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
Ā 
R programming language
R programming languageR programming language
R programming languageAlberto Minetti
Ā 

Viewers also liked (20)

R-ggplot2 package Examples
R-ggplot2 package ExamplesR-ggplot2 package Examples
R-ggplot2 package Examples
Ā 
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
Ā 
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
Ā 

Similar to Introduction to R Graphics with ggplot2

Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2jalle6
Ā 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with ggplot2Joaquim Silva
Ā 
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
Ā 
Data visualization
Data visualizationData visualization
Data visualizationVivian S. Zhang
Ā 
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
Ā 
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
Ā 
Spatial visualization with ggplot2
Spatial visualization with ggplot2Spatial visualization with ggplot2
Spatial visualization with 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
Ā 
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

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
Ā 
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
Ā 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
Ā 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
Ā 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
Ā 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
Ā 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
Ā 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhisoniya singh
Ā 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
Ā 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
Ā 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
Ā 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
Ā 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
Ā 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
Ā 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Ā 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
Ā 
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 āœ“Call Girls In Kalyan ( Mumbai ) secure service
Ā 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Ā 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
Ā 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
Ā 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
Ā 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
Ā 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Ā 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
Ā 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
Ā 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
Ā 
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | DelhiFULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY šŸ” 8264348440 šŸ” Call Girls in Diplomatic Enclave | Delhi
Ā 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
Ā 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Ā 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
Ā 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
Ā 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
Ā 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Ā 

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 speciļ¬cation at a high level of abstraction Very ļ¬‚exible 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 diļ¬€erent 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) ļ¬ll ("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 ļ¬xed 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 ļ¬rst 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 ļ¬ll size shape line type Modiļ¬ed 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 ļ¬rst 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 Modiļ¬cation 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 diļ¬€erent 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 Modiļ¬cation 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_ļ¬ll_continuous scale_ļ¬ll_ 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 oļ¬€ers two functions for creating small multiples: 1 facet_wrap(): deļ¬ne subsets as the levels of a single grouping variable 2 facet_grid(): deļ¬ne 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 Identiļ¬cation 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 Speciļ¬c 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 Diļ¬€erent 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 diļ¬€erent 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 ļ¬le. 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 ļ¬ll 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/ StackOverļ¬‚ow: 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