Tricolor Histogram

Authors

[Editor] Hu Zheng;

[Contributors]

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)

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