## ----chunk-opts, include = FALSE----------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 5,
  fig.height = 3.2,
  dpi = 96,
  dev = "png",
  dev.args = list(bg = "transparent")
)

## ----theme-setup, include = FALSE---------------------------------------------
library(ggplot2)
transparent_theme <- theme(
  plot.background       = element_rect(fill = "transparent", color = NA),
  panel.background      = element_rect(fill = "transparent", color = NA),
  legend.background     = element_rect(fill = "transparent", color = NA),
  legend.box.background = element_rect(fill = "transparent", color = NA)
)
theme_set(theme_minimal(base_size = 12) + transparent_theme)

## ----setup, message = FALSE---------------------------------------------------
library(EraBrewer)
library(ggplot2)

## ----list-palettes------------------------------------------------------------
names(EraPalettes)

## ----build-palettes-----------------------------------------------------------
discrete_pal   <- era.brewer("Lover2",    n = 3)
continuous_pal <- era.brewer("Midnight2", n = 100, type = "continuous")

## ----swatch-lover2, fig.height = 1.3------------------------------------------
print(era.brewer("Lover2")) + transparent_theme

## ----swatch-midnight2, fig.height = 1.3---------------------------------------
print(era.brewer("Midnight2")) + transparent_theme

## ----discrete-scatter---------------------------------------------------------
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 2.4, alpha = 0.9) +
  scale_color_manual(values = discrete_pal) +
  labs(title = "Iris sepals, colored by species",
       x = "Sepal length (cm)", y = "Sepal width (cm)")

## ----discrete-bar-------------------------------------------------------------
species_means <- aggregate(Sepal.Length ~ Species, iris, mean)

ggplot(species_means, aes(Species, Sepal.Length, fill = Species)) +
  geom_col(width = 0.65) +
  scale_fill_manual(values = discrete_pal) +
  labs(title = "Mean sepal length by species",
       x = NULL, y = "Sepal length (cm)") +
  theme(legend.position = "none")

## ----continuous-scatter-------------------------------------------------------
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Petal.Length)) +
  geom_point(size = 2.4, alpha = 0.95) +
  scale_color_gradientn(colors = continuous_pal) +
  labs(title = "Iris sepals, shaded by petal length",
       x = "Sepal length (cm)", y = "Sepal width (cm)",
       color = "Petal length")

## ----continuous-heatmap, fig.height = 4.2-------------------------------------
cor_mat <- cor(mtcars)
cor_df  <- as.data.frame(as.table(cor_mat))
names(cor_df) <- c("Var1", "Var2", "rho")

ggplot(cor_df, aes(Var1, Var2, fill = rho)) +
  geom_tile(color = "white", linewidth = 0.4) +
  scale_fill_gradientn(colors = continuous_pal,
                       limits = c(-1, 1),
                       breaks = c(-1, -0.5, 0, 0.5, 1)) +
  coord_fixed() +
  labs(title = "mtcars correlation matrix",
       x = NULL, y = NULL, fill = expression(rho)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        panel.grid  = element_blank())

