The Grammar of Graphics
plot() VS ggplot()
In these notes, we will look at some differences between the base
plot() function and
ggplot(). First, ensure that the
tidyverse package is loaded.
We will use the famous Iris dataset which you can read about here. It consists of 4 measurements: sepal length, sepal width, petal length and petal width across 3 different species of Iris: setosa, versicolor and virginica.
## 'data.frame': 150 obs. of 5 variables: ## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... ## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... ## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... ## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ... ## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Using the base
plot() function to plot sepal length (x) against sepal width (y) produces:
x <- iris$Sepal.Length y <- iris$Sepal.Width plot(x,y)
This plot can be added to, but it will not be re-drawn, it is a static image. Suppose we want to add a plot of petal length (x) against petal width (y), then we could use
points(). However, since the plot is not re-drawn, the figure might miss some of the data:
x <- iris$Sepal.Length y <- iris$Sepal.Width plot(x,y) points(iris$Petal.Length, iris$Petal.Width, col = "blue")
Here is the same data plotted with
ggplot and stored as an object named
iris.plot <- ggplot(iris, aes(x = iris$Sepal.Length, y = iris$Sepal.Width)) + geom_point() iris.plot
Having made the plot, we simply need to add a new layer to display petal length vs petal width:
iris.plot <- iris.plot + geom_point(aes( x = iris$Petal.Length, y = iris$Petal.Width), col = "blue") iris.plot
Next, suppose, we want to colour the points by Iris type using the standard
plot() function. We would need to re-draw the entire plot with colour assigned to “species”.
x <- iris$Sepal.Length y <- iris$Sepal.Width plot(x,y, col = iris$Species) points(iris$Petal.Length, iris$Petal.Width, col = iris$Species)
Again, further code is required to scale the plot so that it will display all the petal length/width data.
ggplot, we just need to add an aesthetic detail mapping colour to
iris.plot <- iris.plot + geom_point(aes(col = Species)) iris.plot
Note that we didn’t have to re-specify that petal length is mapped to x and that petal width is mapped to y. That information is already stored in
iris.plot. We simply had to add the aesthetic detail
col mapped to
Species in the
You might also have noticed another advantage of
plot(): a legend is added automatically!