gpt4 book ai didi

r - 了解 sunburstR 行为

转载 作者:行者123 更新时间:2023-12-04 18:03:33 25 4
gpt4 key购买 nike

我有一个 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 步)。

现在我想将数据可视化为 sunburst使用 sunburstR 绘图包裹。我这样做:
# devtools::install_github("timelyportfolio/sunburstR")
library(sunburstR)
sunburst(dd)

不幸的是,这不会产生任何输出,我不明白为什么。再举一个例子,这按预期工作:
sunburst(tail(dd, 8))

但这不是:
sunburst(tail(dd, 9))

我也注意到
sunburst(dd[c(5, 1:4),])

产生一个情节,但令人惊讶的是, dt category 被分成两个块,通常应该在第一(最内层)级别上显示为单个块。

问:有人可以向我解释为什么会发生这种情况(有些方法有效,有些方法无效,有些方法有效但显示数据有些不正确)以及我需要做什么来可视化整个数据集(不仅仅是样本数据)?

样本数据
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-rds-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")))

enter image description here

关于r - 了解 sunburstR 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37520920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com