R中列联表的维恩图

icnyk63a  于 2023-01-15  发布在  其他
关注(0)|答案(1)|浏览(120)

我有一个列联表这样的数据,显示了大量的数据,但是我想从这个数据弗拉姆中画出维恩图。
我的数据结构:

species_abundance<-data.frame(Genus = c("Parasphingorhabdus", "Loktanella", "Cytobacillus", "Paracoccus", "Paucisalibacillus", "Kytococcus", "Salinibacterium", "Acinetobacter baumanni","Marinococcus","Bacillus"),
               S3 = c(0, 0, 1, 1, 0, 0, 1,0,4,0),
               S5 = c(0, 0, 0, 1, 1, 0, 1,0,3,5),
               S7 = c(3, 1, 0, 2, 0, 1, 0,0,3,1),
               S9 = c(0, 1, 0, 3, 0, 0, 0,1,2,0)

如何从该数据框架中绘制维恩图,以查找不同站点(S3、S5、S7......)的独特和共享物种?
如果我转换如下所示的数据,我尝试使用Venny2我会得到这样的图像,类似的图像和发现我想用R,请帮助

species_abundance1<-data.frame(S3 = c("", "", "Cytobacillus", "Paracoccus", "", "", "Salinibacterium","", "Marinococcus", ""),
                          S5 = c("", "", "", "Paracoccus", "Paucisalibacillus", "", "Salinibacterium","", "Marinococcus","Bacillus"),
                          S7 = c("Parasphingorhabdus", "Loktanella", "", "", "", "Kytococcus", "","", "Marinococcus","Bacillus"),
                          S9 = c("", "Loktanella", "", "", "", "", "","Acinetobacter baumanni", "Marinococcus",""))

sbdsn5lh

sbdsn5lh1#

有几种方法可以得到R中的4个变量的维恩图,但是超过这个数量的类别的维恩图是extremely complicated,不是一个很好的可视化数据的方法。下面是维基共享资源中的5个类别的维恩图的例子:

一个7类维恩甚至不能用椭圆形绘制,并涉及到一个复杂的花卉形状,可以在链接的文章中看到。
在任何情况下,您都可以看到,即使有5个类别的Venn也不是一种非常用户友好的数据表示方式。
在您的示例中,表示这类数据的自然方式是通过热图,首先需要将数据重塑为长格式。

library(tidyverse)

species_abundance %>%
  pivot_longer(-Genus, names_to = 'Site', values_to = 'Count') %>%
  mutate(Site = factor(Site, unique(Site))) %>%
  ggplot(aes(Site, Genus, fill = factor(Count))) +
  geom_tile(color = 'black') +
  geom_text(aes(label = ifelse(Count == 0, '', Count))) +
  coord_equal() +
  scale_fill_manual(guide = 'none', 
                    values = c('white', 'lightyellow', 'yellow', 'orange')) +
  theme_minimal(base_size = 16)

增编

如果您 * 真的 * 想要一个5类维恩图,显示5个站点共有的物种数量,您可以:

library(VennDiagram)

grid::grid.newpage()

with(sign(species_abundance[-1]),
     draw.quintuple.venn(sum(S3), sum(S5), sum(S7), sum(S9), sum(S10),
        sum(S3 == 1 & S5 == 1),  sum(S3 == 1 & S7 == 1),
        sum(S3 == 1 & S9 == 1),  sum(S3 == 1 & S10 == 1),
        sum(S5 == 1 & S7 == 1),  sum(S5 == 1 & S9 == 1),
        sum(S5 == 1 & S10 == 1), sum(S7 == 1 & S9 == 1),
        sum(S7 == 1 & S10 == 1), sum(S9 == 1 & S10 == 1),
        sum(S3 == 1 & S5 == 1 & S7 == 1),
        sum(S3 == 1 & S5 == 1 & S9 == 1),
        sum(S3 == 1 & S5 == 1 & S10 == 1),
        sum(S3 == 1 & S7 == 1 & S9 == 1),
        sum(S3 == 1 & S7 == 1 & S10 == 1),
        sum(S3 == 1 & S9 == 1 & S10 == 1),
        sum(S5 == 1 & S7 == 1 & S9 == 1),
        sum(S5 == 1 & S7 == 1 & S10 == 1),
        sum(S5 == 1 & S9 == 1 & S10 == 1),
        sum(S7 == 1 & S9 == 1 & S10 == 1),
        sum(S3 == 1 & S5 == 1 & S7 == 1 & S9 == 1),
        sum(S3 == 1 & S5 == 1 & S7 == 1 & S10 == 1),
        sum(S3 == 1 & S5 == 1 & S9 == 1 & S10 == 1),
        sum(S3 == 1 & S7 == 1 & S9 == 1 & S10 == 1),
        sum(S5 == 1 & S7 == 1 & S9 == 1 & S10 == 1),
        sum(S3 == 1 & S5 == 1 & S7 == 1 & S9 == 1 & S10 == 1),
        category = c("S3", "S5", "S7", "S9", "S10"),
        fill = c("orange", "red", "green", "blue", "yellow"),
        cex = 2,
        cat.cex = 2,
        cat.col = 'black'
))

尽管阅读/理解起来要困难得多,但它包含的信息也比热图少。例如,我可以从维恩图中看到只有S3和S5有一个共同的物种,但我可以从热图中同样清楚地看到这一点。此外,我可以告诉您属(它是副球菌),以及使用热图在每个地点观察到多少次副球菌。你不能用维恩图来做这件事。维恩图只是一个错误的工具来展示你所拥有的数据。

相关问题