如何生成类似于Google Analytics访客流的图表/图示?

41

我正在尝试生成类似于最近谷歌分析“访问者流”提供的图表,这些图表也被称为Alluvial diagrams

我可以使用基于网络或非网络的解决方案,只要我可以自己运行它。

我想要可视化的数据如下:

  • 在时间t1,我有x1个单位,分成n1份
  • 在时间t2,n1个部分分裂(或合并)成n2个部分,具有x2个单位
    • 我想要显示拆分/合并发生的位置。

我的数据目前在NetworkX中表示为DiGraph,但这可能是无关紧要的,因为我可以以任何所需的格式输出我的数据。

类似于下面的图表

2个回答

60
我认为这是一个有趣的问题,所以我使用d3制作了一个示例性的涌流图:http://nickrabinowitz.com/projects/d3/alluvial/alluvial.html
此外,因为d3非常擅长动画,我认为做一个动画版本也会很酷。这是它的链接:http://nickrabinowitz.com/projects/d3/alluvial/alluvial-dynamic.html
虽然这并不包含你可能想要的所有内容,但希望它能提供一些基础。开头的大块代码只是用来制造虚假数据的 - 你可以用真实数据替换它,或使用d3.json加载它。期望的格式类似于d3用于网络图的DOM节点结构:
{
    // list of time slots t1 through tn
    times: [
        // list of t1 nodes
        [
            {
                nodeName: "Node 1",
                id: 1,
                nodeValue: 24332
            },
            // etc ...
        ],
        // etc ...
    ],
    // list of all links
    links: [
        {
            source: 1, // id of source node
            target: 5, // id of target node
            value: 3243
        },
        // ... etc
    ]
}

我希望这有所帮助 - 这不是一个典型的SO回答,可能需要一定的工作量来自定义,但我认为它可能会有用。


太棒了!我现在就来试试。 - pocketfullofcheese
那个最后的链接是一个非常流畅的集成,奶酪先生。 - NetConstructor.com
显然,上一个评论中的链接已经失效了,但我在这里找到了它:http://images.theage.com.au/file/2012/07/02/3421190/marc2B%25202/marc2B/parsets.html - pocketfullofcheese
@nrabinowitz非常感谢您的帮助。我已经成功基于此创建了一个漂亮的可视化效果。这段代码有任何许可证吗? - braunmagrin

-1
考虑在R中使用平行坐标图

![赛马的平行坐标图][1]

df <- structure(list(Horse = structure(c(11L, 16L, 13L, 15L, 3L, 18L, 10L, 17L, 19L, 8L, 5L, 9L, 1L, 4L, 12L, 2L, 14L, 7L, 6L), 
.Label = c("Advice", "Atomic Rain", "Chocolate Candy", "Desert Party", "Dunkirk", "Flying Private"
, "Friesan Fire", "General Quarters", "Hold Me Back", "Join in the Dance", "Mine That Bird", "Mr. Hot Stuff", "Musket Man"
, "Nowhere to Hide", "Papa Clem", "Pioneer of the Nile", "Regal Ransom", "Summer Bird", "West Side Bernie")
, class = "factor")
, X1.4 = c(19L, 3L, 8L, 5L, 17L, 16L, 1L, 2L, 13L, 12L, 9L, 14L, 15L, 4L, 18L, 10L, 11L, 6L, 7L)
, X1.2 = c(19L, 3L, 8L, 4L, 12L, 16L, 1L, 2L, 17L, 13L, 10L, 5L, 15L, 6L, 18L, 9L, 14L, 7L, 11L)
, X3.4 = c(19L, 4L, 7L, 3L, 15L, 16L, 1L, 2L, 14L, 11L, 9L, 6L, 17L, 5L, 18L, 10L, 12L, 8L, 13L)
, X1m = c(12L, 2L, 7L, 4L, 8L, 15L, 1L, 3L, 17L, 10L, 11L, 5L, 13L, 6L, 16L, 9L, 18L, 14L, 19L)
, Str = c(1L, 2L, 4L, 3L, 7L, 9L, 5L, 6L, 13L, 10L, 12L, 8L, 14L, 11L, 16L, 15L, 18L, 17L, 19L)
, Finish = 1:19), .Names = c("Horse", "X1.4", "X1.2", "X3.4", "X1m", "Str", "Finish")
, class = "data.frame", row.names = c(NA, -19L))

library(ggplot2)

df$Horse <- with(df, reorder(Horse, Finish))
dfm <- melt(df)

#add a volume metric
dfm$volume <- ifelse(dfm$variable == "X1.4" & dfm$value <= 6,6,
                ifelse(dfm$variable == "X1.4" & dfm$value > 6 & dfm$value <= 12,6,
            ifelse(dfm$variable == "X1.4" & dfm$value > 12,7,1)))
dfm$volume <- ifelse(dfm$variable == "X1.2" & dfm$value <= 9,9,
                ifelse(dfm$variable == "X1.2" & dfm$value > 9 & dfm$value<= 14,5,
            ifelse(dfm$variable == "X1.2" & dfm$value > 14,5,dfm$volume)))
dfm$volume <- ifelse(dfm$variable == "X3.4" & dfm$value <= 3,3,
                  ifelse(dfm$variable == "X3.4" & dfm$value > 3 & dfm$value <= 19,1,dfm$volume))

#Alter the race for some neck to neck action
dfm$value <- ifelse(dfm$variable == "X1.4" & dfm$value <= 6,4,
              ifelse(dfm$variable == "X1.4" & dfm$value > 6 & dfm$value <= 12,8,dfm$value))
dfm$value <- ifelse(dfm$variable == "X1.2" & dfm$value <= 9,5,
              ifelse(dfm$variable == "X1.2" & dfm$value > 9 & dfm$value <= 14,11,dfm$value))
dfm$value <- ifelse(dfm$variable == "X3.4" & dfm$value <= 3,2,
              ifelse(dfm$variable == "X3.4" & dfm$value > 3 & dfm$value <= 19,11,dfm$value))


p <- ggplot(dfm, aes(factor(variable), value, group = Horse, colour = Horse, label = Horse))
p1 <- p + geom_line(aes(size = volume), labels = labels) + geom_text(data = subset(dfm,variable == "Finish"), 
aes(x = factor(variable + 0.5)), size = 3.5, hjust = 0.8)

labels <- c(expression(1/4), expression(1/2),expression(3/4), "1m", "Str", "Finish","")

p1 + theme_bw() + opts(legend.position = "none",
     panel.border = theme_blank(), axis.ticks = theme_blank()) +
     scale_x_discrete(breaks = c(levels(dfm$variable),
         ""), labels = labels) + scale_y_continuous(breaks = NA,
     trans = "reverse") + xlab(NULL) + ylab(NULL)


# Credit and other notes:
# http://learnr.wordpress.com/2009/05/06/ggplot2-bump-chart/
# ![enter image description here][1]http://had.co.nz/ggplot/plot-templates.html Parallel coordinates plot

在这个例子中,有一些已弃用的函数(opts、theme_blank等)和其他错误。 - geotheory

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接