如何将二分图转换为带有彩色单元的网络矩阵?

问题描述 投票:0回答:1

我有一个二分图,但我想将其转换为单元格矩阵,如下图所示,这是我从论文中找到的。我发现它更容易阅读和可视化,尤其是对于更大的数据集。我使用二分 R 包来创建二分图,但现在确定如何根据交互值制作带有彩色单元格的单元格矩阵(我想自己设置比例)。

这是图片,来自这篇paper

enter image description here

如果需要,这里是我的数据集较小版本的代码和 dput:

代码:

plotweb(data.mat,
        method="normal",
        text.rot = 90,
        labsize = 1,
        y.width.low = 0.05,
        col.high = "#66A61E",
        col.interaction = "#E6AB02",
        col.low = "#E7298A",
        bor.col.interaction =  NA,
        arrow="none",
        empty=TRUE,
        plot.axes=FALSE,
        ybig=1)

数据集:

> dput(data.mat)
structure(c(7336L, 1640L, 26293L, 111533L, 71255L, 161705L, 104046L, 
169975L, 395822L, 153999L, 6780L, 10866L, 131162L, 239110L, 130786L, 
88922L, 46725L, 99169L, 109625L, 203947L, 2258L, 550L, 47109L, 
1813L, 362753L, 14451L, 17025L, 30251L, 308L, 34412L, 11L, 0L, 
56988L, 1360L, 4L, 5L, 107L, 31L, 56L, 868L, 31775L, 31L, 327L, 
19L, 180L, 4284L, 77L, 27L, 0L, 854L, 5L, 143L, 4622L, 264L, 
7945L, 51L, 14545L, 32L, 8L, 126L, 66L, 0L, 6L, 14130L, 5L, 9L, 
165L, 1017L, 5L, 115L, 65L, 5L, 9572L, 77L, 820L, 165L, 521L, 
788L, 17L, 538L), dim = c(10L, 8L), dimnames = list(c("Arctium lappa", 
"Cichorium intybus", "Circium arvense", "Circium vulgare", "Hypericum perforatum", 
"Leucanthemum vulgare", "Plantago lanceolata", "Trifolium pratense", 
"Trifolium repens", "Tripleurospermum inodorum"), c("Cladosporium", 
"Alternaria", "Aureobasidium", "X....1", "Golovinomyces", "Podosphaera", 
"Ampelomyces", "X....2")))

我想将其转换为矩阵图,并且如果可能的话,将颜色单元从浅到深设置我自己的范围。

r bipartite
1个回答
0
投票

如果我们稍微调整数据的形状,使每个正方形一行,我们可以使用

geom_tile()
来绘制图形。例如

library(ggplot2)
as.data.frame(data.mat) |>
  tibble::rownames_to_column("id1") |>
  tidyr::pivot_longer(-id1, names_to="id2") |>
  ggplot() + 
    aes(id2, id1, fill=value) + 
    geom_tile() + 
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

你最终会得到 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.