# 安装包
if (!requireNamespace("ggplot2", quietly = TRUE)) {
install.packages("ggplot2")
}
# 加载包
library(ggplot2)
三色直方图
注记
Hiplot 网站
本页面为 Hiplot Tricolor Histogram
插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:
通过三种颜色将直方图分为低值区,中值区和高值区。
环境配置
系统: Cross-platform (Linux/MacOS/Windows)
编程语言: R
依赖包:
ggplot2
数据准备
# 加载数据
<- read.delim("files/Hiplot/174-tricolor-histogram-data.txt", header = T)
data
# 整理数据格式
$draw_color <- ifelse(data$value < 5, "#F44336",
dataifelse(data$value > 7, "#006064", "#3F51B5")
)
# 查看数据
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
可视化
# 三色直方图
<- 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
