ggformaEnglish

绘制多维马赛克图

函数索引 / 马赛克图

ggmosaic()

把多个分类变量的组合画成矩形,可用面积展示频数,用颜色观察哪些组合偏离统计模型。

怎么用

可以直接传入列联表。使用数据框时,指定要比较的分类变量;如果数据已汇总,再指定频数列。按条件分面时,每个面板独立计算。残差颜色的分界用于描述偏离程度。

返回结果

返回标准 ggplot 图形,可继续添加标题、主题或图层。首个图层的数据含观测频数、期望频数、残差和矩形边界。

调用方式

ggmosaic(
  x,
  ...,
  data = NULL,
  vars = NULL,
  weight = NULL,
  facet = NULL,
  model = "independence",
  expected = NULL,
  structural = NULL,
  weight_type = "frequency",
  direction = NULL,
  gap = 0,
  sort = "level",
  max_cells = 100000L,
  shade = "none",
  category = NULL,
  cuts = c(2, 4),
  label = "level",
  hierarchy = TRUE,
  label_format = "plain",
  label_size = 9,
  family = "sans",
  padding = grid::unit(1, "mm"),
  angle = 0,
  mark_zero = FALSE,
  border = "white",
  linewidth = 0.25,
  linetype = 1,
  lang = NULL,
  dictionary = NULL,
  given = NULL,
  residual = "pearson",
  colours = NULL,
  hierarchy_label = "{level}",
  label_hjust = 0.5,
  label_vjust = 0.5,
  type = c("observed", "expected"),
  condvars = NULL,
  highlighting = NULL,
  highlighting_direction = NULL,
  highlighting_fill = NULL,
  split_vertical = NULL,
  zero_split = FALSE,
  zero_size = 1.7,
  zero_shade = FALSE,
  labeling = NULL,
  label_sides = NULL,
  label_rotation = NULL,
  view = c("mosaic", "doubledecker", "spine", "conditional"),
  subset = NULL,
  na.action = NULL,
  panel = NULL,
  label_repeat = TRUE,
  label_abbrev = NULL,
  breaks = NULL,
  fit_scope = c("panel", "table"),
  layout_size = NULL,
  residuals = NULL,
  residual_title = NULL,
  zero_gp = grid::gpar(),
  zero_pch = 4L,
  label_style = "plain",
  global_test = NULL,
  global_alpha = 0.05
)

参数

x、...、data、vars、weight、facet、model、expected, structural、weight_type、direction、gap、sort、max_cells、shade、category、cuts、label、hierarchy、label_format、label_size、family、padding、angle、mark_zero、border, linewidth, linetype、lang、dictionary、given、residual、colours、hierarchy_label、label_hjust, label_vjust、type、condvars、highlighting、highlighting_direction、highlighting_fill、split_vertical、zero_split、zero_size、zero_shade、labeling、label_sides, label_rotation、view、subset、na.action、panel、label_repeat, label_abbrev、breaks、fit_scope、layout_size、residuals、residual_title、zero_gp、zero_pch、label_style、global_test、global_alpha

逐项参数说明(英文)

R 示例

ggmosaic(HairEyeColor, shade = "bands", hierarchy = 1)
d <- as.data.frame(Titanic)
ggmosaic(~ Class + Age, data = d, weight = Freq, facet = Survived)

查看完整英文文档