# 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)
Tricolor Histogram
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:
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
Data Preparation
# Load data
<- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/tricolor-histogram/data.json")$exampleData$textarea[[1]])
data <- as.data.frame(data)
data
# convert data structure
$draw_color <- ifelse(data$value < 5, "#F44336",
dataifelse(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
<- ggplot(data, aes(x = values, fill = draw_color)) +
p geom_histogram(alpha = 0.5, binwidth = 0.05, position = "identity") +
scale_fill_manual(values = c("#3F51B5", "#006064", "#F44336")) +
theme_bw()
p
