如何在维恩图中定义相交的颜色? [英] How to define color of intersection in a Venn diagram?

查看:249
本文介绍了如何在维恩图中定义相交的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多有关如何在R中绘制维恩图的资源.堆栈溢出有很多.但是,我仍然无法按照自己的方式绘制图表.以以下代码为例:

I found many resources on how to draw Venn diagrams in R. Stack Overflow has a lot of them. However, I still can't draw my diagrams the way I want. Take the following code as an example:

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)

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

I want the intersection between the sets to be red. However, if I change any of the white colors to red, I get the following:

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

那不是我想要的.我只希望路口是红色的.如果我更改Alpha,这就是我得到的:

That's not quite what I want. I want only the intersection to be red. If I change the alpha, this is what I get:

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

现在我的路口有粉红色.这也不是我想要的.我想要的是这样的东西来自维基百科的图片:

Now I have pink in my intersection. This is not what I want as well. What I want is something like this image from Wikipedia:

我该怎么做?也许VennDiagram软件包无法做到,我还需要其他软件包,但是我一直在尝试不同的方法来做到这一点,而我却找不到解决方法.

How can I do this? Maybe VennDiagram package can't do it and I need some other package, but I've been testing different ways to do it, and I'm not being able to find a solution.

推荐答案

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

I will show two different possibilities. In the first example, polyclip::polyclip is used to get the intersection. In the second example, circles are converted to sp::SpatialPolygons and we get the intersection using rgeos::gIntersection. Then we re-plot the circles and fill the intersecting area.

使用venn.diagram时得到的对象是

"类"",其中包含组成该图的grid对象"

因此,在两种情况下,我们都可以从"vp"获取相关数据.首先,检查str外观并列出对象的grobs:

Thus, in both cases we can grab relevant data from "vp". First, check the structure and list the grobs of the object:

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所需的格式放置它们:


1. polyclip

Grab x- and y-values, and put them in the format required for 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)

获取圆的坐标:

# 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并找到它们的交点:

Convert points to SpatialPolygons and find their intersection:

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)

我很确定您可以使用gridgridSVG包中的剪切功能直接在grobs上工作.

I'm quite sure you could work directly on the grobs using clipping functions in grid or gridSVG package.

这篇关于如何在维恩图中定义相交的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆