Learn R Programming

ggblanket (version 12.4.0)

bind_each_all: Bind each all.

Description

Binds data to support plotting each category and all combined data.

Usage

bind_each_all(
  data,
  ...,
  name = "each_all",
  each = "Each",
  all = "All",
  all_after = Inf
)

Value

A data frame or tibble

Arguments

data

A data frame or tibble.

...

An unquoted variable.

name

A variable name. Defaults to each_all.

each

A string for the each value. Defaults to "Each".

all

A string for the all value. Defaults to "All".

all_after

A number for where the all value should be placed after. Use 0 for first or Inf for last. Defaults to Inf.

Examples

Run this code
library(dplyr)
library(ggplot2)
library(palmerpenguins)

set_blanket()

penguins |>
  distinct(species)

penguins |>
  bind_each_all(species) |>
  distinct(species, each_all)

penguins |>
  bind_each_all(species) |>
  gg_jitter(
    x = species,
    y = body_mass_g,
  )

penguins |>
  bind_each_all(species) |>
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = each_all,
    col_palette = c(blue, grey),
  ) +
  theme(legend.position = "none")

penguins |>
  bind_each_all(species) |>
  group_by(species, each_all) |>
  summarise(across(body_mass_g, \(x) mean(x, na.rm = TRUE))) |>
  gg_col(
    x = species,
    y = body_mass_g,
    col = each_all,
    col_palette = c(blue, grey),
    width = 0.5,
    y_label = "Average body mass g",
  ) +
  theme(legend.position = "none")

penguins |>
  bind_each_all(species, all = "All\nspecies") |>
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = each_all,
    col_palette = c(blue, grey),
    facet = each_all,
    facet_layout = "grid",
    facet_scales = "free_x",
    facet_space = "free_x",
  ) +
  theme(legend.position = "none") +
  theme(strip.text.x = element_blank()) +
  labs(x = NULL)

Run the code above in your browser using DataLab