Skip to content

ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 1)

June 28, 2009

The data visualization package lattice is part of the base R distribution, and like ggplot2 is built on Grid graphics engine. Deepayan Sarkar’s (the developer of lattice) book Lattice: Multivariate Data Visualization with R gives a detailed overview of how the package works. All the figures and code used to produce them is also available on the book website.

In order to give those interested an option to compare graphs produced by ggplot2 and lattice, I will attempt to recreate the book’s lattice graphs in ggplot2. There are 14 chapters in the book, so this means that there would be at least 13 more posts on the subject.

The output of both packages can be tweaked so that the graphs would look similar if not the same, however for the purposes of comparison, the standard settings (at least in ggplot2) are used when possible. The code used to create the images is in separate paragraphs, allowing easy comparison, and by clicking on the thumbnail, a bigger image file is also available.


Chapter 1 – Introduction

Topics covered:

  • Basic usage; high level functions
  • Conditioning
  • Superposition (a.k.a. grouping)
  • “trellis” objects

Figure 1.1

> library(lattice)
> library(ggplot2)
> data(Chem97, package = "mlmRev")

lattice

> pl <- histogram(~gcsescore | factor(score), data = Chem97)
> print(pl)

ggplot2

> pg <- ggplot(Chem97, aes(gcsescore)) + geom_histogram(binwidth = 0.5) +
+     facet_wrap(~score)
> print(pg)
Note ggplot2 uses counts, not percentages by default.
Note ggplot2 plots the facets starting from top-left, lattice starts from bottom-left.

chapter01-01_01_r_small.png

Figure 1.2

lattice

> pl <- densityplot(~gcsescore | factor(score), data = Chem97,
+     plot.points = FALSE, ref = TRUE)
> print(pl)

ggplot2

> pg <- ggplot(Chem97, aes(gcsescore)) + stat_density(geom = "path",
+     position = "identity") + facet_wrap(~score)
> print(pg)

chapter01-01_02_r_small.png

Figure 1.3

lattice

> pl <- densityplot(~gcsescore, data = Chem97, groups = score,
+     plot.points = FALSE, ref = TRUE, auto.key = list(columns = 3))
> print(pl)

ggplot2

> pg <- ggplot(Chem97, aes(gcsescore)) + stat_density(geom = "path",
+     position = "identity", aes(colour = factor(score)))
> print(pg)

chapter01-01_03_l_small.pngchapter01-01_03_r_small.png

26 Comments leave one →
  1. June 29, 2009 1:36 am

    Awesome idea! Thanks for doing this.

  2. June 29, 2009 2:05 am

    I concur with Hadley. I’ve been doing most of my R graphics with the help of this book, so seeing the ggplot2 versions is a very good learning tool!

  3. June 29, 2009 3:50 pm

    +1 awesome!!!

  4. kaomte permalink
    August 8, 2009 11:45 pm

    Much appreciate. This is very helpful.

  5. R-ing permalink
    June 14, 2010 10:24 pm

    Thank you! this is brilliant

  6. Ron Thornton permalink
    March 18, 2013 3:34 am

    This series is excellent, thanks for making the effort.

  7. Juan permalink
    May 5, 2015 2:32 pm

    Hello.

    Could you show an easy way to produce a scatter plot with error bars associated to each point, please?
    From a dataframe with 4 columns: X, Y, errX, errY.
    I’ve only seen complicated methods.

  8. joe permalink
    August 16, 2015 1:17 am

    It would be great to see a newer version (specially in pdf) updated with lattice, latticeextra, ggplot and ggextra, things such as themes, error bars…

  9. March 15, 2017 3:21 pm

    fantastic – was just starting to convert those in both Bates excellent Lme4 book chapter 4 and also some of those in another excellent book Galecki & Burzykowski’s Linear mixed effect models

Trackbacks

  1. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 5) « Learning R
  2. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 6) « Learning R
  3. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 7) « Learning R
  4. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 8) « Learning R
  5. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 9) « Learning R
  6. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 10) « Learning R
  7. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 11) « Learning R
  8. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 13) « Learning R
  9. ggplot2 Version of Figures in “Lattice: Multivariate Data Visualization with R” (Part 13) « Learning R
  10. Time for Some Lattice « Amanda's Tea Room 阿勳茶室
  11. The R-Podcast Episode 8: Visualization with ggplot2 » The R-Podcast
  12. The R-Podcast Screencast 2: Visualization with ggplot2 » The R-Podcast
  13. Graphics: an important issue to communicate | FreshBiostats
  14. Multivariate weather statistics | Europe by numbeRs
  15. Comparing lattice and ggplot | Diogo Ferrari
  16. Curated list of R tutorials for Data Science – the data science blog
  17. 9 Useful R Data Visualization Libraries for Any Discipline | THE-R

Leave a comment