Learning Objectives
- Understand the “grammar of graphics”
- Produce scatter plots, boxplots, bar graphs, and time series plots using ggplot.
- Set universal plot settings.
- Modify the aesthetics of an existing ggplot plot (including axis labels and color).
- Build complex and customized plots from data in a data frame.
We start by loading the required packages. ggplot2
is included in the tidyverse
package, and is the current standard for data visualization in R. Authored by Hadley Wickham, gg
stands for “Grammar of Graphics.” In learning ggplot2
, you may find the following cheat sheet to be a helpful reference.
library(tidyverse)
Hadley’s Grammar of Graphics is outlined in detail in this article. Here, he illustrates his principles using a small data set similar to the following:
df <- data.frame(A = c(2,1,4,9),
B = c(4,1,15,80),
C = c(1,2,3,4),
D = c('far','far','near','near'))
head(df)
To visualize any data set using the Grammar of Graphics, it helps to understand the 3 components of which any graph is comprised:
Geoms are the visual entities that we see on a graph. In the image below, we see three examples of geoms: a circular point, a bar, and a line:
Scales control how the data columns map to the aesthetic attributes of the geoms. For example, is the point geom yellow or blue? Is it large or small? Is it high or low? Left or right? These aesthetic attributes are respectively controlled by the color, size, y, and x scales:
Additional scales in ggplot2
are:
Any plot created with ggplot2
requires these ingredients. To create a plot, one must specify the desired geom; which data variables are to be aesthetically mapped to the geom; and the scales to use to control the mapping. The skeleton of any ggplot2
command is as follows; parts in italics are to be replaced with specific data variable names, geoms, and scales:
ggplot(data = nameofdata) + geom_nameofgeom(aes(scale1 = variable1, scale2 = variable2))
At a minimum, most geoms require the x
scale.
Let’s begin by mapping A
and B
to the point
geom on a Cartesian plane. Note in ?geom_point
that two scales are required for aesthetic mappings to point geoms, x and y:
ggplot(data = df) + geom_point(aes(x = A,y=B))
We can employ other scales outside of aesthetic data mappings. For example, if we want to change the aesthetic mapping of the above scatterplot by changing the shape, color, and size scale, we can do so with the following:
ggplot(data = df) + geom_point(aes(x = A,y=B), shape = 17,color='red',size=4)
Notice in the above code that the scales that are not mapped to data are outside the aes()
command.
As an aside, looking at the ?shape
help file, we can find code to see all possible shapes:
df2 <- data.frame(x = 1:5 , y = 1:25, z = 1:25)
s <- ggplot(df2, aes(x = x, y = y))
s + geom_point(aes(shape = z), size = 4) + scale_shape_identity()
Now suppose we want to aesthetically map other variables with the shape, color, and size scales. We must now put these specifications inside the aes()
command and specify the variables we wish to map. Consider the following code, and note the different looks, error messages and warnings that appear when attempting to apply aesthetic mappings using various scales depending on the data type. In ggplot
-speak, “continuous” refers to quantitative data in general; while “discrete” refers to categorical data:
#Mapping continuous C with size:
ggplot(data = df) + geom_point(aes(x = A,y=B, size = C))
#Mapping continuous C with size and color:
ggplot(data = df) + geom_point(aes(x = A,y=B, size = C, color = C))
#Mapping continuous C with shape:
ggplot(data = df) + geom_point(aes(x = A,y=B, shape = C))
Challenge
Re-create the following plots:
Note some interesting concepts illustrated here:
A very important aspect of the ggplot2
package is the idea of layers. Aesthetic mappings to different geoms can take place simply by specifying additional mappings with a +
sign. For example, suppose we want to create the above scatterplots with points and lines. This requires two aesthetic mappings: one from the data to the points geom, and one from the data to the lines geom. We can see this in what follows. Note that because both geom_point()
and geom_line()
rely on the same aesthetic mapping, we could simplify the code by specifiying the appropriate mapping in the initial ggplot()
command. The following two lines of code are equivalent:
ggplot(data = df) + geom_point(aes(x = A,y=B), size = 4) + geom_line(aes(x = A,y=B))
ggplot(aes(x = A, y = B), data = df) + geom_point(size = 4) + geom_line()
Challenge
Re-create the following plots. What happens if you try to map variable
C
togeom_line()
using the size scale?
Now let’s start visualizing some real data. If not still in the workspace, load the data we saved in the previous lesson.
surveys <- read.csv('data/portal_data_joined.csv')
When plotting scatterplots, ggplot likes data in the ‘long’ format: i.e., a column for every dimension, and a row for every observation. However, when plotting barplots where the height of the bars are counts or percents, pre-aggregated data (e.g., the output from a series of dplyr
commands) may work better. Well-structured data will save you lots of time when making figures with ggplot.
To create a scatterplot of hind foot length versus weight, we need to:
data
argumentggplot(data = surveys )
aes
), by selecting the variables to be plotted and the scales to define the geomsggplot(data = surveys , aes(x = weight, y = hindfoot_length))
geoms
– graphical representation of the data in the plot (points, lines, bars). To add a geom to the plot use +
operatorggplot(data = surveys, aes(x = weight, y = hindfoot_length)) +
geom_point()
#> Warning: Removed 4048 rows containing missing values (geom_point).
Recall that the +
in the ggplot2
package allows you to modify existing ggplot
objects. This means you can easily set up plot “templates” and conveniently explore different types of plots, so the above plot can also be generated with code like this:
# Assign plot to a variable
surveys_plot <- ggplot(data = surveys , aes(x = weight, y = hindfoot_length))
# Draw the plot
surveys_plot + geom_point()
Remember:
ggplot()
function can be seen by any geom layers that you add (i.e., these are universal plot settings). This includes the x and y axis you set up in aes()
.ggplot()
function.+
sign used to add layers must be placed at the end of each line containing a layer. If, instead, the +
sign is added in the line before the other layer, ggplot2
will not add the new layer and will return an error message.# this is the correct syntax for adding layers
surveys_plot +
geom_point()
# this will not add the new layer and will return an error message
surveys_plot
+ geom_point()
Challenge (optional)
Scatter plots can be useful exploratory tools for small datasets. For data sets with large numbers of observations, such as the
surveys
data set, overplotting of points can be a limitation of scatter plots. One strategy for handling such settings is to use hexagonal binning of observations. The plot space is tessellated into hexagons. Each hexagon is assigned a color based on the number of observations that fall within its boundaries. To use hexagonal binning withggplot2
, first install the R packagehexbin
from CRAN:install.packages("hexbin")
Then use the
geom_hex()
function:surveys_plot + geom_hex()
- What are the relative strengths and weaknesses of a hexagonal bin plot compared to a scatter plot? Examine the above scatter plot and compare it with the hexagonal bin plot that you created.
Building plots with ggplot is typically an iterative process. We start by defining the dataset we’ll use, lay the axes, and choose a geom:
ggplot(data = surveys , aes(x = weight, y = hindfoot_length)) +
geom_point()
#> Warning: Removed 4048 rows containing missing values (geom_point).
Then, we start modifying this plot to extract more information from it. For instance, we can add transparency (alpha
) to avoid overplotting:
ggplot(data = surveys , aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1)
#> Warning: Removed 4048 rows containing missing values (geom_point).
We can also add colors for all the points:
ggplot(data = surveys , aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, color = "blue")
#> Warning: Removed 4048 rows containing missing values (geom_point).
Or to color each species in the plot differently:
ggplot(data = surveys , aes(x = weight, y = hindfoot_length)) +
geom_point(alpha = 0.1, aes(color=species_id))
#> Warning: Removed 4048 rows containing missing values (geom_point).
Challenge
Use what you just learned to create a scatter plot of
weight
overspecies_id
with the plot types showing in different colors. Is this a good way to show this type of data?
We can use boxplots to visualize the distribution of weight within each species:
ggplot(data = surveys , aes(x = species_id, y = weight)) +
geom_boxplot()
#> Warning: Removed 2503 rows containing non-finite values (stat_boxplot).
By adding points to boxplot, we can have a better idea of the number of measurements and of their distribution:
ggplot(data = surveys , aes(x = species_id, y = weight)) +
geom_boxplot(alpha = 0) +
geom_jitter(alpha = 0.3, color = "tomato")
#> Warning: Removed 2503 rows containing non-finite values (stat_boxplot).
#> Warning: Removed 2503 rows containing missing values (geom_point).
Notice how the boxplot layer is behind the jitter layer? What do you need to change in the code to put the boxplot in front of the points such that it’s not hidden?
Challenges
Boxplots are useful summaries, but hide the shape of the distribution. For example, if there is a bimodal distribution, it would not be observed with a boxplot. An alternative to the boxplot is the violin plot (sometimes known as a beanplot), where the shape (of the density of points) is drawn.
- Replace the box plot with a violin plot; see
geom_violin()
.In many types of data, it is important to consider the scale of the observations. For example, it may be worth changing the scale of the axis to better distribute the observations in the space of the plot. Changing the scale of the axes is done similarly to adding/modifying other components (i.e., by incrementally adding commands). Try making these modifications:
- Represent weight on the log10 scale; see
scale_y_log10()
.So far, we’ve looked at the distribution of weight within species. Try making a new plot to explore the distribution of another variable within each species.
Create boxplot for
hindfoot_length
. Overlay the boxplot layer on a jitter layer to show actual measurements.Add color to the datapoints on your boxplot according to the plot from which the sample was taken (
plot_id
).
Hint: Check the class for
plot_id
. Consider changing the class ofplot_id
from integer to factor. Why does this change how R makes the graph?
Bar graphs are frequently used for visualizing the number of observations in each category of a categorical variable, or summary statistics (like a mean or median) of a quantitative variable across levels of a categorical variable.
By default, ggplot
counts the number of observations in each level and plots those counts on the y-axis. For example, consider plotting the number of each sex in the data set:
ggplot(data = surveys , aes(x = sex)) +
geom_bar()
However, what if we wanted to visualize the average hind foot length for each sex instead?
We first need to use dplyr
to group the data by sex and compute the mean length. We can also filter out the unlabeled sexes:
length_sex <- surveys %>%
group_by(sex) %>%
summarise(mean_length = mean(hindfoot_length, na.rm=TRUE)) %>%
filter(sex != '')
length_sex
We now want to map the mean_length
variable to the y scale. We must specify stat='identity'
inside the geom_bar()
command, to specify that we want to use the actual values in the mean_length
column on the y-axis instead of the default count:
ggplot(data = length_sex, aes(x = sex,y = mean_length)) +
geom_bar(stat='identity')
Challenges
- Create a bar graph that shows the number of each species in each data set. What are the issues with this plot?
- Use
group_by()
,summarize()
,n()
andmean()
to create a data set with the species IDs in one column, the number of observations in the second column, and the mean hind foot length in the third column.
- Filter this data set to contain only the species with 5 or more entries. Re-create the bar graph showing the count of each species.
- Create a second bar graph showing the mean foot length of each species. Which has the largest? The smallest?
Let’s calculate number of counts per year for each species. First we need to group the data and count records within each group:
yearly_counts <- surveys %>%
group_by(year, species_id) %>%
tally
Timelapse data can be visualized as a line plot with years on the x axis and counts on the y axis:
ggplot(data = yearly_counts, aes(x = year, y = n)) +
geom_line()
Unfortunately, this does not work because we plotted data for all the species together. We need to tell ggplot to draw a line for each species by modifying the aesthetic function to include group = species_id
:
ggplot(data = yearly_counts, aes(x = year, y = n, group = species_id)) +
geom_line()
We will be able to distinguish species in the plot if we add colors (using color
also automatically groups the data):
ggplot(data = yearly_counts, aes(x = year, y = n, color = species_id)) +
geom_line()
ggplot has a special technique called faceting that allows the user to split one plot into multiple plots based on a factor included in the dataset. We will use it to make a time series plot for each species:
ggplot(data = yearly_counts, aes(x = year, y = n)) +
geom_line() +
facet_wrap(~ species_id)
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
Now we would like to split the line in each plot by the sex of each individual measured. To do that we need to make counts in the data frame grouped by year
, species_id
, and sex
:
yearly_sex_counts <- surveys %>%
group_by(year, species_id, sex) %>%
tally
We can now make the faceted plot by splitting further by sex using color
(within a single plot):
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id)
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
Usually plots with white background look more readable when printed. We can set the background to white using the function theme_bw()
. Additionally, you can remove the grid:
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id) +
theme_bw() +
theme(panel.grid = element_blank())
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
ggplot2
themesIn addition to theme_bw()
, which changes the plot background to white, ggplot2
comes with several other themes which can be useful to quickly change the look of your visualization. The complete list of themes is available at http://docs.ggplot2.org/current/ggtheme.html. theme_minimal()
and theme_light()
are popular, and theme_void()
can be useful as a starting point to create a new hand-crafted theme.
The ggthemes package provides a wide variety of options (including an Excel 2003 theme). The ggplot2
extensions website provides a list of packages that extend the capabilities of ggplot2
, including additional themes.
Challenge
Use what you just learned to create a plot that depicts how the average weight of each species changes through the years.
The facet_wrap
geometry extracts plots into an arbitrary number of dimensions to allow them to cleanly fit on one page. On the other hand, the facet_grid
geometry allows you to explicitly specify how you want your plots to be arranged via formula notation (rows ~ columns
; a .
can be used as a placeholder that indicates only one row or column).
Let’s modify the previous plot to compare how the weights of males and females has changed through time:
# One column, facet by rows
yearly_sex_weight <- surveys %>%
group_by(year, sex, species_id) %>%
summarize(avg_weight = mean(weight))
ggplot(data = yearly_sex_weight, aes(x=year, y=avg_weight, color = species_id)) +
geom_line() +
facet_grid(sex ~ .)
#> Warning: Removed 146 rows containing missing values (geom_path).
# One row, facet by column
ggplot(data = yearly_sex_weight, aes(x=year, y=avg_weight, color = species_id)) +
geom_line() +
facet_grid(. ~ sex)
#> Warning: Removed 146 rows containing missing values (geom_path).
Take a look at the ggplot2
cheat sheet, and think of ways you could improve the plot.
Now, let’s change names of axes to something more informative than ‘year’ and ‘n’ and add a title to the figure:
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id) +
labs(title = 'Observed species in time',
x = 'Year of observation',
y = 'Number of species') +
theme_bw()
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
The axes have more informative names, but their readability can be improved by increasing the font size:
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id) +
labs(title = 'Observed species in time',
x = 'Year of observation',
y = 'Number of species') +
theme_bw() +
theme(text=element_text(size=16))
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
Note that it is also possible to change the fonts of your plots. If you are on Windows, you may have to install the extrafont package, and follow the instructions included in the README for this package.
After our manipulations, you may notice that the values on the x-axis are still not properly readable. Let’s change the orientation of the labels and adjust them vertically and horizontally so they don’t overlap. You can use a 90 degree angle, or experiment to find the appropriate angle for diagonally oriented labels:
ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id) +
labs(title = 'Observed species in time',
x = 'Year of observation',
y = 'Number of species') +
theme_bw() +
theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
axis.text.y = element_text(colour="grey20", size=12),
text=element_text(size=16))
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
#> geom_path: Each group consists of only one observation. Do you need to
#> adjust the group aesthetic?
If you like the changes you created better than the default theme, you can save them as an object to be able to easily apply them to other plots you may create:
grey_theme <- theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
axis.text.y = element_text(colour="grey20", size=12),
text=element_text(size=16))
ggplot(surveys , aes(x = species_id, y = hindfoot_length)) +
geom_boxplot() +
grey_theme
#> Warning: Removed 3348 rows containing non-finite values (stat_boxplot).
Challange
With all of this information in hand, please take another five minutes to either improve one of the plots generated in this exercise or create a beautiful graph of your own. Use the RStudio
ggplot2
cheat sheet for inspiration.
Here are some ideas:
- See if you can change the thickness of the lines.
- Can you find a way to change the name of the legend? What about its labels?
- Try using a different color palette (see http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/).
After creating your plot, you can save it to a file in your favorite format. You can easily change the dimension (and resolution) of your plot by adjusting the appropriate arguments (width
, height
and dpi
):
my_plot <- ggplot(data = yearly_sex_counts, aes(x = year, y = n, color = sex)) +
geom_line() +
facet_wrap(~ species_id) +
labs(title = 'Observed species in time',
x = 'Year of observation',
y = 'Number of species') +
theme_bw() +
theme(axis.text.x = element_text(colour="grey20", size=12, angle=90, hjust=.5, vjust=.5),
axis.text.y = element_text(colour="grey20", size=12),
text=element_text(size=16))
ggsave("name_of_file.png", my_plot, width=15, height=10)
Note: The parameters width
and height
also determine the font size in the saved plot.
Page build on: 2017-08-15 10:21:58
Data Carpentry,
2017. License. Contributing.
Questions? Feedback?
Please file
an issue on GitHub.
On
Twitter: @datacarpentry