把马赛克布局添加为图层

函数索引 / 马赛克图
geom_mosaic() · stat_mosaic()
geom_mosaic() 绘制矩形,stat_mosaic() 为每个面板计算一份完整的列联表。
怎么用
把分类和频数映射给图层;用 after_stat(residual) 读取模型残差。呈现面积时使用线性笛卡尔坐标。层级标签和零值标记可用 ggmosaic()。
返回结果
返回 ggplot2 图层,提供 observed、expected、residual、proportion、矩形坐标及原始分类键。
调用方式
geom_mosaic(
mapping = NULL,
data = NULL,
stat = "mosaic",
position = "identity",
...,
vars = NULL,
model = "independence",
expected = NULL,
structural = NULL,
given = NULL,
residual = "pearson",
weight_type = "frequency",
direction = NULL,
gap = 0,
sort = "level",
max_cells = 100000L,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
type = "observed",
zero_split = FALSE,
layout_size = NULL,
residuals = NULL
)
stat_mosaic(
mapping = NULL,
data = NULL,
geom = "mosaic",
position = "identity",
...,
vars = NULL,
model = "independence",
expected = NULL,
structural = NULL,
given = NULL,
residual = "pearson",
weight_type = "frequency",
direction = NULL,
gap = 0,
sort = "level",
max_cells = 100000L,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
type = "observed",
zero_split = FALSE,
layout_size = NULL,
residuals = NULL
)参数
mapping、data、stat, geom、position、...、vars、model、expected, structural、given、residual、weight_type、direction、gap、sort、max_cells、na.rm、show.legend, inherit.aes、type、zero_split、layout_size、residuals
R 示例
d <- as.data.frame(Titanic)
ggplot2::ggplot(d) +
geom_mosaic(ggplot2::aes(weight = Freq, fill = ggplot2::after_stat(residual)),
vars = c(Class, Sex, Age)) +
ggplot2::facet_wrap(~ Survived) + ggplot2::coord_fixed()
ggplot2::ggplot(d, ggplot2::aes(weight = Freq)) +
stat_mosaic(ggplot2::aes(x = ggplot2::after_stat(label_x),
y = ggplot2::after_stat(label_y),
label = ggplot2::after_stat(observed)),
vars = c(Class, Sex), geom = "text") +
ggplot2::coord_fixed()