library(ggplot2)
library(tidyverse)
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Introduction

ggplot2 tools helps you to quickly create ggplots according to Graydon's style specifications.

First let's load the package:

library(graydon.package)

Graydon colouring aesthetics and grid background

ggplot(mtcars,
       aes(x = qsec, y = hp, col = as.factor(carb))) +
  geom_point() +
  guides(fill = FALSE, col = FALSE) +
  scale_color_graydon() +
  theme_graydon("grid")

Graydon fill aesthetic and horizontal gridlines only

ggplot(mtcars,
       aes(x = gear, y = qsec, fill = factor(carb))) +
  geom_col() +
  guides(fill = FALSE, col = FALSE) +
  scale_fill_graydon() +
  theme_graydon("horizontal")

Graydon fill aesthetic and vertical gridlines only

ggplot(mtcars,
       aes(x = gear, y = qsec, fill = factor(cyl))) +
  geom_col() +
  guides(fill = FALSE, col = FALSE) +
  coord_flip() +
  scale_fill_graydon() +
  theme_graydon("vertical")

Graydon gradient aesthetics and no background

ggplot(mtcars, aes(x = hp, y = wt, fill = qsec, col = qsec)) +
  geom_jitter(size = 15, shape = 22) +
  guides(fill = FALSE, col = FALSE) +
  scale_color_gradient(low = col_graydon[8], high = col_graydon[4]) +
  scale_fill_gradient(low = col_graydon[2], high = col_graydon[1]) +
  theme_graydon("blank")

Axis formatting

Numbers

You can turn numeric results is presentable formats by using the scale_x_numeric or scale_y_numeric function. These functions optionally take the following input variables:

diamonds %>% 
  group_by(color, cut) %>% 
  summarise(price_median = median(price)) %>% 
ggplot(aes(x = cut, y = price_median, col = color)) +
  geom_line(aes(group = color), size = 1, alpha = 0.7) +
  geom_point(size = 5) +
  labs(x = "Cut quality", y = "Price", col = "Color") +
  scale_color_graydon() +
  scale_y_numeric(format_EN = TRUE) +
  theme_graydon("grid")

Currency

You can turn numeric results is presentable formats by using the scale_x_currency or scale_y_currency function. These functions optionally take the following input variables:

Some examples:

diamonds %>% 
  group_by(color, cut) %>% 
  summarise(price_median = median(price)) %>% 
ggplot(aes(x = cut, y = price_median, col = color)) +
  geom_line(aes(group = color), size = 1, alpha = 0.7) +
  geom_point(size = 5) +
  labs(x = "Cut quality", y = "Price", col = "Color") +
  scale_color_graydon() +
  scale_y_currency(currency = "EUR", scale = "k", number_decimals = 1) +
  theme_graydon("grid")

Percent

The functions scale_x_percent and scale_y_percent can be used in ggplots to for percentage formatted axes. These functions optionally take the following input variables:

diamonds %>% 
  mutate(depth = depth / 100) %>% 
  group_by(cut) %>% 
  summarise(price_median = median(price),
            depth_median = median(depth)) %>% 
ggplot(aes(x = depth_median, y = price_median, col = cut)) +
  geom_point(size = 7, alpha = 0.7) +
  labs(x = "Depth", y = "Price", col = "Cut") +
  scale_color_graydon() +
  scale_x_percent(format_EN = TRUE) +
  scale_y_currency(currency = "GBP") +
  theme_graydon("grid")

Saving hires plots to PNG files

Plots are nice to have within R Studio or a R Markdown document, but we need them in PowerPoint as well. You can automatically save high res PNG files using the save_plot_to_png function. To use this function you must store the plot in a variable, pass a filename (without extension) and indicate whether you want a squared plot (for side by side PP slides) or not (a slide filling PP slide).

Let's create some high res abstract art:

p_abstract <- ggplot(mtcars, aes(x = hp, y = wt, fill = qsec, col = qsec)) +
  geom_jitter(size = 15, shape = 22, alpha = 0.7) +
  guides(fill = FALSE, col = FALSE) +
  scale_color_gradient(low = col_graydon[8], high = col_graydon[4]) +
  scale_fill_gradient(low = col_graydon[2], high = col_graydon[1]) +
  theme_graydon("blank")

save_plot_to_png(p_abstract, "abstract_art", squared = TRUE)

p_abstract


mark-me/graydon.package documentation built on Nov. 14, 2023, 5:31 p.m.