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

ggfacet is a simple package providing a wrapper around ggplot2's facetting functions facet_grid and facet_wrap.

library(tidyverse)
library(ggfacet)

The main interest of ggfacet is that it can do this:

facettize(

  iris %>%
    mutate(Species = as.numeric(factor(Species))) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    theme_classic() +
    geom_point() +
    theme(legend.position = "none"),

  cols = "Species",
  prepend = "alpha==",
  parsed = "Species"

)

It basically facilitates mathematical notation into facet strips, as the regular ggplot2 workflow can be a little tedious if you want to parse the strips of the facets as plotmath expressions (e.g. with greek letters).

Other than that, facettize is the main function of the package and expects (1) a ggplot object to split into facets, (2) a rows and/or cols argument containing the quoted names of the variables to facet by. For example:

facettize(

  iris %>%
    mutate(
      Size = ifelse(Petal.Length > mean(Petal.Length), "large", "small")
    ) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    theme_classic() +
    geom_point() +
    theme(legend.position = "none"),

  rows = "Size",
  cols = "Species"

)

Multiple variables can be combined, in rows or in columns, e.g.

facettize(

  iris %>%
    mutate(
      Size = ifelse(Petal.Length > mean(Petal.Length), "large", "small")
    ) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    theme_classic() +
    geom_point() +
    theme(legend.position = "none"),

  cols = c("Species", "Size")

)

In order to use facet_wrap instead of facet_grid, set wrap = TRUE. This way the function will not care about whether facetting variables are provided in rows or columns.

facettize(

  iris %>%
    mutate(
      Size = ifelse(Petal.Length > mean(Petal.Length), "large", "small")
    ) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    theme_classic() +
    geom_point() +
    theme(legend.position = "none"),

  rows = "Size",
  cols = "Species",
  wrap = TRUE

)

Last but not least, you can choose which facetting variables must be parsed as plotmath expressions:

facettize(

  iris %>%
    mutate(
      Species = as.numeric(factor(Species)),
      Size = ifelse(Petal.Length > mean(Petal.Length), "large", "small")
    ) %>%
    ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
    theme_classic() +
    geom_point() +
    theme(legend.position = "none"),

  rows = "Size",
  cols = "Species",
  prepend = "alpha==",
  parsed = "Species"

)


rscherrer/ggfacet documentation built on May 7, 2021, 11:40 a.m.