Diverging Scale

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Diverging Scale 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/diverging-scale?lang=en

The diverging scale is a graph that maps a continuous, quantitative input to a continuous fixed interpolator.

Setup

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

  • Programming language: R

  • Dependent packages: data.table; jsonlite; ggcharts

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

# Load packages
library(data.table)
library(jsonlite)
library(ggcharts)
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
 ggcharts   * 0.2.1   2020-05-20 [1] RSPM
 ggplot2    * 4.0.1   2025-11-14 [1] RSPM
 jsonlite   * 2.0.0   2025-03-27 [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

The first column is a list of model names, and the remaining columns enter the relevant indicators and corresponding values.

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

# convert data structure
data <- dplyr::transmute(.data = data, x = model, y = scale(hp))

# View data
head(data)
                  x          y
1         Mazda RX4 -0.6130929
2     Mazda RX4 Wag -0.6130929
3        Datsun 710 -0.9123057
4    Hornet 4 Drive -0.6130929
5 Hornet Sportabout  0.5309561
6           Valiant -0.7010967

Visualization

1.Barplot

# Diverging Scale Barplot
fill_colors <- c("#C20B01", "#196ABD")
fill_colors <- fill_colors[c(any(data[, "y"] > 0), any(data[, "y"] < 0))]
p <- diverging_bar_chart(data = data, x = x, y = y, bar_colors = fill_colors,
                         text_color = '#000000') + 
  theme(axis.text.x = element_text(color = "#000000"),
        axis.title.x = element_text(colour = "#000000"),
        axis.title.y = element_text(colour = "#000000"),
        plot.background = element_blank()) + 
  labs(x = "model", y = "scale(hp)", title = "")

p
FigureΒ 1: Diverging Scale Barplot

Hp data is shown on the horizontal axis, model names (classification) are shown on the vertical axis, models above average are shown in red, and models below average are shown in blue. Data is assigned on a scale of 2 by size.

2.Lollipop Plot

# Diverging Scale Lollipop Plot
fill_colors <- c("#C20B01", "#196ABD")
fill_colors <- fill_colors[c(any(data[, "y"] > 0), any(data[, "y"] < 0))]
p <- diverging_lollipop_chart(
  data = data, x = x, y = y, lollipop_colors = fill_colors,
  line_size = 0.3, point_size = 1.9, text_color = '#000000') + 
  theme(axis.text.x = element_text(color = "#000000"),
        axis.title.x = element_text(colour = "#000000"),
        axis.title.y = element_text(colour = "#000000"),
        plot.background = element_blank()) + 
  labs(x = "model", y = "scale(hp)", title = "")

p
FigureΒ 2: Diverging Scale Lollipop Plot