了解 sunburstR 行为
Understanding sunburstR behaviour
我有一个 data.frame
看起来类似于此示例:
> head(dd)
# paths counts
#1 s 4735
#2 dt 4635
#3 so 2191
#4 sb 1949
#5 dt-dt 1310
#6 s-s 978
路径中的不同步骤由 -
分隔。如您所见,有些路径长度为 1,有些路径 > 1 步(示例中最多 5 步)。
现在我想使用 sunburstR
包将数据可视化为 sunburst
图。我这样做:
# devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
sunburst(dd)
不幸的是,这没有产生任何输出,我不明白为什么。再举一个例子,这按预期工作:
sunburst(tail(dd, 8))
但这不是:
sunburst(tail(dd, 9))
我也注意到
sunburst(dd[c(5, 1:4),])
生成一个图,但令人惊讶的是,dt
类别被分成两个块,通常应该在第一层(最内层)显示为一个块。
问:有人可以向我解释为什么会发生这种情况(有些方法有效,有些无效,有些有效但显示数据有些不正确)以及我需要做什么如何可视化整个数据集(不仅仅是样本数据)?
示例数据
dd <- structure(list(paths = c("s", "dt", "so", "sb", "dt-dt", "s-s",
"so-dt", "dt-dt-dt", "sb-sb", "so-so", "s-s-s", "s-rd", "dt-dt-dt-dt",
"s-sb", "a", "so-dt-dt", "s-rd-rd", "r", "dt-s", "so-sb", "dt-sb",
"s-rd-rd-rd", "dt-rd", "dt-dt-dt-dt-dt", "so-dt-dt-dt"), counts = c(4735L,
4635L, 2191L, 1949L, 1310L, 978L, 558L, 455L, 324L, 281L, 266L,
231L, 208L, 200L, 200L, 196L, 156L, 150L, 142L, 129L, 123L, 114L,
113L, 113L, 100L)), .Names = c("paths", "counts"), class = "data.frame", row.names = c(NA, -25L))
dd
包含其他序列的子序列:
tail(dd, 9)
# paths counts
# 17 s-rd-rd 156 # <-----
# 18 r 150
# 19 dt-s 142
# 20 so-sb 129
# 21 dt-sb 123
# 22 s-rd-rd-rd 114 # <-----
# 23 dt-rd 113
# 24 dt-dt-dt-dt-dt 113
# 25 so-dt-dt-dt 100
例如s-rd-rd
是 s-rd-rd-rd
的一部分。 sunburst
似乎对此感到窒息。
在 package author's example 你会注意到一个额外的
-end
以防止此类情况发生。 tips here中也提到了这一点:
each line should be a complete path from root to leaf - don't include
counts for intermediate steps. For example, include "home-search-end"
and "home-search-product-end" but not "home-search" - the latter is
computed by the partition layout, by adding up the counts of all the
sequences with that prefix.
这似乎也能解决这个问题:
transform(tail(dd, 9), paths=paste0(paths, "-end"))
# paths counts
# 17 s-rd-rd-end 156
# 18 r-end 150
# 19 dt-s-end 142
# 20 so-sb-end 129
# 21 dt-sb-end 123
# 22 s-rd-rd-rd-end 114
# 23 dt-rd-end 113
# 24 dt-dt-dt-dt-dt-end 113
# 25 so-dt-dt-dt-end 100
sunburst(transform(tail(dd, 9), paths=paste0(paths, "-end")))
我有一个 data.frame
看起来类似于此示例:
> head(dd)
# paths counts
#1 s 4735
#2 dt 4635
#3 so 2191
#4 sb 1949
#5 dt-dt 1310
#6 s-s 978
路径中的不同步骤由 -
分隔。如您所见,有些路径长度为 1,有些路径 > 1 步(示例中最多 5 步)。
现在我想使用 sunburstR
包将数据可视化为 sunburst
图。我这样做:
# devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
sunburst(dd)
不幸的是,这没有产生任何输出,我不明白为什么。再举一个例子,这按预期工作:
sunburst(tail(dd, 8))
但这不是:
sunburst(tail(dd, 9))
我也注意到
sunburst(dd[c(5, 1:4),])
生成一个图,但令人惊讶的是,dt
类别被分成两个块,通常应该在第一层(最内层)显示为一个块。
问:有人可以向我解释为什么会发生这种情况(有些方法有效,有些无效,有些有效但显示数据有些不正确)以及我需要做什么如何可视化整个数据集(不仅仅是样本数据)?
示例数据
dd <- structure(list(paths = c("s", "dt", "so", "sb", "dt-dt", "s-s",
"so-dt", "dt-dt-dt", "sb-sb", "so-so", "s-s-s", "s-rd", "dt-dt-dt-dt",
"s-sb", "a", "so-dt-dt", "s-rd-rd", "r", "dt-s", "so-sb", "dt-sb",
"s-rd-rd-rd", "dt-rd", "dt-dt-dt-dt-dt", "so-dt-dt-dt"), counts = c(4735L,
4635L, 2191L, 1949L, 1310L, 978L, 558L, 455L, 324L, 281L, 266L,
231L, 208L, 200L, 200L, 196L, 156L, 150L, 142L, 129L, 123L, 114L,
113L, 113L, 100L)), .Names = c("paths", "counts"), class = "data.frame", row.names = c(NA, -25L))
dd
包含其他序列的子序列:
tail(dd, 9)
# paths counts
# 17 s-rd-rd 156 # <-----
# 18 r 150
# 19 dt-s 142
# 20 so-sb 129
# 21 dt-sb 123
# 22 s-rd-rd-rd 114 # <-----
# 23 dt-rd 113
# 24 dt-dt-dt-dt-dt 113
# 25 so-dt-dt-dt 100
例如s-rd-rd
是 s-rd-rd-rd
的一部分。 sunburst
似乎对此感到窒息。
在 package author's example 你会注意到一个额外的
-end
以防止此类情况发生。 tips here中也提到了这一点:
each line should be a complete path from root to leaf - don't include counts for intermediate steps. For example, include "home-search-end" and "home-search-product-end" but not "home-search" - the latter is computed by the partition layout, by adding up the counts of all the sequences with that prefix.
这似乎也能解决这个问题:
transform(tail(dd, 9), paths=paste0(paths, "-end"))
# paths counts
# 17 s-rd-rd-end 156
# 18 r-end 150
# 19 dt-s-end 142
# 20 so-sb-end 129
# 21 dt-sb-end 123
# 22 s-rd-rd-rd-end 114
# 23 dt-rd-end 113
# 24 dt-dt-dt-dt-dt-end 113
# 25 so-dt-dt-dt-end 100
sunburst(transform(tail(dd, 9), paths=paste0(paths, "-end")))