Learn R Programming

ggmosaic

ggmosaic was designed to create visualizations of categorical data and is capable of producing bar charts, stacked bar charts, mosaic plots, and double decker plots.

Installation

You can install ggmosaic from github with:

# install.packages("devtools")
devtools::install_github("haleyjeppson/ggmosaic")

Example

library(ggmosaic)
#> Loading required package: ggplot2
ggplot(data = fly) +
  geom_mosaic(aes(x = product(rude_to_recline), fill=do_you_recline)) +
  theme_mosaic()

geom_mosaic: setting the aesthetics

In geom_mosaic(), the following aesthetics can be specified:

  • weight: select a weighting variable

  • x: select variables to add to formula

    • declared as x = product(var2, var1, ...)
  • alpha: add an alpha transparency to the selected variable

    • unless the variable is called in x, it will be added to the formula in the first position
  • fill: select a variable to be filled

    • unless the variable is called in x, it will be added to the formula in the first position after the optional alpha variable.
  • conds : select a variable to condition on

    • declared as conds = product(cond1, cond2, ...)

These values are then sent through repurposed productplots functions to create the desired formula: weight ~ alpha + fill + x | conds.

Version compatibility issues with ggplot2

Since the initial release of ggmosaic, ggplot2 has evolved considerably. And as ggplot2 continues to evolve, ggmosaic must continue to evolve alongside it. Although these changes affect the underlying code and not the general usage of ggmosaic, the general user may need to be aware of compatibility issues that can arise between versions. The table below summarizes the compatibility between versions.

ggmosaicggplot2Axis labelsTick marks
0.3.33.3.3xx
0.3.03.3.0xx
0.2.23.3.0Default labels are okay, but must use scale_*_productlist() to modifyNo tick marks
0.2.23.2.0Default labels okay, but must use scale_*_productlist() to modifyx
0.2.03.2.0Default labels are wrong, but can use labs() to modifyx

Copy Link

Version

Install

install.packages('ggmosaic')

Monthly Downloads

5,571

Version

0.3.3

License

GPL (>= 2)

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

February 23rd, 2021

Functions in ggmosaic (0.3.3)

scale_x_productlist

Determining scales for mosaics
product

Wrapper for a list
scale_type.productlist

Helper function for determining scales
StatMosaic

Geom proto
mosaic

Template for a mosaic plot. A mosaic plot is composed of spines in alternating directions.
squeeze

Internal helper function
theme_mosaic

Theme for mosaic plots
spine

Spine partition: divide longest dimension.
vspine

Vertical spine partition: width constant, height varies.
prodcalc

Calculate frequencies.
hbar

Horizontal bar partition: width constant, height varies.
hspine

Horizontal spine partition: height constant, width varies.
happy

Data related to happiness from the general social survey.
geom_mosaic_text

Labeling for Mosaic plots.
vbar

Vertical bar partition: height constant, width varies.
titanic

Passengers and crew on board the Titanic
GeomMosaicText

Geom proto
ddecker

Template for a double decker plot. A double decker plot is composed of a sequence of spines in the same direction, with the final spine in the opposite direction.
geom_mosaic

Mosaic plots.
StatMosaicText

Geom proto
GeomMosaicJitter

Geom proto
StatMosaicJitter

Geom proto
GeomMosaic

Geom proto
fly

Flying Etiquette Survey Data
geom_mosaic_jitter

Jittered dots in Mosaic plots.