The Grammar of Graphics
Here again are the 3 essential elements required when plotting.
|Data||The dataset to be plotted|
|Aesthetics||The scales onto which we map our data|
|Geometries||The visual elements used for our data|
In the previous notes, we explored the Aesthetics layer and the various details which can be assigned to variables in order to highlight and to reveal relationships between them. In these notes, we will explore three of the various Geometries available. Specifically, we will look at:
geom_smooth. The geometry layer specifies the mode of visualization, that is, whether to use a scatter plot, a line plot, or a smoothed line amongst many other possibilities.
Finally, we look at ggplot as an object which can be called and adjusted.
Geometries are referred to as “geoms”. A geom is the form of the visualization. In the code below,
ggplot that the form of visualization required is to plot the data as points.
library(tidyverse) ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
The data can be visualized by lines connecting the points defined by the variables assigned to the x and y axes. The second plot below includes an aesthetic detail in
geom_line to split the data by type
vs. This geom isn’t especially helpful for this dataset, but would be very useful for plotting time series data.
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_line()
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_line(aes(col = factor(vs)))
A smoothed curve through the data can help reveal trends.
geom_smooth is the geom to add this to your plot.
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_smooth()
It’s not one or the other, you can have both!
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth()
We can apply some aesthetics details within these geoms as in the following examples. Let’s apply smoothing by
gear type. Note that we have to ensure that the variable chosen is categorical. Thus, we tell ggplot to treat
gear as a factor.
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(aes(col = factor(gear)))
If the shading is distracting, it can be switched off:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(aes(col = factor(gear)), se = FALSE)
The smoothing line type is easily altered:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(aes(col = factor(gear)), se = FALSE, linetype = 2)
In the plot above, the
linetype option was applied to all lines in the
geom_smooth layer. We could instead have included it as an aesthetic detail used to pick out cars by
gear by mapping ‘linetype’ to this variable (which should be categorical). Note the difference in the code below and try it out for yourself:
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + geom_smooth(aes(linetype = factor(gear), col = factor(gear)), se = FALSE)
There are many more geoms some of which we will look at later.
ggplot as R object
We are familiar with variable assignment in R such as in the following:
a <- 5 b <- 6 c <- a^2 + b^2
ggplot can be assigned a variable name which we can call and modify later in the code. Thus, assigning the output of
ggplot to a variable name helps us to easily add layers to the plot without re-writing the whole code. This will help you to explore and visualize your datasets.
car.plot <- ggplot(mtcars, aes(x = wt, y = mpg)) car.plot <- car.plot + geom_point() car.plot <- car.plot + geom_smooth()
These notes have illustrated some of the different geometries available and that aesthetics can be added within them. As should be clear, not all have been especially helpful in visualizing this dataset; the primary purpose was to show how to add and manipulate these geoms in your plot. It will be up to you to experiment with and decide upon the best way to display your data.