我将展示两种不同的可能性。在第一个示例中,polyclip::polyclip 用于获取交集。在第二个示例中,圆被转换为sp::SpatialPolygons,我们使用rgeos::gIntersection 得到交集。然后我们重新绘制圆圈并填充相交区域。
使用venn.diagram时产生的对象是
“属于gList 类,包含构成图表的grid 对象”
因此,在这两种情况下,我们都可以从“vp”中获取相关数据。首先查看structure,列出对象的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)
2。 SpatialPolygons 和 gIntersection
获取圆的坐标:
# 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)
我很确定您可以使用 grid 或 gridSVG 包中的剪辑功能直接在 grobs 上工作。