| geom_mosaic | R Documentation |
Adds a mosaic-style rectangles layer to a ggplot, visualizing the
joint distribution of categorical variables.
Each rectangle's size reflects the proportion of observations for
combinations of x and fill.
geom_mosaic(
mapping = NULL,
data = NULL,
stat = "mosaic",
position = "identity",
...,
na.rm = FALSE,
show.legend = TRUE,
inherit.aes = TRUE
)
.calculate_coordinates(data)
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. |
stat |
The statistical transformation to use on the data. Defaults to |
position |
Position adjustment. Defaults to |
... |
Other arguments passed to |
na.rm |
Logical. Should missing values be removed? |
show.legend |
Logical. Should this layer be included in the legends? |
inherit.aes |
Logical. If |
A ggplot2 layer that adds mosaic rectangles to the plot.
.calculate_coordinates(): Computes the coordinates for rectangles in a mosaic plot based
on combinations of x and fill variables.
For each unique x and fill, calculates the proportional
widths and heights, stacking rectangles within each x group.
A data frame with columns: x, fill, xmin, xmax, ymin, ymax,
representing the position and size of each rectangle.
df <- data.frame(RACE = c("Black", "White", "Black", "Asian"), SEX = c("M", "M", "F", "F"))
library(ggplot2)
ggplot(df) +
geom_mosaic(aes(x = RACE, fill = SEX))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.