Lecture 07

StatOracle–07 Data Visualization with the ggplot2 Package

Using ggplot2 in R for building informative statistical graphics in zoology and ecology.

Data Visualization with the ggplot2 Package 7.1. The Grammar of Graphics ggplot2 The ggplot2 package has become the most popular tool for creating plots in R. The abbreviation "gg" in its name comes from the concept of the Grammar of Graphics, proposed in the book by Leland Wilkinson (Wilkinson, Leland. 2005. The Grammar of Graphics (Statistics and Computing). Berlin, Heidelberg: Springer-Verlag). The essence of this grammar is to describe statistical diagrams using a special language. This concept was developed by Hadley Wickham, who proposed the ggplot2 package in 2010 (Wickham, Hadley. 2010. “A Layered Grammar of Graphics.” Journal of Computational and Graphical Statistics 19 (1): 3–28). The description of the ggplot2 package can be found online here. A "cheat sheet" to help work with this package is provided below. Note: clicking the mouse on the image will bring up a magnified copy, which will be much easier to read. Fig. 7.1.1. The first page of the ggplot2 cheat sheet (clickable!) Fig. 7.1.2. The second page of the ggplot2 cheat sheet (clickable!) A characteristic feature of ggplot2 is the "layered" construction of graphics. The main layers of an image in this package are shown in Table 7.1.1. Note: this table should be read from bottom to top; the base level, the data level, is shown at the very bottom. Each subsequent layer is superimposed on the previous ones; the layer with the diagram titles, axis labels, etc., should be located higher. Layer names, such as "aesthetics," may seem unclear; you just need to get used to them. Table 7.1.1. Image layers in ggplot2 (in order from bottom to top)

Layer

In the Statistics / Basic Statistics menu, select the appropriate option for sample comparison using Student's t-test. The dialog box that opens offers four options for such comparison.

Description

Score

7

Topics
(Themes)

The styling of the image from a predefined list

(Themes)

6

Let us slightly complicate the task. Table 9.1.1 shows the coordinates of 10 points in 10-dimensional space. The first three points and the first two measurements are the example we have just considered.
(Coordinates)

The image space

(Coordinates)

5

1.7 Distributions, statistics, and parameters
(Statistics)

A generalized description of the data

(Statistics)

4

Facets
(Facets)

Images that differ by one variable (categorized plots)

(Facets)

3

Geometry
(Geometries)

Elements used to represent variables

(Geometries)

2

Aesthetics
(Aestetics)

The scales and variables on which the data are built

(Aestetics)

1

Data
(Data)

The data set from which the images are built

(Data)