R绘图分类更改 - sankey图

时间:2015-02-27 19:13:24

标签: r categories sankey-diagram

有人知道如何在R中绘制两个分类变量的变化吗?

例如在这个例子中:

http://www.sueddeutsche.de/politik/waehlerwanderung-in-hamburg-wer-wem-die-stimmen-stahl-1.2353382

(没有动态)

所以我有两个分类变量,如:Pre和Post

Pre = sample(1:100,10000,replace = TRUE)

Post = sample(1:100,10000,replace = TRUE)

感谢您的任何建议!

2 个答案:

答案 0 :(得分:1)

这是所谓的sankey图的一个例子:

set.seed(1)
(edgelist <- transform(subset(expand.grid(source = c("spd", "linke", "cdu"), target = c("spd ", "linke ", "cdu "), stringsAsFactors = FALSE), target != paste0(source, " ")), value = sample(1:200, nrow(edgelist))))
#   source target value
# 2  linke   spd     54
# 3    cdu   spd     75
# 4    spd linke    114
# 6    cdu linke    179
# 7    spd   cdu     40
# 8  linke   cdu    176

require(rCharts)
sankeyPlot <- rCharts$new()
sankeyPlot$setTemplate(script = "layouts/chart.html")
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey') 
sankeyPlot$set(data = edgelist, nodeWidth = 55, nodePadding = 50, width = 700, height = 400)
sankeyPlot

sankey

答案 1 :(得分:0)

对于一个非常简单,没有废话的情节,你可以使用segments()函数。

plot(NULL, xlim=c(1,2), ylim=c(0,100))

segments(x0, Pre, x1, Post)