Mosaic plots with ggplot2

Haley Jeppson and Heike Hofmann

2018-09-12

Introduction

Basics of ggmosaic

Creation of ggmosaic

ggmosaic was created primarily using ggproto and the productplots package

ggproto allows you to extend ggplot2 from within your own packages

ggplot2 limitations

ggplot2 is not capable of handling a variable number of variables

These limitations also lead to issues with the labeling, but those can be fixed manually.

geom_mosaic: setting the aesthetics

Aesthetics that can be set:

These values are then sent through productplots functions to create the formula for the desired distribution

Formula: weight ~ fill + x | conds

From the aesthetics to the formula

Example of how the formula is built

  • weight = 1
  • x = product(Y, X)
  • fill = W
  • conds = product(Z)

These aesthetics set up the formula for the distribution:

Formula: 1 ~ W + X + Y | Z

Because a mosaic plot is constructed hierarchically through alternating spines, the ordering of the variables is very important.

1 ~ X

1 ~ Y + X

1 ~ X + Y / Z

Alternative to conditioning: facetting

Importance of ordering

Other features of geom_mosaic

Arguments unique to geom_mosaic:

Divider function: Types of partitioning

Four options available for each partion:

Partitioning with one or more variables

geom_mosaic: offset

offset: Set the space between the first spine

Adjusting the offset

Plotly