【问题标题】:Visualize in R flow from one set of objects to another在 R 中可视化从一组对象到另一组对象的流程
【发布时间】:2013-11-02 02:07:47
【问题描述】:

我对 NIH 如何审查拨款感兴趣。拨款审查程序的运作方式是国会将资金分配给各个机构(例如,国家癌症研究所或 NCI),并将个别拨款提交给这些机构。这些机构围绕各种资金优先事项(例如癌症、传染病等)组织起来。

但是,在审核拨款时,通常(但不总是)将其发送到各个研究部门,这些部门更多地围绕科学学科进行组织。因此,如果研究人员向 NHLBI 提交用于研究白血病的资助,“肿瘤进展”研究部分会发现自己正在审查国家癌症研究所和国家心肺血液研究所 (NHLBI) 的资助。

我在 R 中有一个如下所示的数据框:

grant_id <- 1:100
funding_agency <- sample(rep(c("NIAID", "NIGMS", "NHLBI", "NCI", "NINDS"), 20))
study_section <- sample(rep(c("Tumor Cell Biology", "Tumor Progression", 
                              "Vector Biology", "Molecular Genetics", 
                              "Medical Imaging", "Macromolecular Structure",
                              "Infectious Diseases", "Drug Discovery", 
                              "Cognitive Neuroscience", "Aging and Geriatrics"), 
                            10)
                        )
total_cost <- rnorm(100, mean = 30000, sd = 10000)
d <- data.frame(grant_id, funding_agency, study_section, total_cost)

some(d)

   grant_id funding_agency          study_section total_cost
15       15          NINDS         Vector Biology   25242.19
19       19            NCI    Infectious Diseases   29075.21
50       50            NCI         Drug Discovery   25176.35
62       62            NCI      Tumor Progression   14264.34
64       64          NIAID     Tumor Cell Biology   30024.13

我想为这些数据创建两个可视化,希望使用 R;一个显示提交给各个研究所的赠款如何分配给研究部门,第二个显示研究所分配给研究部门的赠款金额。我最终想要的是您在以下网站上看到的图表:

Migration flow

College major to job pipelines

有人知道 R 包和/或有一些示例代码来创建您在上面的网站上找到的图表吗?或者,我应该考虑使用不同的可视化来实现相同的目标吗?

【问题讨论】:

标签: r visualization data-visualization


【解决方案1】:

这里是使用rCharts 的方法。可以查看最终的SankeyPlot here

d <- data.frame(
  id = grant_id, 
  source = funding_agency, 
  target = study_section, 
  value = total_cost
)
# devtools::install_github("rCharts", "ramnathv", ref = "dev")
require(rCharts)
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = d,
  nodeWidth = 15,
  nodePadding = 10,
  layout = 32,
  width = 750,
  height = 500,
  labelFormat = ".1%"
)
sankeyPlot

要保存图表,你可以这样做

sankeyPlot$save('mysankey.html')

【讨论】:

  • 这太好了,谢谢@Ramnath!我有一个可能很愚蠢的问题——我尝试运行您的代码,但在运行sankeyPlot 后无法找到文件的保存位置。你能告诉我结果图表保存在哪里吗?它似乎不在我的工作目录中。
  • 生成的图表在一个临时文件中。保存图表运行sankeyPlot$save("mysankey.html")
  • 完美。感谢您的及时回复!
  • 你能解释一下这行 sankeyPlot$setLib('timelyportfolio.github.io/rCharts_d3_sankey')
  • rCharts 需要用于确认文件夹结构的可视化库。要使用库,您需要指定其路径,这是使用 setLib 方法完成的。在这种情况下,您从其在线位置使用该库。您也可以下载 repo 并将 setLib 指向本地路径。
【解决方案2】:

对可视化部分帮助不大,但您正在寻找数据的二维表。

使用包 reshape2 并忽略 grant_id

d1 <- melt(d[,2:4])
d2 <- dcast(d1, study_section~funding_agency,sum)
> d2
              study_section      NCI     NHLBI     NIAID     NIGMS     NINDS
1      Aging and Geriatrics 28598.04  76524.55      0.00 109492.59 138330.12
2    Cognitive Neuroscience 76484.18  88217.42  78126.55  71546.62  73132.14
3            Drug Discovery 43667.30  39683.03  23797.24  46363.75 105655.61
4       Infectious Diseases 65375.44 136462.03  96413.08  34653.48  13835.22
5  Macromolecular Structure 84308.64  42290.61  39886.87  61645.00  67550.41
6           Medical Imaging 26264.32  86736.36 106356.13  41001.21  35549.83
7        Molecular Genetics 49473.72      0.00 110201.52  69468.03  86688.24
8        Tumor Cell Biology 99930.88  50862.39  95394.23  26269.98  46944.60
9         Tumor Progression 58719.89  52669.80  86874.89      0.00 119264.59
10           Vector Biology 64251.66  30880.81  66734.26 125524.72      0.00

这会告诉您哪个 study_section 从哪个资助机构获得了多少资助。现在如何显示这是一个不同的问题。也许看看http://statmath.wu.ac.at/projects/vcd/

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    相关资源
    最近更新 更多