# install.packages("tidyverse")
library(tidyverse)Solution to ggplot Task 2: Beautify and save plots
Get started
First we need to load the tidyverse packages:
Add labels
Here is an example of adding labels to the scatterplot from Task 1:
ggplot(penguins, aes(
x = bill_len,
y = bill_dep,
color = species
)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(
title = "Penguin bill dimensions",
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Species"
)
Change the colors
You can use scale_color_manual() to set custom colors:
ggplot(penguins, aes(
x = bill_len,
y = bill_dep,
color = species
)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("darkorange", "purple", "cyan4")) +
labs(
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Species"
)
Apply a theme
Try different themes to change the overall look of the plot:
ggplot(penguins, aes(
x = bill_len,
y = bill_dep,
color = species
)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("darkorange", "purple", "cyan4")) +
labs(
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Species"
) +
theme_minimal()
Reproduce the plot
The following code is adapted from the palmerpenguins package website.
ggplot(
data = penguins,
aes(
x = bill_len,
y = bill_dep,
color = species,
shape = species
)
) +
geom_point(size = 3, alpha = 0.8) +
geom_smooth(method = "lm", se = FALSE) +
scale_color_manual(values = c("darkorange", "purple", "cyan4")) +
labs(
title = "Penguin bill dimensions",
subtitle = "Bill length and depth for Adelie, Chinstrap and
Gentoo Penguins at Palmer Station LTER",
x = "Bill length (mm)",
y = "Bill depth (mm)",
color = "Penguin species",
shape = "Penguin species"
) +
theme_minimal() +
theme(
legend.position = c(0.85, 0.15),
legend.background = element_rect(fill = "white", color = NA)
)
What was changed compared to the basic plot?
- Make points larger and slightly transparent by setting
sizeandalphafor the point layer - Change to custom color scale
- Add title and subtitle with
labs - Change title of x-axis, y-axis and legend for color and shape aesthetic with
labs - Use
theme_minimal()instead of default theme - Change legend position to bottom right corner within the plot
- Positions are relative to the bottom left corner of the plot
- 0.85 (85% of plot width) to the right
- 0.15 (15% of plot height) towards the top
Save your plot
Example with one of the plots from above:
# First save the plot in a variable
flipper_box <- ggplot(penguins, aes(species, flipper_len, color = species)) +
geom_boxplot(width = 0.3) +
geom_jitter(alpha = 0.5, position = position_jitter(width = 0.2, seed = 123)) +
ggsci::scale_color_uchicago() +
labs(x = "Species", y = "Flipper length (mm)") +
theme_minimal() +
theme(legend.position = "none")# save as png in /img directory of the project
ggsave(filename = "img/flipper_box.png", plot = flipper_box)
# save as pdf in /img directory of the project
ggsave(filename = "img/flipper_box.pdf", plot = flipper_box)For the fast ones
Beautified histogram
ggplot(penguins, aes(x = flipper_len, fill = species)) +
geom_histogram(alpha = 0.6) +
ggsci::scale_fill_d3() +
labs(
y = "Frequency",
x = "Flipper length [mm]",
fill = "Penguin species"
) +
theme_minimal()`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).

Beautified heatmap
ggplot(penguins, aes(
x = species,
y = sex,
fill = flipper_len
)) +
geom_tile() +
scale_fill_viridis_c() +
theme_classic()
The patchwork package
With the patchwork package, you can combine multiple ggplots into one plot. The package allows you to add annotations to the plot and to control the layout and appearance.
Below you find a simple example of two different penguin scatterplots. For more explanation and an overview of what is possible with the package, please have a look at the package documentation
library(patchwork)
plot_1 <- ggplot(penguins, aes(
x = bill_len, y = bill_dep,
color = species
)) +
geom_point()
plot_2 <- ggplot(penguins, aes(
x = bill_len, y = body_mass,
color = species
)) +
geom_point()
# Simple combination of 2 plots in patchwork
plot_1 + plot_2
And a more complex example where shared layers are defined for both plots:
# more complex combination with annotation and definition of shared layers
final_plot <- plot_1 + plot_2 +
plot_layout(guides = "collect") +
plot_annotation(tag_levels = "a", tag_prefix = "(", tag_suffix = ")") &
theme_minimal() &
scale_color_manual(values = c("darkorange", "purple", "cyan4")) &
labs(
color = "Penguin species"
) &
theme(
plot.tag.position = c(0.1, 0.95),
plot.tag = element_text(face = "bold")
)
final_plot
References
Check out the package website of the palmerpenguins package. They have more nice examples of data visualizations that you can do with ggplot.
Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) penguin data. R package version 0.1.0. https://allisonhorst.github.io/palmerpenguins/. doi: 10.5281/zenodo.3960218.