Learn R Programming

⚠️There's a newer version (12.2.0) of this package.Take me there.

ggblanket

Overview

{ggblanket} is a package of wrapper functions around the amazing ‘ggplot2’ package to simplify pretty visualisation.

With this objective in mind, the {ggblanket} package:

  1. uses gg_* functions that each wrap a ggplot2::ggplot call with a single ggplot2::geom_* function
  2. merges col and fill aesthetics into a single col aesthetic
  3. provides colour customisation via pal and alpha arguments
  4. treats faceting as an aesthetic
  5. provides good-looking default x and y scales
  6. provides prefixed arguments for customisable scale adjustment
  7. arranges horizontal geom y and col labels etc to be in correct order
  8. converts unspecified titles to snakecase::to_sentence by default
  9. provides access to all of the relevant geom arg’s through the dots argument
  10. supports ggplotly use.
  11. {ggblanket} provides a gg_blank function for even more flexibility.

Installation

Install either from CRAN with:

install.packages("ggblanket")

Or install the development version with:

# install.packages("devtools")
devtools::install_github("davidhodge931/ggblanket")

Website

Click here for the {ggblanket} website.

Examples

library(dplyr)
library(ggplot2)
library(ggblanket)
library(palmerpenguins)
  1. {ggblanket} uses gg_* functions that each wrap a ggplot2::ggplot call with a single ggplot2::geom_* function.
iris %>%
  gg_point(x = Sepal.Width, y = Sepal.Length, col = Species)

  1. {ggblanket} merges col and fill aesthetics into a single col aesthetic.
penguins %>% 
  gg_histogram(x = body_mass_g, col = species) 

  1. {ggblanket} provides colour customisation via pal and alpha arguments.
penguins %>% 
  gg_density(x = body_mass_g, col = species, 
             pal = pals::brewer.dark2(3), 
             alpha = 0.5)

  1. {ggblanket} treats faceting as an aesthetic.
penguins %>% 
  tidyr::drop_na() %>% 
  gg_violin(x = sex, y = body_mass_g, facet = species)

  1. {ggblanket} provides good-looking default x and y scales.

For where:

  • x categorical and y numeric/date: y_limits default to min/max of y_breaks with y_expand of c(0, 0)
  • y categorical and x numeric/date: x_limits default to min/max of x_breaks with x_expand of c(0, 0)
  • x numeric/date and y numeric/date: y_limits default to min/max of y_breaks with y_expand of c(0, 0), and x_limits default to NULL (i.e. min/max of x variable) and x_expand of c(0.025, 0.025)
storms %>%
  group_by(year) %>%
  filter(between(year, 1980, 2020)) %>%
  summarise(wind = mean(wind, na.rm = TRUE)) %>%
  gg_col(
    x = year,
    y = wind,
    title = "Storm wind speed",
    subtitle = "USA average storm wind speed, 1980\u20132020",
    y_title = "Wind speed (knots)",
    caption = "Source: NOAA"
  ) 

  1. {ggblanket} provides prefixed arguments for customisable scale adjustment.

This is designed to work with the Rstudio autocomplete to help you find the adjustment you need. Press the tab key after typing x_,y_, col_ or facet_ to access this. Then use arrow keys, and press tab again to select.

Available arguments are *_limits, *_expand, *_labels, *_breaks, *_include, and col_intervals arguments.

penguins %>%
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = flipper_length_mm,
    position = ggplot2::position_jitter(width = 0.2, height = 0, seed = 123), 
    col_intervals = ~ santoku::chop_quantiles(.x, probs = seq(0, 1, 0.25)),
    col_legend_place = "b",
    y_include = 0,
    y_breaks = scales::breaks_width(1500), 
    y_labels = scales::label_number()
  )

  1. {ggblanket} arranges horizontal geom y and col labels etc to be in correct order.
penguins %>%
  tidyr::drop_na() %>% 
  group_by(species, sex, island) %>%
  summarise(body_mass_kg = mean(body_mass_g) / 1000) %>%
  gg_col(x = body_mass_kg, y = species, col = sex, facet = island,
         col_labels = stringr::str_to_sentence, 
         position = "dodge", 
         col_legend_place = "b")

  1. {ggblanket} converts unspecified titles to snakecase::to_sentence by default.
penguins %>%
  group_by(species, sex) %>%
  summarise(across(body_mass_g, ~ round(mean(.x, na.rm = TRUE)), 0)) %>% 
  gg_tile(sex, species, col = body_mass_g, 
          x_labels = snakecase::to_sentence_case,
          pal = pals::brewer.blues(9),
          width = 0.9, 
          height = 0.9, 
          size = 0.5,
          title = "Average penguin body mass",
          subtitle = "Palmer Archipelago, Antarctica",
          theme = gg_theme(pal_axis = "#ffffff", pal_ticks = "#ffffff")) +
  geom_text(aes(label = body_mass_g), col = "#232323", size = 3.5) 

  1. {ggblanket} provides access to all of the relevant geom arg’s through the dots argument.
penguins %>%
  tidyr::drop_na() %>% 
  gg_smooth(
    x = bill_length_mm,
    y = flipper_length_mm,
    col = species,
    level = 0.99,
    size = 0.5
    ) 

  1. {ggblanket} supports ggplotly use.

ggplotly won’t work in all situations, and with all functions and arguments. But it does work a lot of the time.

iris %>% 
  mutate(Species = stringr::str_to_sentence(Species)) %>% 
  add_tooltip_text(titles = snakecase::to_sentence_case) %>% 
  gg_point(x = Sepal.Width, 
           y = Sepal.Length, 
           col = Species, 
           text = text, 
           theme = gg_theme("helvetica", y_grid = TRUE)) %>% 
  plotly::ggplotly(tooltip = "text")

  1. {ggblanket} provides a gg_blank function for even more flexibility.
penguins %>%
  tidyr::drop_na() %>%
  mutate(sex = stringr::str_to_sentence(sex)) %>% 
  gg_blank(x = flipper_length_mm, col = sex, facet = species) +
  geom_histogram(aes(y = after_stat(density)), alpha = 0.9) +
  labs(y = "Density")

penguins %>%
  tidyr::drop_na() %>%
  mutate(sex = stringr::str_to_sentence(sex)) %>%
  group_by(species, sex) %>%
  summarise(
    bill_length_mm = round(mean(bill_length_mm, na.rm = TRUE), 0),
    n = n(),
    se = bill_length_mm / sqrt(n),
    upper = bill_length_mm + 1.96 * se,
    lower = bill_length_mm - 1.96 * se
  ) %>%
  gg_blank(
    x = sex,
    y = bill_length_mm,
    col = sex,
    facet = species,
    label = bill_length_mm,
    ymin = lower,
    ymax = upper,
    yend = upper + 10,
    y_include = 0
  ) +
  geom_col(width = 0.75, alpha = 0.9) +
  geom_errorbar(width = 0.1, col = "black") +
  geom_text(aes(y = upper + 5), fill = NA, size = 3)

x_zoom <- c(4, 6)

iris %>%
  gg_blank(
    x = Petal.Length,
    y = Petal.Width,
    col = Species,
    facet_scales = "free_x",
    col_legend_place = "b",
    y_expand = expansion(mult = c(0, 0.05))
  ) +
  geom_rect(aes(
    xmin = x_zoom[1] * 0.975,
    xmax = x_zoom[2] * 1.025,
    ymin = 0,
    ymax = Inf
  ),
  col = NA,
  fill = "#F0F0F0"
  ) +
  geom_point() +
  ggforce::facet_zoom(xlim = c(x_zoom[1], x_zoom[2]), zoom.size = 1) +
  theme(strip.background = element_rect(fill = "#E6E6E6")) +
  theme(panel.spacing = unit(0.2, "cm"))

Copy Link

Version

Install

install.packages('ggblanket')

Monthly Downloads

520

Version

1.2.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

David Hodge

Last Published

July 1st, 2022

Functions in ggblanket (1.2.0)

gg_boxplot

Boxplot ggplot.
add_tooltip_text

Add a tooltip text column of united variable names and values.
gg_errorbar

Errorbar ggplot.
gg_bar

Bar ggplot.
gg_area

Area ggplot.
gg_crossbar

Crossbar ggplot.
gg_freqpoly

Freqpoly ggplot.
gg_blank

Point ggplot.
gg_col

Col ggplot.
gg_density

Density ggplot.
gg_function

Function ggplot.
gg_histogram

Histogram ggplot.
gg_linerange

Linerange ggplot.
gg_line

Line ggplot.
gg_path

Path ggplot.
gg_point

Point ggplot.
gg_label

Label ggplot.
gg_rect

Rect ggplot.
gg_smooth

Smooth ggplot.
gg_raster

Raster ggplot.
gg_sf

Sf ggplot.
gg_jitter

Jitter ggplot.
gg_theme

Quick theme for a ggplot.
gg_tile

Tile ggplot.
gg_violin

Violin ggplot.
gg_pointrange

Pointrange ggplot.
pal_d3_mix

D3 palette reordered.
ggblanket-package

ggblanket: Wrappers to Simplify Pretty 'ggplot2' Visualisation
pal_na

NA palette.
gg_segment

Segment ggplot.
gg_ribbon

Ribbon ggplot.
pal_viridis_mix

Viridis palette reordered.
%>%

Pipe operator
gg_qq

Qq ggplot.
gg_step

Step ggplot.
gg_text

Text ggplot.