在R中自定义googleVis Sankey图表以添加自定义工具提示

时间:2016-06-12 10:31:26

标签: r group-by dplyr googlevis sankey-diagram

我设法绘制一个sankey图如下,col1包含id

set.seed(1000)
df <- data.frame(col1= sample(c(1:15), 15, replace = F),
col2=sample(c("aa", "bb","cc"), 15, replace=TRUE),
col3=sample(c('a','b','c','d'), 15, replace=TRUE, prob=c(0.25, 0.25, 0.20, 0.30)))

grp<-df %>%
group_by(col2, col3) %>%
summarise(n=n())
View(grp)

enter image description here

Sankeyy <- gvisSankey(grp, from="col2", to="col3", weight="n",
                  options=list(
                    sankey="{link: {color: { fill: '#d799ae' } },
                    node: { color: { fill: '#a61d4c' },
                    label: { color: '#871b47' } }}"))
plot(Sankeyy)

enter image description here

以下内容有助于跟踪属于每个组的ID

grp<-df %>%
group_by(col2, col3) %>%
summarise(n=n(), rows=paste(sort(col1), collapse = ", "))
View(grp)

enter image description here

Sankeyy <- gvisSankey(grp, from="col2", to="col3", weight="n",
                  options=list(
                    sankey="{link: {color: { fill: '#d799ae' } },
                    node: { color: { fill: '#a61d4c' },
                    label: { color: '#871b47' } }}"))
plot(Sankeyy)

但我收到以下错误。

enter image description here

在sankey图工具提示中包含属于每个组的ID的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

为了使sankey图表工具提示有效,您作为工具提示使用的列必须命名为 something.tooltip 。数据也必须是&#34;字符&#34; (虽然你没有遇到这个问题。)

如果我更改您的列名称以遵循 .tooltip 格式,一切正常。

grp<-df %>%
group_by(col2, col3) %>%
summarise(n=n(), rows.tooltip=paste(sort(col1), collapse = ", "))

Sankey chart with custom tooltip

附注:对于其他gvis图表类型,工具提示列的名称似乎必须包含数据列的名称,该列用作以下工具提示:https://cran.r-project.org/web/packages/googleVis/vignettes/Using_Roles_via_googleVis.html

我还没有玩过这个,所以我无法对具体内容发表评论。然而,对于sankey排行榜,以上就是你所需要的。

相关问题