如何在R中绘制堆叠排序的功能区图

时间:2019-04-19 01:40:23

标签: r ggplot2

我想创建如下所示的堆叠排序的功能区图。

Ribbon Plot

import re
re.split('[ ,]+', 'add $s2, $s0, $s1')

我通过excel制作了这张图,但想在r-studio中进行。

1 个答案:

答案 0 :(得分:0)

这是使用geom_ribbon的一种方法,您可以操纵该方法来获得所需的结果。它需要对数据进行一些重新格式化:

df2 <- data.frame(x=c(2000,2001,2002,2003,2004,2000,2001,2002,2003,2004,2000,2001,2002,2003,2004,2000,2001,2002,2003,2004), y=c(0.16,0.10,0.05,0,0,0.17,0.16,0.14,0.13,0.12,0,0.01,0.04,0.08,0.12,0.36,0.32,0.26,0.19,0.09), group=c("orange","orange","orange","orange","orange","lightblue","lightblue","lightblue","lightblue","lightblue","blue","blue","blue","blue","blue","red","red","red","red","red"))

> df2
      x    y     group
1  2000 0.16    orange
2  2001 0.10    orange
3  2002 0.05    orange
4  2003 0.00    orange
5  2004 0.00    orange
6  2000 0.17 lightblue
7  2001 0.16 lightblue
8  2002 0.14 lightblue
9  2003 0.13 lightblue
10 2004 0.12 lightblue
11 2000 0.00      blue
12 2001 0.01      blue
13 2002 0.04      blue
14 2003 0.08      blue
15 2004 0.12      blue
16 2000 0.36       red
17 2001 0.32       red
18 2002 0.26       red
19 2003 0.19       red
20 2004 0.09       red

现在您可以像这样绘图。更改常数(20)以更改色带的宽度。

ggplot(df2, aes(x=x, y=y, group=group, colour=group)) + 
  geom_ribbon(aes(ymin=v-20*y, ymax=v+20*y, fill=group), alpha=0.2) +
  geom_line()

enter image description here

[在提问者添加数据之后,很明显,下面的解决方案不适合该数据类型的图表,但是如果有人在寻找sankey图,则将答案留在这里。]

尽管有一些更漂亮的选项(例如networkD3),但最简单的制作功能区图的方法是使用sankey软件包。这是https://cran.r-project.org/web/packages/sankey/sankey.pdf中的示例:

edges <- read.table(stringsAsFactors = FALSE, textConnection(
" get_deps get_description
get_deps parse_deps
get_deps %||%
get_deps drop_internal
get_description pkg_from_filename
parse_deps str_trim
cran_file get_pkg_type
cran_file r_minor_version
download_urls split_pkg_names_versions
download_urls cran_file
pkg_download dir_exists
pkg_download download_urls
pkg_download filename_from_url
pkg_download try_download
restore pkg_download
restore drop_missing_deps
restore install_order
restore get_deps
split_pkg_names_versions data_frame
"))

数据结构如下:

> edges
                         V1                       V2
1                  get_deps          get_description
2                  get_deps               parse_deps
3                  get_deps                     %||%
4                  get_deps            drop_internal
5           get_description        pkg_from_filename
6                parse_deps                 str_trim
7                 cran_file             get_pkg_type
8                 cran_file          r_minor_version
9             download_urls split_pkg_names_versions
10            download_urls                cran_file
11             pkg_download               dir_exists
12             pkg_download            download_urls
13             pkg_download        filename_from_url
14             pkg_download             try_download
15                  restore             pkg_download
16                  restore        drop_missing_deps
17                  restore            install_order
18                  restore                 get_deps
19 split_pkg_names_versions               data_frame

此代码生成图:

pkgsnap_sankey <- make_sankey(edges = edges)
sankey(pkgsnap_sankey)

enter image description here