【问题标题】:How to define color of intersection in a Venn diagram?如何在维恩图中定义交叉点的颜色?
【发布时间】:2017-05-14 11:20:59
【问题描述】:

我找到了很多关于如何在 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)

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

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

这不是我想要的。我只希望十字路口是红色的。如果我更改 alpha,这就是我得到的:

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

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

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

【问题讨论】:

    标签: r venn-diagram


    【解决方案1】:

    我将展示两种不同的可能性。在第一个示例中,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。 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)
    


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

    【讨论】:

    • 没有。您可能会在 plot.euler.R 中找到有用的信息。
    【解决方案2】:

    在 eulerr R 包中非常简单

    library(eulerr)
    plot(euler(c("A"=5,"B"=4,"A&B"=2)),quantities = TRUE,fills=c("white","white","red"))
    

    euler set colours

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-15
      • 2015-11-26
      • 2012-02-01
      相关资源
      最近更新 更多