Data Visualization With Ggplot2 Cheat Sheet

Posted on  by 



In the third and last of the ggplot series, this post will go over interesting ways to visualize the distribution of your data. Add geoms – graphical representation of the data in the plot (points, lines, bars). Ggplot2 offers many different geoms; we will use some common ones today, including: geompoint for scatter plots, dot plots, etc. Geomboxplot for, well, boxplots! Geomline for trend lines, time-series, etc. To add a geom to the plot use + operator. Because we have two continuous variables.

In the last chapter, we visualized the relationship between per capita GDP and life expectancy. You might have wondered how time fits into that association. In this chapter, we’ll explore life expectancy and GDP over time.

The following ggplot2 cheat sheet sections will be helpful for this chapter:

  • Geoms
    • geom_path()
  • Scales

The lubridate package is a helpful tool for working with dates. We’ll use some lubridate functions throughout the chapter. Take a look at the lubridate cheat sheet if you’re not already familiar with the package.

Not all time series are alike. In some situations, you’ll be interested in a long-term trend, but in others you’ll want to highlight short-term changes or even just individual values. In this chapter, we’ll cover various strategies for dealing with these different scenarios.

First, we’ll talk about the mechanics of date scales, which are useful for time series.

6.1 Mechanics

6.1.1 Date/time scales

Data Visualization With Ggplot2 Cheat Sheet

Sometimes, your time series data will include detailed date or time information stored as a date, time, or date-time. For example, the nycflights13::flights variable time_hour is a date-time.

When you map time_hour to an aesthetic, ggplot2 uses scale_*_datetime(), the scale function for date-times. There is also scale_*_date() for dates and scale_*_time() for times. The date- and time-specific scale functions are useful because they create meaningful breaks and labels.

flights_0101_0102 contains data on the number of flights per hour on January 1st and January 2nd, 2013.

Just like with the other scale functions, you can change the breaks using the breaks argument. scale_*_date() and scale_*_datetime() also include a date_breaks argument that allows you to supply the breaks in date-time units, like “1 month”, “6 years”, or “2 hours.”

Similarly, you can change the labels using the labels argument, but scale_*_date() and scale_*_datetime() also include a date_labels function made for working with dates. date_labels takes the same formatting strings as functions like ymd() and as_datetime(). You can see a list of all formatting strings at ?strptime.

We’ll use date_labels to format time_hour so that it doesn’t take up as much space.

6.2 Trends

6.2.1 One response variable

gm_combined is the same Gapminder data you saw in the previous chapter. Previously, we investigated how life expectancy is associated with per capita GDP. Now, we’ll ask a more obvious question: how has life expectancy changed over time?

Data Visualization With Ggplot2 Cheat Sheet

First, let’s just look at a single country: South Africa.

Adding geom_line() will make it easier to connect the dots and see the trend.

It looks like life expectancy started to fall around the time apartheid ended. We can add a reference line to check our hypothesis.

We could remove the points and just use a line, but the points are helpful indicators of the actual data. Generally, the more points you have, the less important it is to keep the points. For example, the following plot shows life expectancy for South Africa using the gm_life_expectancy, which contains yearly data. We’ve also filtered the data to only include the years 1800 to 2015, because the data after 2015 is based on projections.

The points are so close together that they form their own line, and so removing geom_point() doesn’t really affect the appearance of the plot.

Including points is important, however, if your data is irregularly distributed. For example, say our data only included two points between 1800 and 1950, but was yearly after 1950. In this situation, it would be important to use both points and lines to show the discrepancy in the amount of data.

There are two major dips in life expectancy in our plot. The first is the result of the 1918 influenza pandemic. The second, as we already pointed out, started around the end of apartheid. One hypothesis for this dip is that data reporting procedures changed when the South African government changed. Maybe the apartheid government systematically under-sampled non-white groups who may have had lower life expectancies. Another hypothesis is that changes in government led to general upheaval, which somehow affected life expectancy.

To further investigate, we’ll compare South Africa to its neighbors during this time period.

When you have multiple time series, adding geom_line() makes it easier to group each series together.

In the previous chapter, you learned how to reorder and align legends to make it easier to match lines with labels. Directly labeling the lines makes it even easier to connect a line with its label.

We removed the legend by using guides(color = 'none').

Interestingly, all four countries experienced similar declines in the early 1990s. This suggests that South Africa’s decline was not related to the end of apartheid. Next, we might ask if this dip occurred in all of Africa, which brings us to the next section: visualizing distributions over time.

6.2.2 Distributions over time

We can join gm_life_expectancy with gm_countries to look at all countries in Africa.

There are 54 countries listed under the “Africa” region in the data.

Plotting all the data at once is messy, but you can see that many countries experienced the same dip in the 1990s. You can also see that the countries with high life expectancies didn’t experience at dip at all, and that many countries experienced dramatic downward spikes due to single events.

One of these downward spikes is the Rwandan genocide.

Another is the Libyan Civil War.

Highlighting specific lines of interest is a useful way to compare select values to the rest of the data. We could try highlighting the subset of countries in southern Africa that we looked at earlier.

Visualization

You can see that the countries we highlighted weren’t the only ones that experienced the dip in the 1990s.

To get a sense of the trend for all of Africa, we could add a smooth line.

You can see a slight dip around the 1990s. However, this smooth line doesn’t capture the amount of variation in trends. Africa is a massive continent. Maybe dividing it up further will be more informative.

We can use region_gm8 to compare North Africa to Sub-Saharan Africa.

Most of the data lies between 40 and 80 years. We’ll use coord_cartesian() to zoom in on this region.

Individual lines are now easier to see. There aren’t very many countries in North Africa, but they all have relatively high life expectancies. All of the countries that experienced a severe dip in life expectancy in the 1990s are in Sub-Saharan Africa.

Now, we can try a smooth line for each region.

There’s a sharp contrast between North Africa and Sub-Saharan Africa. The dip in Sub-Saharan African life expectancies is likely due the HIV/AIDS pandemic, which affected most of Sub-Saharan Africa severely, but was particularly bad in the southern countries that we looked at earlier.

In other situations, you may want to use the techniques covered in the Distributions chapter. For example, you might be curious how the distribution of life expectancies for African countries has changed over time. Box plots are a good option.

6.2.3 Two response variables

So far, we’ve encoded time as position along the x-axis and a response variable, life_expectancy, along the y-axis. What if we want to encode a second response variable? It would be interesting to see how both life expectancy and per capita GDP have changed over time.

geom_path() is a useful way to represent a time series with two response variables. Unlike geom_line(), which connects points in the order they appear along the x-axis, geom_path() connects points in the order they appear in the data. We can use this feature of geom_path() to represent a time series without actually plotting time along an axis.

Here, we’ve plotted gdp_per_capita and life_expectancy for South Africa using geom_path(). We arranged the data by year so that the earliest years are plotted first.

To read a geom_path() plot, you follow the line’s path through the plot space, but you can’t currently tell which end of the line is the starting point. We’ll add a dot to the end of the line indicating the endpoint.

Sometimes, you’ll want to plot individual points as well as the path line. We’ll make the last point bigger to indicate that it’s the endpoint.

Now, it’s clear that the line starts in the lower left corner. Both life expectancy and per capita GDP increased until around $11000. Then, life expectancy continued to grow, but per capita GDP shrank. Around $9025, this trend reversed. Life expectancy fell when per capita GDP increased. Finally, both started increasing again.

You still can’t match points on the path to specific years. Labeling inflection points is helpful.

Data visualization with ggplot2 cheat sheet printable

In 1980, per capita GDP started going down, but life expectancy kept increasing. As a response to apartheid, the United States and other countries imposed sanctions against South Africa in the 1980s. The campaign to divest from South Africa also gathered speed in the 1980s. Both the sanctions and the divestment campaign likely caused the per capita capita GDP to drop. Notice that the trend reversed in 1995.

We can also use alpha to encode year.

Directly labeling points instead of using a legend makes the plot easier to decode. At a minimum, you should label the starting and ending points.

Again, labeling inflection points is helpful.

6.3 Short-term fluctuations

In the mechanics section of this chapter, you saw the following plot.

You might wonder why we used geom_col() to represent a time series. Here’s the same plot using geom_line() and geom_point().

From both plots, you can see that most flights occur in the early morning and around 4pm, but notice that we’re actually treating time like a discrete variable in this situation. We’ve counted the number of flights for each hour, and so it’s useful to be able to connect a number of flights with a specific hour. Columns make it easier to connect numbers of flights to specific hours.

Vertical segment plots using geom_segment() can also be helpful for some time series data. Say we want to understand what the first week in January looked like.

Data Visualization With Ggplot2 Cheat Sheet Download

geom_point() and geom_line() produce the following plot.

You can see that each day is shaped similarly. However, you can’t tell that there are actually no flights for a couple hours each night.

geom_segment() does a better job of showing the gaps between days. Segments also make it easier to perceive each day as a group to compare against the others. Another advantage of geom_segment() is that we can use color to encode a categorical variable.

In this case, there’s no long-term trend we’re interested in. Instead, we want to understand short-term fluctuations, and we care about individual values. In these situations, geom_col() and geom_segment() are good options.

6.4 Individual values

Sometimes, you’ll want to display time on the x-axis like a time series, but you won’t actually care about displaying any kind of trend.

The famines dataset from dcldata tracks major famines across time.

There’s no obvious relationship between time and deaths due to famines.

Even though there’s no trend, this data is still interesting if you’re curious about individual famines.

The above plot only uses the start date, but we also have the length of the famines. We can treat the x-axis as representing year generally and encode the length of a line as the length of the famine.

In this lesson we will dive into making common graphics with ggplot2. This approach follows The R Graphics Cookbook by Winston Chang.

Data Visualization With Ggplot2 Cheat Sheet Printable

ggplot2 is a system for declaratively creating graphics, based on The Grammar of Graphics. You provide the data, tell ggplot2 how to map variables to aesthetics, what graphical primitives to use, and it takes care of the details.

You will likely find RStudio’s Data Visualization Cheat Sheet helpful as a quick reference. If you want to learn more about ggplot2 after this lesson, the documentation has some good suggestions. ggplot2: Elegant Graphics for Data Analysis is the definitive book on the subject.

ggplot2 Glossary

  • The data is what we want to visualize. It consists of variables, which are stored as columns in a data frame.
  • geoms are the geometric objects that are drawn to represent the data, such as bars, lines, and points.
  • Aesthetic attributes, or aesthetics, are visual properties of geoms, such as x and y position, line color, point shapes, etc.
  • There are mappings from data values to aesthetics.
  • scales control the mapping from the values in the data space to values in the aesthetic space. A continuous y scale maps larger numerical values to vertically higher positions in space.
  • guides show the viewer how to map the visual properties back to the data space. The most commonly used guides are the tick marks and labels on an axis.

ggplot2 Mechanics

The ggplot() function is used to initialize the basic graph structure, then we add to it. The basic idea is that you specify different parts of the plot, and add them together using the + operator.

We will start with a blank plot.

Geometric objects are the actual marks we put on a plot. Examples include:

Data Visualization With Ggplot2 Cheat Sheet
  • geom_point()
  • geom_line()
  • geom_boxplot()

To visualize data a plot should have at least one geom; there is no upper limit. You can add a geom to a plot using the + operator. Evaluating the following line of code will produce an informative error message.

Each type of geom usually has a required set of aesthetics to be set, and usually accepts only a subset of all aesthetics. Refer to the help pages to see what mappings each geom accepts, for instance help(geom_point). Aesthetic mappings are set with the aes() function. Examples include:

  • position (x and y coordinates)
  • color (“outside” color)
  • fill (“inside” color)
  • shape (of points)
  • linetype
  • size

To create a scatter plot we need to use the aes() function to tell ggplot which variable should be used for the x-coordinate of the points and which variable should be used for the y-coordinate of the points…

Scatter Plot

How has life expectancy changed over time?

Try replacing geom_point() with geom_jitter() to deal with over-plotting.

How is life expectancy related to GDP per capita?

Bar Graph

What does the distribution of life expectancy look like?

Try plotting a histogram of GDP per capita, gdpPercap.

Line Graph

How has life expectancy changed in Afghanistan over time?

How has GDP per capita changed in Afghanistan over time?

Grouping

How has life expectancy changed in each country over time?

Faceting

Do these time trends differ between continents?

Do the trends for GDP per capita look similar?

Layering

Does the relationship between life expectancy and time look linear? The first layer in this graph shows all the data points, the second layer shows a smoothed trend line and confidence interval.

If you change the smoothing method from loess to lm, does the linear model look reasonable?

Density Plots

What does the joint distribution of GDP per capita and life expectancy look like?

Does this relationship change over time?

Saving

There are a couple ways to save figures made with ggplot2.

The easiest way to save a graph is to export directly from the RStudio Plots panel, by clicking on Export when the image is plotted. This will give you the option of png or pdf and allow you to select the directory where you wish to save the file.

Alternatively the ggsave() function from the ggplot2 library is the way to go if you want to create files programmatically.

Try using ggsave() to create a file named gdpPercap.png that contains a bar chart of GDP per capita.

A Final Example

Below is an example to show off a bunch of ggplot2’s features at once. Notice that different layers in a plot can use different data (the text labels are created using a smaller set of data).

Documentation built with Hugo using the Material theme.



Coments are closed