发色与眼睛颜色
列越宽,这种发色的人越多。同一列里,哪种眼睛颜色占得高,哪种就更常见。图块之间没有留白,每块的面积占比就是这组人占总人数的比例。

PNG · SVG · PDF · 下载 R 脚本 · 软件版本
查看 R 代码
library(ggplot2)
library(ggforma)
counts <- marginSums(HairEyeColor, c("Hair", "Eye"))
ggmosaic(counts, shade = "category", category = "Eye", gap = 0, labeling = "border", label = NULL) +
labs(title = "Hair and eye colour", subtitle = "Area is proportional to the observed count")查看三种格式的导出代码
# Run in the folder containing the downloaded mosaic-2d.R script.
draw_example <- function(ext) {
path <- paste0('mosaic-2d.', ext)
switch(ext,
png = ragg::agg_png(path, width = 7.5, height = 4.35, units = 'in', res = 144, background = 'white'),
svg = grDevices::svg(path, width = 7.5, height = 4.35, bg = 'white'),
pdf = grDevices::cairo_pdf(path, width = 7.5, height = 4.35, bg = 'white')
)
on.exit(grDevices::dev.off())
source('mosaic-2d.R', local = new.env(), echo = FALSE, print.eval = TRUE, encoding = 'UTF-8')
}
invisible(lapply(c('png', 'svg', 'pdf'), draw_example))




















