ggplot Basics
Each plot with ggplot has the same template for the code:
## call to ggplot and the name of the data frame
ggplot(dataframename,
## which variables to use for which plot components
aes(x = column_name, y = column_name)) +
# what type of plot
geom_****()
The first line says what data and variables to use, and the second line says what type of plot to make. We add the components of the plot together, which is a somewhat unique syntax.
For example:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point()
## Warning: Removed 2 rows containing missing values (geom_point).
The warning message is telling you that two observations (rows) in your data frame are not plotted because there were missing values. You can get this warning from explicit missing values or in cases where you set the limits of the axes in a way that excludes some data points.
The first line alone (without the + at the end) makes a plot set up without adding the data:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm))
If we change the geom function we use, the plot type changes:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_bin2d()
## Warning: Removed 2 rows containing non-finite values (stat_bin2d).
This is a density plot, where the color of each rectangle indicates how many observations (points on the above plot) fall in that area.
EXERCISE
Fill in the ___
to make a line plot using geom_line()
with the nh
data frame, with year
as the x variable and temp
as the y:
ggplot(___, aes(x = ___, y = ___)) +
___()
Different Plot Types
The above plots used two continuous variables. The cheat sheet has the geoms organized by how many and what type of variables you want to plot. For example, we can make a histogram, which is of a single variable:
ggplot(penguins, aes(x = bill_depth_mm)) +
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 2 rows containing non-finite values (stat_bin).
The warning message is how many bins (bars) there are in the histogram. It defaults to 30, but it wants you to explicitly pick a number instead:
ggplot(penguins, aes(x = bill_depth_mm)) +
geom_histogram(bins = 25)
## Warning: Removed 2 rows containing non-finite values (stat_bin).
We can also have multiple geoms on the same plot:
ggplot(nh, aes(x=year, y=temp)) +
geom_line() +
geom_point()
ggplot(nh, aes(x=year, y=temp)) +
geom_point() +
geom_line() +
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
EXERCISE
Make a histogram (geom_histogram()
) of the temp
variable in the nh
dataset
Other Aesthetics and Grouping
We can set other aesthetics of the plot (color, fill, linetype, marker, etc.) according to variables in our data.
Note that if you want a line or color for each group, those groups need to be defined by a categorical variable. This is the opposite of how data usually needs to be structured when using base R plotting functions. If the values for the two groups are in separate columns in your dataset, you’ll need to reshape your data to get them in a single column instead – something covered in the tidyr session of these workshops.
Each aesthetic – each thing you define in aes() – needs to be a single column in your data set.
With categorical variables, this helps us see the groups in our data:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point()
## Warning: Removed 2 rows containing missing values (geom_point).
Note that a legend was added automatically.
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
geom_smooth(method="lm", se=FALSE)
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 2 rows containing non-finite values (stat_smooth).
## Warning: Removed 2 rows containing missing values (geom_point).
method=lm
tells it how to fit a line to the data; “lm” means fit a linear model with the lm
function. se=FALSE
means to not display the standard errors on the plot.
Note that the smoothing was done by group. Specifying color
effectively grouped our data, much like what happens with group_by
in dplyr.
EXERCISE
Repeat the same plot as above, bill_length_mm
vs. bill_depth_mm
, but color
by body_mass_g
instead of species
.
Labels/Titles
A convenient way to label axes and legends and set the title is with the labs() function, added as a component of the plot:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm,
color = species)) +
geom_point() +
labs(title="Penguin Bill Dimensions",
y="Depth (mm)",
x="Length (mm)",
color="Body Mass",
subtitle="Three Species",
caption="Source: palmerpenguins")
## Warning: Removed 2 rows containing missing values (geom_point).
EXERCISE
Plot flipper_length_mm
vs. body_mass_g
and color by sex
. Label the axes and title your plot.
Axis Limits
To change the axes min and max values, like we would with xlim
or ylim
in base R graphics, we use a scale function and set the limits
argument:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point() +
scale_x_continuous(limits=c(30, 50))
## Warning: Removed 54 rows containing missing values (geom_point).
Facets
In addition to using color, fill, linetype, etc. to denote groups, we can also make a separate plot for each group, where the plots are aligned and share axes. These are called facets:
ggplot(penguins, aes(flipper_length_mm)) +
geom_histogram() +
facet_grid(sex ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 2 rows containing non-finite values (stat_bin).
The facet function syntax is using the formula syntax used elsewhere in R, but it can be tough to remember. It’s on the cheat sheet though so you can look it up. There are other options for laying out plots horizontally or in a grid.
Note that the plots share an x-axis, and the y-axis has the same range in each plot.
If you want to put multiple plots in a single image together, but they are not facets of a single plot, see the patchwork package.
EXERCISE
Make a histogram (set a value for bins) of body_mass_g
for each species
:
ggplot(penguins, aes(___)) +
___(___) +
facet_grid(___ ~ .)
Styling
Themes control the look of the plot not related to the data: fonts, backgrounds, grid lines, axis lines, etc.
There are some built-in themes (theme_minimal()
, theme_bw()
, theme_classic()
, etc.), but you can also control the styling further with the theme()
function:
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal()
## Warning: Removed 2 rows containing non-finite values (stat_bin).
For further control, use theme
instead or in addition:
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(), # get rid of the vertical grid lines
panel.grid.minor.x = element_blank())
## Warning: Removed 2 rows containing non-finite values (stat_bin).
Many theme elements are controlled by setting options in additional element
functions:
element_blank()
- omit the element
element_text()
element_line()
element_rect()
These are options that you will generally google for how to do: “ggplot remove gridlines”, “ggplot change font size”, etc.
In addition to themes, you can control some aspects of the data-driven elements of the plot directly, without using a variable to define them. For example with color or size, if we specify them outside of a call to aes()
within the geom function:
ggplot(penguins, aes(x = bill_length_mm,
y = bill_depth_mm)) +
geom_point(color="red", size=3)
## Warning: Removed 2 rows containing missing values (geom_point).
it colors all of the points the same color.
EXERCISE
Change the fill
color for the bars in our histogram above to be “darkgreen”. The code from above is copied below to get you started.
ggplot(penguins, aes(body_mass_g)) +
geom_histogram(bins = 30) +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
Pipes
We use +
, not %>%
, to join together components of our ggplot. But we can still pipe the results of other commands into ggplot - I just didn’t above because we were just plotting the data frame as it is. For example, to plot only the Adelie penguins:
penguins %>%
filter(species == "Adelie") %>%
ggplot(aes(x=bill_length_mm, y=bill_depth_mm)) +
geom_point()
## Warning: Removed 1 rows containing missing values (geom_point).
LS0tCnRpdGxlOiAnZ2dwbG90OiBRdWljayBJbnRyb2R1Y3Rpb24nCm91dHB1dDoKICBodG1sX2RvY3VtZW50OgogICAgZGZfcHJpbnQ6IHBhZ2VkCiAgICBjb2RlX2Rvd25sb2FkOiBUUlVFCiAgICB0b2M6IHRydWUKICAgIHRvY19kZXB0aDogMgplZGl0b3Jfb3B0aW9uczoKICBjaHVua19vdXRwdXRfdHlwZTogY29uc29sZQotLS0KCmBgYHtyLCBzZXR1cCwgaW5jbHVkZT1GQUxTRX0Ka25pdHI6Om9wdHNfY2h1bmskc2V0KGV2YWw9VFJVRSwgZmlnLndpZHRoPTUsIGZpZy5oZWlnaHQ9My41KQpgYGAKClRoaXMgaXMgYW4gW1IgTWFya2Rvd25dKGh0dHBzOi8vcm1hcmtkb3duLnJzdHVkaW8uY29tLykgZG9jdW1lbnQuICBGb2xsb3cgdGhlIGxpbmsgdG8gbGVhcm4gbW9yZSBhYm91dCBSIE1hcmtkb3duIGFuZCB0aGUgbm90ZWJvb2sgZm9ybWF0IHVzZWQgZHVyaW5nIHRoZSB3b3Jrc2hvcC4KClRoZSBjaGVhdHNoZWV0IGlzIHZlcnkgdXNlZnVsIHdoZW4gd29ya2luZyB3aXRoIGdncGxvdDI6IGh0dHBzOi8vd3d3LnJzdHVkaW8ub3JnL2xpbmtzL2RhdGFfdmlzdWFsaXphdGlvbl9jaGVhdF9zaGVldAoKCiMgU2V0dXAKCmBgYHtyLCB3YXJuaW5nPUZBTFNFLCBlcnJvcj1GQUxTRSwgbWVzc2FnZT1GQUxTRX0KbGlicmFyeShkcGx5cikKbGlicmFyeShnZ3Bsb3QyKQpsaWJyYXJ5KH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