导语
GUIDE ╲
阿基米德螺线(亦称等速螺线),得名于公元前三世纪希腊数学家阿基米德。阿基米德螺线是一个点匀速离开一个固定点的同时又以固定的角速度绕该固定点转动而产生的轨迹。阿基米德在其著作《螺旋线》中对此作了描述。
背景介绍
对于长轴数据的可视化,使用常规的图形往往不能够完全展示,小编就给大家推荐一个新发布的R包-spiralize,通过阿基米德螺线来对数据进行高分辨率的可视化,能够让我们的图形更加高大上!
R包安装
代码语言:javascript复制install.packages("spiralize")
library(spiralize)
##最新版本
library(devtools)
install_github("jokergoo/spiralize")
可视化展示
01
基本构图
不同的起始值和旋转圈数
代码语言:javascript复制# 左
spiral_initialize(start = 90, end = 360)
spiral_track()##初始化
#右
spiral_initialize(start = 180, end = 360*4 180)
spiral_track()
参数 scale_by 控制线性缩放螺旋上的数据,scale_by = "curve_length" 的极线也被删除。
代码语言:javascript复制# 左
spiral_initialize(scale_by = "angle") # default
spiral_track(height = 0.6)
spiral_axis()
# 右
spiral_initialize(scale_by = "curve_length")
spiral_track(height = 0.6)
spiral_axis()
创建轨道
height控制轨道的高度,值是一个介于 0 和 1 之间的值,是螺旋中两个相邻环之间距离的分数。
代码语言:javascript复制#左
spiral_initialize()
spiral_track(height = 1, background_gp = gpar(col = "black"))##黑色边框
#右
spiral_initialize()
spiral_track(height = 0.5)
多个轨道时,高度总和不能超过1
代码语言:javascript复制spiral_initialize()
spiral_track(height = 0.4, background_gp = gpar(fill = 2))
spiral_track(height = 0.2, background_gp = gpar(fill = 3))
spiral_track(height = 0.1, background_gp = gpar(fill = 4))
02
图形功能
散点图、折线图
代码语言:javascript复制#散点图
spiral_initialize() # by default xlim = c(0, 1)
spiral_track() # by default ylim = c(0, 1)
spiral_points(x = runif(1000), y = runif(1000))
#折线图
x = sort(runif(1000))
y = runif(1000)
spiral_initialize()
spiral_track()
spiral_lines(x, y)
代码语言:javascript复制# the left plot
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h")
# the right plot
spiral_initialize()
spiral_track()
spiral_lines(x, y, type = "h", baseline = 0.5, gp = gpar(col = ifelse(y > 0.5, "red", "blue")))
矩形
代码语言:javascript复制n = 1000
require(circlize)
spiral_initialize(xlim = c(0, n))
spiral_track(height = 0.9)
x1 = runif(n)
col1 = circlize::colorRamp2(c(0, 0.5, 1), c("blue", "white", "red"))
spiral_rect(1:n - 1, 0, 1:n, 0.5, gp = gpar(fill = col1(x1), col = NA))
x2 = runif(n)
col2 = circlize::colorRamp2(c(0, 0.5, 1), c("green", "white", "red"))
spiral_rect(1:n - 1, 0.5, 1:n, 1, gp = gpar(fill = col2(x2), col = NA))
柱状图
代码语言:javascript复制x = seq(1, 1000, by = 1) - 0.5 # middle points of bars
y = runif(1000)
# the left plot
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y)
# the right plot
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y, baseline = 0.5, gp = gpar(fill = ifelse(y > 0.5, 2, 3), col = NA))
代码语言:javascript复制y = matrix(runif(3*1000), ncol = 3)
y = y/rowSums(y)
spiral_initialize(xlim = c(0, 1000))
spiral_track(height = 0.8)
spiral_bars(x, y, gp = gpar(fill = 2:4, col = NA))
Horizon chart
代码语言:javascript复制df = readRDS(system.file("extdata", "ggplot2_downloads.rds", package = "spiralize"))
df = df[df$date < as.Date("2021-01-01"), ]
day_diff = as.double(df$date[nrow(df)] - df$date[1], "days")
year_mean = tapply(df$count, lubridate::year(df$date), function(x) mean(x[x > 0]))
df$diff = log2(df$count/year_mean[as.character(lubridate::year(df$date))])
df$diff[is.infinite(df$diff)] = 0
q = quantile(abs(df$diff), 0.99) # adjust outliers
df$diff[df$diff > q] = q
df$diff[df$diff < -q] = -q
树状图和系统发育树
代码语言:javascript复制library(ape)
data(hivtree.newick)
tree.hiv = read.tree(textConnection(hivtree.newick))
n = length(tree.hiv$tip.label) # number of leaves
spiral_initialize(xlim =c(0, n), start = 360, end = 360*2 180, scale_by = "curve_length")
spiral_track()
spiral_phylo(tree.hiv)
split = dendextend::cutree(phylo_to_dendrogram(tree.hiv), k = 8)
tb = table(split)
for(i in seq_along(tb)) {
spiral_highlight(sum(tb[seq_len(i-1)]), sum(tb[seq_len(i)]), gp = gpar(fill = i))
}
代码语言:javascript复制spiral_initialize(xlim =c(0, n), start = 360, end = 360*2 180, scale_by = "curve_length")
spiral_track()
spiral_phylo(tree.hiv, group = split)
小编总结
通过阿基米德螺线对长轴数据进行可视化,可以解决由于数据量过大造成的可视化难题!如果大家想进行更加细致的了解,可以参考作者的说明文档,作者对如何实现构图进行了非常详细的描述,非常值得大家学习!
https://jokergoo.github.io/spiralize_vignettes/spiralize.html#the-archimedean-spiral