Mosaic Ratio Plot

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Mosaic Ratio Plot plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/mosaic?lang=en

Use mosaic blocks to show data proportions.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: data.table; jsonlite; vcd; DescTools; ggplotify

# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("vcd", quietly = TRUE)) {
  install.packages("vcd")
}
if (!requireNamespace("DescTools", quietly = TRUE)) {
  install.packages("DescTools")
}
if (!requireNamespace("ggplotify", quietly = TRUE)) {
  install.packages("ggplotify")
}

# Load packages
library(data.table)
library(jsonlite)
library(vcd)
library(DescTools)
library(ggplotify)
sessioninfo::session_info("attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.2 (2025-10-31)
 os       Ubuntu 24.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-01-17
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.8.27 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package    * version date (UTC) lib source
 data.table * 1.18.0  2025-12-24 [1] RSPM
 DescTools  * 0.99.60 2025-03-28 [1] RSPM
 ggplotify  * 0.1.3   2025-09-20 [1] RSPM
 jsonlite   * 2.0.0   2025-03-27 [1] RSPM
 vcd        * 1.4-13  2024-09-16 [1] RSPM

 [1] /home/runner/work/_temp/Library
 [2] /opt/R/4.5.2/lib/R/site-library
 [3] /opt/R/4.5.2/lib/R/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────

Data Preparation

# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/mosaic/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)

# Convert data structure
tbl <- xtabs(~ Survived + PassengerClass + Gender, data)

# View data
head(data)
  PassengerId Survived PassengerClass
1           1        0              3
2           2        1              1
3           3        1              3
4           4        1              1
5           5        0              3
6           6        0              3
                                                 Name Gender Age SibSp Parch
1                             Braund, Mr. Owen Harris   Male  22     1     0
2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) Female  38     1     0
3                              Heikkinen, Miss. Laina Female  26     0     0
4        Futrelle, Mrs. Jacques Heath (Lily May Peel) Female  35     1     0
5                            Allen, Mr. William Henry   Male  35     0     0
6                                    Moran, Mr. James   Male  NA     0     0
            Ticket    Fare Cabin Embarked
1        A/5 21171  7.2500  <NA>        S
2         PC 17599 71.2833   C85        C
3 STON/O2. 3101282  7.9250  <NA>        S
4           113803 53.1000  C123        S
5           373450  8.0500  <NA>        S
6           330877  8.4583  <NA>        Q

Visualization

# Mosaic Ratio Plot
p <- as.ggplot(function() {
  mosaic(tbl, shade = TRUE, legend = TRUE, main = "Mosaic Ratio Plot",
         gp = shading_binary(tbl, col = c("#3B4992FF","#EE0000FF")))
})

p
FigureΒ 1: Mosaic Ratio Plot