R中的维恩图显示字符标签
Venn Diagram in R to show character labels
我是 R 的新手,正在尝试使用包含所有分类变量的数据创建 4 维维恩图。
例如,如果我有以下数据并且我想在 R 中创建维恩图以在 A 和 B 的交叉点显示单词“hello”而不是计数和百分比,我该怎么做?我在创建下面的列表后使用了代码 ggVennDigram(x)
,它给了我计数而不是实际的数据标签。
x=list()
x$A=as.character(c("hello"))
x$B=as.character(c("hello", "how", "are"))
x$C=as.character(c("how", "You"))
x$D=as.character(c("me", "her", "they"))
默认情况下无法为图表添加标签。这是一些 hack 的解决方案。
(修改自Venn Diagram with Item labels)
library(VennDiagram)
library(stringr)
library(purrr)
# Generate plot
v <- venn.diagram(x,
fill = c("orange", "blue", "red", "green"),
filename=NULL)
# Calculate overlap site
overlaps <- calculate.overlap(x)
overlaps <- overlaps[str_sort(names(overlaps), numeric = TRUE)] # sort base on numeric value
# Apply name to global variable
# Index of venn diagram start at calculate.overlaps + 8. You have to find the index value by yourself for (3,5,6,7,.. venn)
walk2(seq(overlaps) +8, seq(overlaps),
function(x,y) {v[[x]]$label <<- paste0(overlaps[[y]], collapse = "\n")})
# Draw plot
grid.draw(v)
我是 R 的新手,正在尝试使用包含所有分类变量的数据创建 4 维维恩图。
例如,如果我有以下数据并且我想在 R 中创建维恩图以在 A 和 B 的交叉点显示单词“hello”而不是计数和百分比,我该怎么做?我在创建下面的列表后使用了代码 ggVennDigram(x)
,它给了我计数而不是实际的数据标签。
x=list()
x$A=as.character(c("hello"))
x$B=as.character(c("hello", "how", "are"))
x$C=as.character(c("how", "You"))
x$D=as.character(c("me", "her", "they"))
默认情况下无法为图表添加标签。这是一些 hack 的解决方案。 (修改自Venn Diagram with Item labels)
library(VennDiagram)
library(stringr)
library(purrr)
# Generate plot
v <- venn.diagram(x,
fill = c("orange", "blue", "red", "green"),
filename=NULL)
# Calculate overlap site
overlaps <- calculate.overlap(x)
overlaps <- overlaps[str_sort(names(overlaps), numeric = TRUE)] # sort base on numeric value
# Apply name to global variable
# Index of venn diagram start at calculate.overlaps + 8. You have to find the index value by yourself for (3,5,6,7,.. venn)
walk2(seq(overlaps) +8, seq(overlaps),
function(x,y) {v[[x]]$label <<- paste0(overlaps[[y]], collapse = "\n")})
# Draw plot
grid.draw(v)