1 ggplot in 3 easy steps (maybe 2 easy steps)

1.1 aesthetic: what you want to graph (e.g. x, y, z).

1.2 geom: how you want to graph it.

1.3 options: optional titles, themes, etc.

2 Background

R has a number of graphing libraries, including base graphics that are installed whenever you install R.

ggplot2, is a graphing library in R that makes beautiful graphs. ggplot2 graph syntax can be formidably complex, with a somewhat steep learning curve.

That being said, learning ggplot2 is worth the effort for a couple of reasons. First, the graphs are beautiful. Second, ggplot2’s syntax, though seemingly arcane at times, forces you to think about the nature of your data, and the ideas that you are graphing. Lastly, a little bit of knowledge about ggplot2 can go a long way, and can build a powerful foundation for future learning.

3 A Simple Quick Example

The intent of this tutorial is to build the foundation of this idea that:

A little bit of ggplot can go a long way

and to give you a simple introduction to the idea that any ggplot graph is composed of:

an aesthetic + a geom or two + other optional elements like titles and themes.

So, as a quick and simple example…

And now, with titles…

This document is a very brief introduction to the basic ideas of ggplot2. More information about ggplot can be found here. More ggplot2 examples can be found here.

4 Call The Relevant Libraries

You will need a few R libraries to work in ggplot. You may only need library(ggplot2), but some of these other libraries may also be helpful.

5 Now We Simulate Some Data

In this example, we simulate some data. But your own learning of ggplot will progress more quickly if you use data that you have access to, on an issue that you care about.

Here are the first few rows of simulated data:

predictor outcome group
81.64 50.28 0
112.7 126.2 0
92.26 116.6 0
115.5 125.7 0
71.83 54.52 0
97.61 101.1 0
109.8 104 1
129.3 153.9 1
91.53 110.7 0
122.2 119.4 0

6 The Essential Idea Of ggplot2 Is Simple.

There are 3 essential elements to any ggplot call:

  1. An aesthetic that tells ggplot which variables are being mapped to the x axis, y axis, (and often other attributes of the graph, such as the color fill). Intuitively, the aesthetic can be thought of as what you are graphing.
  2. A geom or geometry that tells ggplot about the basic structure of the graph. Intuitively, the geom can be thought of as how you are graphing it.
  3. Other options, such as a graph title, axis labels and overall theme for the graph.

6.1 ggplot2 Starts By Calling The Aesthetic.

For one variable:

p <- ggplot(mydata, aes(x = ...)) This says there is only one variable running along the horizontal x axis in the aesthetic.

The p <-... means that we are assigning this graph aesthetic to plot p. We can then add other features to plot p as we continue our work. This iterative nature of ggplot2 is one of the things that makes it so powerful. As your workflow and your documents become more complex, you can build a simple consistent foundation1 for your graphs, then add something simple to make a first graph, and a different something simple to make a second graph.

For two variables:

p <- ggplot(mydata, aes(x = ..., y = ...)) This says there are two variables: one for the horizontal x axis; and another for the vertical y axis, in the aesthetic.

6.2 We Then Call The geom

We can then add different geometries to our plot:

For one variable:

+ geom_dotplot() This says add a dotplot geometry to the graph.

+ geom_histogram() This says add a histogram geometry to the graph.

+ geom_violin() This says add a violin plot geometry to the graph.

+ geom_beeswarm() This says add a beeswarm geometry to the graph.

A beeswarm is a creative layout of points that intuitively lets you understand the distribution of a quantity. The beeswarm geometry requires separate installation of the ggbeeswarm package. You also need to call library(ggbeeswarm) to use this geometry.

For two variables:

+ geom_point() This says add a point (scatterplot) geometry to the graph.

+ geom_smooth() This says add a smoother to the graph.

7 Examples

7.2 One Categorical Variable at a Time

The easiest way to represent a single categorical variable is likely a bar graph.

Here bars represent the count of observations in each group.

Changing the aesthetic slightly results in a stacked bar chart. Since all groups are stacked in 1 bar, we have to add information about the colors that we want to use to distinguish the groups.

7.4 Two Continuous Variables At A Time

7.4.5 Try a Hexagon Geom

geom_hex may be a useful visualization, especially when there is the possiblity of over-plotting due to many many points.

7.4.6 Combine Points and Smoother And Add Some Themes

7.4.6.2 Themes requiring ggthemes()

The themes below make use of library(ggthemes) which you will need to install.

8 There Is A Lot More That Can Be Done With ggplot2

More information can be found at ggplot2.

More ggplot2 examples can be found here.


Graphics made with the ggplot2 graphing library created by Hadley Wickham.

Available online at https://agroganweb.wordpress.com/data-visualization-dataviz/

Quick Introduction to ggplot2 by Andrew Grogan-Kaylor is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Last updated: February 05 2019 at 10:55


  1. By way of illustration, this foundation could be just an aesthetic (e.g. aes(...)) alone, or possibly an aesthetic plus a theme (e.g. theme_tufte()), plus axis labels to create a consistent look and feel for your graphs across a report.