r - 如何定义维恩图中相交的颜色?

标签 r venn-diagram

我找到了很多关于如何在 R 中绘制维恩图的资源。Stack Overflow has a lot of them 。但是,我仍然无法按照我想要的方式绘制图表。以下面的代码为例:

library("VennDiagram")

A <- 1:4
B <- 3:6
d <- list(A, B)

vp <- venn.diagram(d, fill = c("white", "white"), alpha = 1, filename = NULL, 
  category.names=c("A", "B"))
grid.draw(vp)

enter image description here

我希望集合之间的交集为红色。但是,如果我将任何白色更改为红色,我会得到以下结果:

vp_red <- venn.diagram(d, fill = c("red", "white"), alpha = 1, filename = NULL, 
  category.names=c("A", "B"))
grid.draw(vp_red)

enter image description here

这不是我想要的。我只希望交叉路口是红色的。如果我改变阿尔法,这就是我得到的:

vp_alpha <- venn.diagram(d, fill = c("red", "white"), alpha = 0.5, filename = NULL, 
  category.names=c("A", "B"))
grid.draw(vp_alpha)

enter image description here

现在我的十字路口出现了粉红色。这也不是我想要的。我想要的是这样的image from Wikipedia :

enter image description here

我该怎么做?也许 VennDiagram 包无法做到这一点,我需要一些其他包,但我一直在测试不同的方法来做到这一点,但我无法找到解决方案。

最佳答案

我将展示两种不同的可能性。在第一个示例中,polyclip::polyclip 用于获取交集。在第二个示例中,圆被转换为 sp::SpatialPolygons,并且我们使用 rgeos::gIntersection 获得交集。然后我们重新绘制圆圈并填充相交区域。

<小时/>

使用venn.diagram时生成的对象是

"of class gList containing the grid objects that make up the diagram"

因此,在这两种情况下我们都可以从“vp”中获取相关数据。首先,检查str结构并列出对象的grobs:

str(vp)
grid.ls()
# GRID.polygon.234
# GRID.polygon.235
# GRID.polygon.236 <~~ these are the empty circles
# GRID.polygon.237 <~~ $ col : chr "black"; $ fill: chr "transparent"
# GRID.text.238 <~~ labels
# GRID.text.239
# GRID.text.240
# GRID.text.241
# GRID.text.242 
<小时/>

1。 polyclip

获取 x 和 y 值,并将它们采用 polyclip 所需的格式:

A <- list(list(x = as.vector(vp[[3]][[1]]), y = as.vector(vp[[3]][[2]])))
B <- list(list(x = as.vector(vp[[4]][[1]]), y = as.vector(vp[[4]][[2]])))

寻找交集:

library(polyclip)
AintB <- polyclip(A, B)

抓取标签:

ix <- sapply(vp, function(x) grepl("text", x$name, fixed = TRUE))
labs <- do.call(rbind.data.frame, lapply(vp[ix], `[`, c("x", "y", "label")))

绘制它!

plot(c(0, 1), c(0, 1), type = "n", axes = FALSE, xlab = "", ylab = "")
polygon(A[[1]])
polygon(B[[1]])
polygon(AintB[[1]], col = "red")
text(x = labs$x, y = labs$y, labels = labs$label)

enter image description here

<小时/>

2。 SpatialPolygonsgIntersection

获取圆的坐标:

# grab x- and y-values from first circle
x1 <- vp[[3]][["x"]]
y1 <- vp[[3]][["y"]]

# grab x- and y-values from second circle
x2 <- vp[[4]][["x"]]
y2 <- vp[[4]][["y"]]

将点转换为SpatialPolygons并找到它们的交点:

library(sp)
library(rgeos)
p1 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(x1, y1))), ID = 1))) 
p2 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(x2, y2))), ID = 2))) 

ip <- gIntersection(p1, p2) 

绘制它!

# plot circles 
plot(p1, xlim = range(c(x1, x2)), ylim = range(c(y1, y2))) 
plot(p2, add = TRUE) 

# plot intersection
plot(ip, add = TRUE, col = "red") 

# add labels (see above)
text(x = labs$x, y = labs$y, labels = labs$label)

enter image description here

<小时/>

我非常确定您可以使用 gridgridSVG 包中的剪切函数直接在 grobs 上工作。

关于r - 如何定义维恩图中相交的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963293/

相关文章:

r - 匹配 R 中给定向量中的一系列单个或多个单词

R 语言,暂停循环并要求用户继续

r - 什么是命名号码?

r - 使用 R 中的现有计数创建维恩图?

r - 从数据框制作维恩图

regex - R:回归匹配特定模式的所有变量

r - 如何只保留连续第一次出现的重复项?

r - 没有组名和 Arial 字体的 VennDiagram

graph - 我可以从包含图自动生成欧拉(维恩)图吗

重绘维恩图