Skip to contents

Overview

ggblanket has over thirty gg_* wrapper functions. This article will demonstrate each of these.

gg_area

economics |>
  gg_area(
    x = date,
    y = unemploy,
    y_title = "Unemployment",
    pal = "#1B9E77")

gg_bar

penguins |>
  gg_bar(
    x = sex,
    col = sex,
    facet = species,
    width = 0.75,
    x_labels = \(x) str_to_sentence(x),
    pal = c("#2596be", "#fc7c24"))

gg_bin_2d

diamonds |>
  gg_bin_2d(
    x = carat,
    y = price,
    pal = viridis::cividis(9))

gg_blank

penguins |>
  gg_blank(
    x = flipper_length_mm,
    y = body_mass_g,
    col = sex,
    facet = species,
    col_labels = \(x) str_to_sentence(x),
    pal = c("#2596be", "#fc7c24"),
    y_include = 0)

gg_boxplot

penguins |>
  gg_boxplot(
    x = sex,
    y = body_mass_g,
    col = sex,
    facet = species,
    x_labels = snakecase::to_sentence_case,
    pal = c("#2596be", "#fc7c24"))

gg_col

penguins |>
  group_by(sex, species) |>
  summarise(flipper_length_mm = mean(flipper_length_mm, na.rm = TRUE)) |>
  tidyr::drop_na(sex) |>
  mutate(species =str_to_sentence(species)) |> 
  gg_col(
    x = flipper_length_mm,
    y = species,
    col = sex,
    width = 0.75,
    position = "dodge",
    pal = c("#2596be", "#fc7c24"))

gg_contour

ggplot2::faithfuld |>
 gg_contour(
   x = waiting,
   y = eruptions,
   z = density,
   bins = 8)

gg_contour_filled

ggplot2::faithfuld |>
 gg_contour_filled(
   x = waiting,
   y = eruptions,
   z = density,
   bins = 8)

gg_crossbar

data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)) |>
  gg_crossbar(
    x = trt,
    y = resp,
    ymin = lower,
    ymax = upper,
    col = group,
    width = 0.5,
    x_title = "Treatment",
    y_title = "Response",
    pal = c("#2596be", "#fc7c24"))

gg_density

penguins |>
  tidyr::drop_na(sex) |>
  gg_density(
    x = flipper_length_mm,
    col = sex,
    facet = species,
    pal = c("#2596be", "#fc7c24"),
    col_labels = \(x) str_to_sentence(x))

gg_density_2d

faithful |>
  gg_density_2d(
    x = waiting,
    y = eruptions,
    bins = 8)

gg_density_2d_filled

faithful |>
  gg_density_2d_filled(
    x = waiting,
    y = eruptions,
    bins = 8)

gg_errorbar

data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
) |>
  gg_errorbar(
    x = trt,
    ymin = lower,
    ymax = upper,
    col = group,
    width = 0.1,
    pal = c("#2596be", "#fc7c24"),
    x_title = "Treatment",
    y_title = "Response")

gg_freqpoly

penguins |>
  tidyr::drop_na(sex) |>
  gg_freqpoly(
    x = flipper_length_mm,
    col = sex,
    facet = species,
    pal = c("#2596be", "#fc7c24"),
    col_labels = \(x) str_to_sentence(x))

gg_hex

diamonds |>
  gg_hex(
    x = carat,
    y = price,
    pal = viridis::cividis(9),
    y_limits = c(0, 20000),
    coord = ggplot2::coord_cartesian(clip = "on"))

gg_histogram

penguins |>
  tidyr::drop_na(sex) |>
  gg_histogram(
    x = flipper_length_mm,
    col = sex,
    facet = species,
    bins = 50,
    pal = c("#2596be", "#fc7c24"),
    col_labels = \(x) str_to_sentence(x))

gg_freqpoly

penguins |>
  tidyr::drop_na(sex) |>
  gg_freqpoly(
    x = flipper_length_mm,
    col = sex,
    facet = species,
    pal = c("#2596be", "#fc7c24"),
    col_labels = \(x) str_to_sentence(x))

gg_jitter

set.seed(123)

penguins |>
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = flipper_length_mm,
    col_continuous = "steps",
    y_include = 0,
    position = position_jitter(height = 0))

gg_label

bind_rows(
  mtcars |> slice_min(order_by = mpg),
  mtcars |> slice_max(order_by = mpg)) |>
  tibble::rownames_to_column("model") |>
  gg_label(
    x = model,
    y = mpg,
    col = mpg,
    label = model,
    y_include = 0,
    pal = RColorBrewer::brewer.pal(9, "RdBu"),
    y_title = "Miles per gallon")

gg_line

economics |>
  gg_line(
    x = date,
    y = unemploy,
    y_title = "Unemployment",
    pal = "#9E361B",
    y_include = 0)

gg_linerange

data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)) |>
  gg_linerange(
    x = trt,
    ymin = lower,
    ymax = upper,
    col = group,
    position = position_dodge(width = 0.2),
    x_title = "Treatment",
    y_title = "Response")

gg_path

economics |>
  mutate(unemploy_rate = unemploy / pop) |>
  gg_path(
    x = unemploy_rate,
    y = psavert,
    x_title = "Unemployment rate",
    y_title = "Personal savings rate",
    y_include = 0)

gg_point

penguins |>
  gg_point(
    x = flipper_length_mm,
    y = body_mass_g,
    col = sex,
    facet = species,
    col_labels = \(x) str_to_sentence(x),
    pal = c("#2596be", "#fc7c24"))

gg_pointrange

data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)) |>
  gg_pointrange(
    x = trt,
    y = resp,
    col = group,
    ymin = lower,
    ymax = upper,
    position = position_dodge(width = 0.2),
    size = 0.2,
    pal = c("#2596be", "#fc7c24"),
    x_title = "Treatment",
    y_title = "Response")

gg_polygon

ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))

values <- data.frame(
  id = ids,
  value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
)

positions <- data.frame(
  id = rep(ids, each = 4),
  x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
        0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
  y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
        2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)

datapoly <- merge(values, positions, by = c("id"))

datapoly |>
  gg_polygon(
    x = x,
    y = y,
    col = value,
    group = id,
    pal = viridis::cividis(9))

gg_qq

penguins |>
  gg_qq(
    sample = body_mass_g,
    facet = species,
    pal = "#1B9E77", 
    coord = ggplot2::coord_cartesian(clip = "on")) +
  geom_qq_line(alpha = 0.5)

gg_raster

faithfuld |>
  gg_raster(
    x = waiting,
    y = eruptions,
    col = density,
    pal = viridis::cividis(9))

gg_rect

data.frame(
  x = rep(c(2, 5, 7, 9, 12), 2),
  y = rep(c(1, 2), each = 5),
  z = factor(rep(1:5, each = 2)),
  w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)) |>
  mutate(
    xmin = x - w / 2,
    xmax = x + w / 2,
    ymin = y,
    ymax = y + 1
  ) |>
  gg_rect(
    xmin = xmin,
    xmax = xmax,
    ymin = ymin,
    ymax = ymax,
    col = z)

gg_ribbon

data.frame(year = 1875:1972, level = as.vector(LakeHuron)) |>
  mutate(level_min = level - 1, level_max = level + 1) |>
  gg_ribbon(
    x = year,
    ymin = level_min,
    ymax = level_max,
    pal = scales::alpha("#1B9E77", 0),
    y_title = "Level") +
  geom_line(aes(x = year, y = level), col = "#1B9E77")

gg_segment

data.frame(x1 = 2.62, x2 = 3.57, y1 = 21.0, y2 = 15.0) |>
  gg_segment(
    x = x1,
    xend = x2,
    y = y1,
    yend = y2,
    pal = "#1B9E77")

gg_sf

if (requireNamespace("sf", quietly = TRUE)) {
  nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
  
  nc |>
    gg_sf(
      col = AREA,
      pal = viridis::cividis(9))
}

gg_smooth

penguins |>
  tidyr::drop_na(sex) |>
  gg_smooth(
    x = flipper_length_mm,
    y = body_mass_g,
    facet = species)

gg_step

economics |>
  gg_step(
    x = date,
    y = unemploy,
    y_title = "Unemployment",
    pal = "#1B9E77",
    x_limits = c(lubridate::ymd("2010-01-01"), lubridate::NA_Date_), 
    y_include = 0,
    coord = ggplot2::coord_cartesian(clip = "on"))

gg_text

bind_rows(
  mtcars |> slice_min(order_by = mpg),
  mtcars |> slice_max(order_by = mpg)) |>
  tibble::rownames_to_column("model") |>
  gg_text(
    x = model,
    y = mpg,
    col = mpg,
    label = model,
    y_include = 0,
    pal = RColorBrewer::brewer.pal(9, "RdBu"),
    y_title = "Miles per gallon")

gg_tile

penguins |>
  group_by(species, sex) |>
  summarise(flipper_length_mm = mean(flipper_length_mm, na.rm = TRUE)) |>
  gg_tile(
    x = sex,
    y = species,
    col = flipper_length_mm,
    pal = RColorBrewer::brewer.pal(9, "Blues"),
    col_labels = \(x) str_to_sentence(x))

gg_violin

penguins |>
  tidyr::drop_na(sex) |>
  mutate(sex = str_to_sentence(sex)) |>
  gg_violin(
    x = sex,
    y = body_mass_g,
    col = sex,
    facet = species,
    pal = c("#2596be", "#fc7c24"))