• Have students install devtools and patchwork (using devtools)
  • Open https://ggplot2.tidyverse.org/reference/ggtheme.html in browser

File formats

library(ggplot2)
library(readr)

trees <- read_tsv("https://ndownloader.figshare.com/files/5629536")

ggplot(trees, aes(x = HEIGHT, y = CIRC)) +
  geom_point()

ggsave("acacia_size_scaling.png")
ggsave("acacia_size_scaling.svg")
ggsave("acacia_size_scaling.pdf")

Resolution, DPI, and Image Dimensions

ggsave("acacia_size_scaling.png", height = 7, width = 10)
ggsave("acacia_size_scaling.png", dpi = 300)
ggsave("acacia_size_scaling.png", dpi = 30)

Color palettes

ggplot(trees, aes(x = HEIGHT, y = CIRC, color = SPECIES)) +
  geom_point() +
  scale_color_viridis_d()
ggplot(trees, aes(x = HEIGHT, y = CIRC, color = SPECIES)) +
  geom_point() +
  scale_color_viridis_d(option = "magma")
ggplot(trees, aes(x = HEIGHT, y = CIRC, color = HEIGHT)) +
  geom_point() +
  scale_color_viridis_c(option = "magma")

Themes

ggplot(trees, aes(x = HEIGHT, y = CIRC, color = SPECIES)) +
  geom_point() +
  scale_color_viridis_c() +
  theme_classic()

Saving and exporting multiple plots

species_scaling <- ggplot(trees, aes(x = HEIGHT, y = CIRC, color = SPECIES)) +
  geom_point() +
  scale_color_viridis_d()

species_scaling
ggsave("species_scaling.jpg", species_scaling)

Combining multiple plots

install.packages('devtools')
library(devtools)
install_github('thomasp85/patchwork')
library(patchwork)

height_dist <- ggplot(trees, aes(x = HEIGHT, fill = SPECIES)) +
  geom_histogram() +
  scale_fill_viridis_d()

species_scaling + height_dist
species_scaling + height_dist +
  plot_layout(ncol = 1)
species_scaling + height_dist +
  plot_layout(ncol = 1, heights = c(3, 1))
species_scaling + height_dist +
  plot_layout(ncol = 1, heights = c(3, 1)) +
  plot_annotation(tag_levels = "A")
height_dist <- height_dist +
  theme_void() +
  theme(legend.position='none')

height_dist + species_scaling + plot_layout(ncol = 1, heights = c(1, 5))