A Report Generated by knitr

This report is automatically generated with the R package knitr (version `1.5`) .

    # Chapter 17 - Creating Faceted Graphics with Lattice Creating a Lattice Plot
    str(mtcars)

    ## 'data.frame':	32 obs. of  11 variables:
    ##  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
    ##  $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
    ##  $ disp: num  160 160 108 258 360 ...
    ##  $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
    ##  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
    ##  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
    ##  $ qsec: num  16.5 17 18.6 19.4 17 ...
    ##  $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
    ##  $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
    ##  $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
    ##  $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

    ## Loading the lattice package
    library("lattice")
    ## Making a lattice scatterplot
    xyplot(mpg ~ hp | factor(cyl), data = mtcars)

    ## Adding trend lines
    xyplot(mpg ~ hp | factor(cyl), data = mtcars, type = c("p", "r"))

    # Changing Plot Options Adding titles and labels
    xyplot(mpg ~ hp | factor(cyl), data = mtcars, type = c("p", "r"), main = "Fuel economy vs. Performance",
        xlab = "Performance (horse power)", ylab = "Fuel economy (miles per gallon)", )

    xyplot(mpg ~ hp | factor(cyl), data = mtcars, type = c("p", "r"), main = list(label = "Fuel economy vs. Performance given Number of Cylinders",
        cex = 0.75))

    ## Changing the font size of titles and labels
    xyplot(mpg ~ hp | factor(cyl), data = mtcars, type = c("p", "r"), main = list(label = "Fuel economy vs. Performance given Number of Cylinders",
        cex = 0.75), xlab = list(label = "Performance (horse power)", cex = 0.75), ylab = list(label = "Fuel economy (miles per gallon)",
        cex = 0.75), scales = list(cex = 0.5))

    ## Using themes to modify plot options
    xyplot(mpg ~ hp | factor(cyl), data = mtcars, type = c("p", "r"), par.settings = simpleTheme(col = "red",
        col.line = "blue"))

    # Plotting Different Types Making a bar chart
    mtcars$cars <- rownames(mtcars)
    barchart(cars ~ mpg | factor(cyl), data = mtcars, main = "barchart", scales = list(cex = 0.5),
        layout = c(3, 1))

    ## Making a box-and-whisker plot
    bwplot(~hp | factor(cyl), data = mtcars, main = "bwplot")

    # Plotting Data in Groups Using data in tall format
    str(longley)

    ## 'data.frame':	16 obs. of  7 variables:
    ##  $ GNP.deflator: num  83 88.5 88.2 89.5 96.2 ...
    ##  $ GNP         : num  234 259 258 285 329 ...
    ##  $ Unemployed  : num  236 232 368 335 210 ...
    ##  $ Armed.Forces: num  159 146 162 165 310 ...
    ##  $ Population  : num  108 109 110 111 112 ...
    ##  $ Year        : int  1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 ...
    ##  $ Employed    : num  60.3 61.1 60.2 61.2 63.2 ...

    library("reshape2")
    mlongley <- melt(longley, id.vars = "Year")
    str(mlongley)

    ## 'data.frame':	96 obs. of  3 variables:
    ##  $ Year    : int  1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 ...
    ##  $ variable: Factor w/ 6 levels "GNP.deflator",..: 1 1 1 1 1 1 1 1 1 1 ...
    ##  $ value   : num  83 88.5 88.2 89.5 96.2 ...

    xyplot(value ~ Year | variable, data = mlongley, layout = c(6, 1), par.strip.text = list(cex = 0.7),
        scales = list(cex = 0.7))

    ## Creating a chart with groups
    mtcars$cars <- rownames(mtcars)
    mtcars$am <- with(mtcars, ifelse(am == 0, "Automatic", "Manual"))
    barchart(cars ~ mpg | factor(cyl), data = mtcars, group = am, scales = list(cex = 0.5), layout = c(3,
        1), )

    ## Adding a key
    barchart(cars ~ mpg | factor(cyl), data = mtcars, main = "barchart with groups", group = am,
        auto.key = TRUE, par.settings = simpleTheme(col = c("grey80", "grey20")), scales = list(cex = 0.5),
        layout = c(3, 1))

    # Printing and Saving a Lattice Plot Assigning a lattice plot to an object
    my.plot <- xyplot(mpg ~ hp | cyl, data = mtcars)
    class(my.plot)

    ## [1] "trellis"

    ## Printing a lattice plot in a script
    xyplot(mpg ~ hp | cyl, data = mtcars)

    my.plot <- xyplot(mpg ~ hp | cyl, data = mtcars)
    print(my.plot)

    ## Saving a lattice plot to file
    setwd("~/")
    trellis.device(device = "png", filename = "xyplot.png")
    print(my.plot)
    dev.off()

    ## pdf
    ##   2

The R session information (including the OS info, R version and all packages used):

    sessionInfo()

    ## R version 3.0.2 (2013-09-25)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ##
    ## locale:
    ## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252
    ## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
    ## [5] LC_TIME=English_United Kingdom.1252
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ##  [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1      reshape2_1.2.2       sos_1.3-8
    ##  [5] brew_1.0-6           stringr_0.6.2        knitr_1.5            plyr_1.8
    ##  [9] Revobase_7.1.0       RevoMods_7.1.0       RevoScaleR_7.1.0     lattice_0.20-27
    ## [13] rpart_4.1-2
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] codetools_0.2-8    colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4
    ##  [5] evaluate_0.5.1     foreach_1.4.1      formatR_0.10       fortunes_1.5-2
    ##  [9] grid_3.0.2         gtable_0.1.2       highr_0.3          iterators_1.0.6
    ## [13] labeling_0.2       MASS_7.3-29        munsell_0.4.2      proto_0.3-10
    ## [17] RColorBrewer_1.0-5 scales_0.2.3       tools_3.0.2

    Sys.time()

    ## [1] "2014-05-13 15:06:28 BST"