使用 GoogleVis 的 R 中的桑基图
Sankey Diagram in R using GoogleVis
这是我的虚拟数据:
df <- data.frame(First_Area=c('group1', 'group1', 'group1', 'group2', 'group2', 'group2', 'group2', 'group3', 'group3', 'group3', 'group3', 'group3', 'group4', 'group4', 'group4', 'group4', 'group5', 'group5', 'group5', 'group5', 'group6', 'group6', 'group6', 'group7', 'group7', 'group8', 'group8', 'group8', 'group8', 'group8', 'group9', 'group9', 'group9', 'group10', 'group10', 'group10', 'group11', 'group11', 'group11'), record_count=c(25000, 25000, 25000, 5555, 5555, 5555, 8335, 5556, 5556, 5556, 5556, 2776, 5555, 5555, 5555, 5555, 2500, 2500, 500, 55, 1851, 1851, 1853, 5000, 555, 1100, 500, 1500, 1500, 956, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852), Second_Area=c('group2', 'group3', 'group12', 'group13', 'group5', 'group6', 'group7', 'group8', 'group9', 'group10', 'group11', 'group14', 'group15', 'group16', 'group17', 'group18', 'group19', 'group20', 'group21', 'group22', 'group23', 'group24', 'group25', 'group26', 'group27', 'group28', 'group29', 'group30', 'group31', 'group32', 'group33', 'group34', 'group35', 'group36', 'group37', 'group38', 'group39', 'group40', 'group41'))
我尝试使用 Googlevis 使用以下代码制作桑基图:
plot(
gvisSankey(df, from="First_Area",
to="Second_Area", weight="record_count",
options=list(
height=1250,
sankey="{link:{color:{fill:'lightblue'}}}"
))
)
而且我一直收到这个错误
数据表中的格式无效:第 1 列的类型必须为 'string'
我试过将第一个和第二个区域转换为一个因素和一个字符,但我仍然得到同样的错误。有人可以帮忙吗?
难以置信,但您需要更改数据框中列的顺序。
数据
df <- data.frame(First_Area=c('group1', 'group1', 'group1', 'group2', 'group2', 'group2', 'group2', 'group3', 'group3', 'group3', 'group3', 'group3', 'group4', 'group4', 'group4', 'group4', 'group5', 'group5', 'group5', 'group5', 'group6', 'group6', 'group6', 'group7', 'group7', 'group8', 'group8', 'group8', 'group8', 'group8', 'group9', 'group9', 'group9', 'group10', 'group10', 'group10', 'group11', 'group11', 'group11'), Second_Area=c('group2', 'group3', 'group12', 'group13', 'group5', 'group6', 'group7', 'group8', 'group9', 'group10', 'group11', 'group14', 'group15', 'group16', 'group17', 'group18', 'group19', 'group20', 'group21', 'group22', 'group23', 'group24', 'group25', 'group26', 'group27', 'group28', 'group29', 'group30', 'group31', 'group32', 'group33', 'group34', 'group35', 'group36', 'group37', 'group38', 'group39', 'group40', 'group41'), record_count=c(25000, 25000, 25000, 5555, 5555, 5555, 8335, 5556, 5556, 5556, 5556, 2776, 5555, 5555, 5555, 5555, 2500, 2500, 500, 55, 1851, 1851, 1853, 5000, 555, 1100, 500, 1500, 1500, 956, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852))
代码
plot(
googleVis::gvisSankey(df, from="First_Area",
to="Second_Area", weight="record_count",
options=list(
height=1250,
sankey="{link:{color:{fill:'lightblue'}}}"
))
)
结果
这是我的虚拟数据:
df <- data.frame(First_Area=c('group1', 'group1', 'group1', 'group2', 'group2', 'group2', 'group2', 'group3', 'group3', 'group3', 'group3', 'group3', 'group4', 'group4', 'group4', 'group4', 'group5', 'group5', 'group5', 'group5', 'group6', 'group6', 'group6', 'group7', 'group7', 'group8', 'group8', 'group8', 'group8', 'group8', 'group9', 'group9', 'group9', 'group10', 'group10', 'group10', 'group11', 'group11', 'group11'), record_count=c(25000, 25000, 25000, 5555, 5555, 5555, 8335, 5556, 5556, 5556, 5556, 2776, 5555, 5555, 5555, 5555, 2500, 2500, 500, 55, 1851, 1851, 1853, 5000, 555, 1100, 500, 1500, 1500, 956, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852), Second_Area=c('group2', 'group3', 'group12', 'group13', 'group5', 'group6', 'group7', 'group8', 'group9', 'group10', 'group11', 'group14', 'group15', 'group16', 'group17', 'group18', 'group19', 'group20', 'group21', 'group22', 'group23', 'group24', 'group25', 'group26', 'group27', 'group28', 'group29', 'group30', 'group31', 'group32', 'group33', 'group34', 'group35', 'group36', 'group37', 'group38', 'group39', 'group40', 'group41'))
我尝试使用 Googlevis 使用以下代码制作桑基图:
plot(
gvisSankey(df, from="First_Area",
to="Second_Area", weight="record_count",
options=list(
height=1250,
sankey="{link:{color:{fill:'lightblue'}}}"
))
)
而且我一直收到这个错误 数据表中的格式无效:第 1 列的类型必须为 'string'
我试过将第一个和第二个区域转换为一个因素和一个字符,但我仍然得到同样的错误。有人可以帮忙吗?
难以置信,但您需要更改数据框中列的顺序。
数据
df <- data.frame(First_Area=c('group1', 'group1', 'group1', 'group2', 'group2', 'group2', 'group2', 'group3', 'group3', 'group3', 'group3', 'group3', 'group4', 'group4', 'group4', 'group4', 'group5', 'group5', 'group5', 'group5', 'group6', 'group6', 'group6', 'group7', 'group7', 'group8', 'group8', 'group8', 'group8', 'group8', 'group9', 'group9', 'group9', 'group10', 'group10', 'group10', 'group11', 'group11', 'group11'), Second_Area=c('group2', 'group3', 'group12', 'group13', 'group5', 'group6', 'group7', 'group8', 'group9', 'group10', 'group11', 'group14', 'group15', 'group16', 'group17', 'group18', 'group19', 'group20', 'group21', 'group22', 'group23', 'group24', 'group25', 'group26', 'group27', 'group28', 'group29', 'group30', 'group31', 'group32', 'group33', 'group34', 'group35', 'group36', 'group37', 'group38', 'group39', 'group40', 'group41'), record_count=c(25000, 25000, 25000, 5555, 5555, 5555, 8335, 5556, 5556, 5556, 5556, 2776, 5555, 5555, 5555, 5555, 2500, 2500, 500, 55, 1851, 1851, 1853, 5000, 555, 1100, 500, 1500, 1500, 956, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852, 1852))
代码
plot(
googleVis::gvisSankey(df, from="First_Area",
to="Second_Area", weight="record_count",
options=list(
height=1250,
sankey="{link:{color:{fill:'lightblue'}}}"
))
)