制作维恩图比较两个数据框之间的列组合
Making a Venn diagram comparing column combinations between two data frames
我无法解决这个问题:
我有两个数据框,每个数据框都有两列,名为 c("miRNAs", "Targets")
。每个数据框都是预测的 microRNA 及其来自不同程序的目标的组合。我正在尝试绘制一个显示这两个预测的交集的维恩图,但我在最后一步失败了。我可以发现相同点和不同点,但我无法绘制维恩图。
数据框如下所示:
> dput(test_a)
structure(list(miRNA = structure(c(1L, 1L, 1L, 1L, 4L, 3L, 2L
), .Label = c("miR-10", "miR-300", "miR-8", "miR-9"), class = "factor"),
Target = structure(c(1L, 1L, 3L, 4L, 1L, 5L, 2L), .Label = c("A",
"AA", "B", "C", "D"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
> dput(test_b)
structure(list(miRNA = structure(c(2L, 2L, 2L, 2L, 4L, 3L, 1L
), .Label = c("", "miR-10", "miR-12", "miR-9"), class = "factor"),
Target = structure(c(2L, 2L, 3L, 4L, 6L, 5L, 1L), .Label = c("",
"A", "B", "C", "D", "Y"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
#present in both dataframes
both_test_ab<- intersect(test_a, test_b)
miRNA Target
1 miR-10 A
2 miR-10 B
3 miR-10 C
#present only in a
only1 <- setdiff(test_a, test_b)
miRNA Target
1 miR-9 A
2 miR-8 D
3 miR-300 AA
#present only in b
only2 <- setdiff(test_b,test_a )
miRNA Target
1 miR-9 Y
2 miR-12 D
#Venn diagram (PROBLEM)
vp <- venn.diagram(
x = list(test_a, test_b), col=c("#440154ff", '#21908dff'),
fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3)),
category.names = c("seed" , "miranda"),
filename = NULL, cat.pos = 1)
谁能告诉我为什么我画不出维恩图以及如何解决这个问题?
谢谢
您使用了错误的数据类型。函数 venn.diagram()
需要一个 向量列表 。然而,test_a
和test_b
是数据帧。
我将 test_a
和 test_b
转换为向量,并且成功了。见下文。
# Convert to vectors
test_a_vector <- as.vector(unlist(test_a))
test_b_vector <- as.vector(unlist(test_b))
# Create Venn Diagram
venn.diagram(
x = list(test_a_vector, test_b_vector), col=c("#440154ff", '#21908dff'),
category.names = c("seed" , "miranda " ), fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3)),
filename = '#Ven_Test.tiff',
output=TRUE, cat.pos = 1)
结果:
我无法解决这个问题:
我有两个数据框,每个数据框都有两列,名为 c("miRNAs", "Targets")
。每个数据框都是预测的 microRNA 及其来自不同程序的目标的组合。我正在尝试绘制一个显示这两个预测的交集的维恩图,但我在最后一步失败了。我可以发现相同点和不同点,但我无法绘制维恩图。
数据框如下所示:
> dput(test_a)
structure(list(miRNA = structure(c(1L, 1L, 1L, 1L, 4L, 3L, 2L
), .Label = c("miR-10", "miR-300", "miR-8", "miR-9"), class = "factor"),
Target = structure(c(1L, 1L, 3L, 4L, 1L, 5L, 2L), .Label = c("A",
"AA", "B", "C", "D"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
> dput(test_b)
structure(list(miRNA = structure(c(2L, 2L, 2L, 2L, 4L, 3L, 1L
), .Label = c("", "miR-10", "miR-12", "miR-9"), class = "factor"),
Target = structure(c(2L, 2L, 3L, 4L, 6L, 5L, 1L), .Label = c("",
"A", "B", "C", "D", "Y"), class = "factor")), class = "data.frame", row.names = c(NA,
-7L))
#present in both dataframes
both_test_ab<- intersect(test_a, test_b)
miRNA Target
1 miR-10 A
2 miR-10 B
3 miR-10 C
#present only in a
only1 <- setdiff(test_a, test_b)
miRNA Target
1 miR-9 A
2 miR-8 D
3 miR-300 AA
#present only in b
only2 <- setdiff(test_b,test_a )
miRNA Target
1 miR-9 Y
2 miR-12 D
#Venn diagram (PROBLEM)
vp <- venn.diagram(
x = list(test_a, test_b), col=c("#440154ff", '#21908dff'),
fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3)),
category.names = c("seed" , "miranda"),
filename = NULL, cat.pos = 1)
谁能告诉我为什么我画不出维恩图以及如何解决这个问题? 谢谢
您使用了错误的数据类型。函数 venn.diagram()
需要一个 向量列表 。然而,test_a
和test_b
是数据帧。
我将 test_a
和 test_b
转换为向量,并且成功了。见下文。
# Convert to vectors
test_a_vector <- as.vector(unlist(test_a))
test_b_vector <- as.vector(unlist(test_b))
# Create Venn Diagram
venn.diagram(
x = list(test_a_vector, test_b_vector), col=c("#440154ff", '#21908dff'),
category.names = c("seed" , "miranda " ), fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3)),
filename = '#Ven_Test.tiff',
output=TRUE, cat.pos = 1)
结果: