Tricolor Histogram

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Tricolor Histogram 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/tricolor-histogram?lang=en

The tricolored histogram divides the histogram into three regions: low-value zone, middle-value zone, and high-value zone, using three different colors.

Setup

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

  • Programming language: R

  • Dependent packages: data.table; jsonlite; ggplot2

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

# Load packages
library(data.table)
library(jsonlite)
library(ggplot2)
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
 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

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

# convert data structure
data$draw_color <- ifelse(data$value < 5, "#F44336",
                          ifelse(data$value > 7, "#006064", "#3F51B5")
)

# View data
head(data)
    values draw_color
1 6.063364    #3F51B5
2 4.354496    #F44336
3 5.569931    #3F51B5
4 5.602465    #3F51B5
5 6.831472    #3F51B5
6 6.055604    #3F51B5

Visualization

# Tricolor Histogram
p <- ggplot(data, aes(x = values, fill = draw_color)) +
  geom_histogram(alpha = 0.5, binwidth = 0.05, position = "identity") +
    scale_fill_manual(values = c("#3F51B5", "#006064", "#F44336")) +
  theme_bw()

p
FigureΒ 1: Tricolor Histogram