在R中创建一个数值求和的Venn图。

huangapple go评论70阅读模式
英文:

Create a Venn Diagram in R while creating a sum of numbers

问题

我正在尝试在R中创建一个Venn图,以显示不同机器上是否为所有参与者执行了某些测试。换句话说,我想查看参与者是否在三台、两台、一台或零台机器上执行了某些测试。

这是数据的示例:

dat <- data.frame(id=1:30,
                  machine1 = sample(0:7, 30, replace = T),
                  machine2 = sample(0:3, 30, replace = T),
                  machine3 = sample(0:6, 30, replace = T))

这些机器列是不同测试的原始列的总和。我省略了这些列,但如果更容易的话,可以使用以下方式创建:machine1test1 = sample(0:1, 30, replace = T) 等等

因此,如果参与者在机器1上有2个测试,在机器2上有3个测试,在机器3上没有测试,则应在机器1和机器2之间的重叠部分中添加值5。

我尝试过遵循在线示例,但它们似乎都是针对Venn图中的字符串值。这将需要我重新构建数据,我希望能够在不转换为字符串的情况下完成。我尝试了以下示例:

https://www.datanovia.com/en/blog/venn-diagram-with-r-or-rstudio-a-million-ways/
https://stackoverflow.com/questions/74028157/making-a-venn-diagram-from-a-count-table
https://stackoverflow.com/questions/69820167/how-to-add-count-values-in-venn-diagram-for-more-than-6-sets
https://stackoverflow.com/questions/71385638/create-a-venn-diagram-in-r-to-represent-rows-with-the-same-value-from-a-datafram

但似乎没有一个完全适用,因为它们大多适用于字符串值。非常感谢您的帮助!

英文:

I'm trying to create a Venn Diagram in R to show whether certain tests on different machines are performed for all participants. In other words, I'm interested to see if certain tests for participants are performed on all three, two, one or none of the machines.

Here is an example of the data:

dat &lt;- data.frame(id=1:30,
                  machine1 = sample(0:7, 30, replace =T),
                  machine2 = sample(0:3, 30, replace =T),
                  machine3 = sample(0:6, 30, replace =T))

These machine columns are sums of original columns for different tests. I have omitted those, but if easier they can be created with: machine1test1 = sample(0:1, 30, replace = T) etcetera

So, if a participant had 2 tests on machine 1 and 3 tests on machine 2 and 0 tests on machine 3, it should add a value of 5 in the Venn diagram for the overlap between machine 1 and machine 2.

I have tried to follow several examples online, but they all seem to take in string values for a Venn Diagram. This would require me to restructure the data, and I was hoping it's possible without converting to strings. I've tried to follow these example:

https://www.datanovia.com/en/blog/venn-diagram-with-r-or-rstudio-a-million-ways/
https://stackoverflow.com/questions/74028157/making-a-venn-diagram-from-a-count-table
https://stackoverflow.com/questions/69820167/how-to-add-count-values-in-venn-diagram-for-more-than-6-sets
https://stackoverflow.com/questions/71385638/create-a-venn-diagram-in-r-to-represent-rows-with-the-same-value-from-a-datafram

But none of those seem to fully apply, since they mostly apply to string values. Any help would be much appreciated!

答案1

得分: 0

以下是您要翻译的代码部分:

library(nVennR)
dat <- data.frame(id=1:30,
                  machine1 = sample(0:7, 30, replace =T),
                  machine2 = sample(0:3, 30, replace =T),
                  machine3 = sample(0:6, 30, replace =T))
toBin <- function(l){
  result <- 0
  bit <- 0
  for (v in rev(l)){
    if (v > 0){
      bpos <- bitwShiftL(1, bit)
      result <- result + bpos
    }
    bit <- bit + 1
  }
  return(result + 1)
}

nReg <- bitwShiftL(1, ncol(dat) - 1)
sets <- as.list(rep(0, nReg))
for (r in rownames(dat)){
  set <- toBin(dat[r, 2:ncol(dat)])
  sets[[set]] <- sets[[set]] + sum(dat[r, 2:ncol(dat)])
}

myV <- createVennObj(nSets = ncol(dat) - 1, sNames = colnames(dat[,2:ncol(dat)]), sSizes = sets)
myV <- plotVenn(nVennObj = myV)

希望这能帮助您。如果您需要更多帮助,请随时告诉我。

英文:

The simplest way I can think of would take advantage of how my nVennR package (link, the CRAN version is unavailable at this time) labels regions in a Venn diagram (as explained here). You would need an auxiliary function and row processing:

library(nVennR)
dat &lt;- data.frame(id=1:30,
                  machine1 = sample(0:7, 30, replace =T),
                  machine2 = sample(0:3, 30, replace =T),
                  machine3 = sample(0:6, 30, replace =T))
toBin &lt;- function(l){
  result &lt;- 0
  bit &lt;- 0
  for (v in rev(l)){
    if (v &gt; 0){
      bpos &lt;- bitwShiftL(1, bit)
      result &lt;- result + bpos
    }
    bit &lt;- bit + 1
  }
  return(result + 1)
}

nReg &lt;- bitwShiftL(1, ncol(dat) - 1)
sets &lt;- as.list(rep(0, nReg))
for (r in rownames(dat)){
  set &lt;- toBin(dat[r, 2:ncol(dat)])
  sets[[set]] &lt;- sets[[set]] + sum(dat[r, 2:ncol(dat)])
}

myV &lt;- createVennObj(nSets = ncol(dat) - 1, sNames = colnames(dat[,2:ncol(dat)]), sSizes = sets)
myV &lt;- plotVenn(nVennObj = myV)

And the result would be:

在R中创建一个数值求和的Venn图。

The key is toBin, where the values in each row get converted into a number whose binary representation is 1 where the value is higher than zero and 0 otherwise. With a couple of transformations, that is the Venn region (set in the code) where you want to store the sum of the values (sum(dat[r, 2:ncol(dat)).
There is more information about nVennR at its vignette.

huangapple
  • 本文由 发表于 2023年5月24日 17:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定