流动图

作者

[编辑] 郑虎;

[审核] .

注记

Hiplot 网站

本页面为 Hiplot Streamgraph 插件的源码版本教程,您也可以使用 Hiplot 网站实现无代码绘图,更多信息请查看以下链接:

https://hiplot.cn/basic/streamgraph?lang=zh_cn

环境配置

  • 系统: Cross-platform (Linux/MacOS/Windows)

  • 编程语言: R

  • 依赖包: streamgraph

# 安装包
if (!requireNamespace("streamgraph", quietly = TRUE)) {
  install_github("hrbrmstr/streamgraph")
}

# 加载包
library(streamgraph)

数据准备

# 加载数据
data <- read.delim("files/Hiplot/168-streamgraph-data.txt", header = T)

# 整理数据格式
colnames(data) <- c("date","key","value")

# 查看数据
head(data)
  date key  value
1 1990   a 0.7688
2 1990   b 0.5782
3 1990   c 0.8164
4 1990   d 0.4791
5 1990   e 0.8793
6 1990   f 0.4640

可视化

# 流动图
p <- streamgraph(data, key = "key", value = "value", date = "date",
                 offset = "silhouette", interpolate = "cardinal",
                 interactive = F, scale = "date") %>% 
  sg_fill_brewer(palette = "Spectral")

p
图 1: 流动图