Krantz
Krantz

Reputation: 1493

Transforming data to create generalized, quasi-proportional Venn diagrams using Package ‘nVennR’

I have the below dataset and would like your help to transform it in order to be able to plot a Venn Diagram using the Package ‘nVennR’ by Pérez-Silva et al. 2018.

Here's the dataset:

dput(data)
structure(list(Employee = c("A001", "A002", "A003", "A004", "A005", 
"A006", "A007", "A008", "A009", "A010", "A011", "A012", "A013", 
"A014", "A015", "A016", "A017", "A018"), SAS = c("Y", "N", "Y", 
"Y", "Y", "Y", "N", "Y", "N", "N", "Y", "Y", "Y", "Y", "N", "N", 
"N", "N"), Python = c("Y", "Y", "Y", "Y", "N", "N", "N", "N", 
"N", "N", "Y", "Y", "N", "N", "N", "N", "Y", "Y"), R = c("Y", 
"Y", "N", "Y", "N", "Y", "N", "N", "Y", "Y", "Y", "Y", "Y", "Y", 
"Y", "Y", "N", "N")), .Names = c("Employee", "SAS", "Python", 
"R"), row.names = c(NA, -18L), class = c("tbl_df", "tbl", "data.frame"
))

See below an example of the Venn diagram I would like to get:

enter image description here

Update:

After installing the updated version of nVennR and rsvg, when I run the example code from here I get the error and diagram below:

Warning message:
In checkValidSVG(doc, warn = warn) :
This picture was not generated by the 'grConvert' package, errors may result

enter image description here

Below is my session info:

sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] nVennR_0.2.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.16       lattice_0.20-35    XML_3.98-1.10     
 [4] png_0.1-7          rsvg_1.1           grid_3.4.2        
 [7] plyr_1.8.4         gtable_0.2.0       scales_0.5.0.9000 
[10] ggplot2_2.2.1.9000 pillar_1.2.1       rlang_0.2.0.9001  
[13] grImport2_0.1-2    lazyeval_0.2.1     Matrix_1.2-12     
[16] tools_3.4.2        munsell_0.4.3      jpeg_0.1-8        
[19] compiler_3.4.2     base64enc_0.1-3    colorspace_1.3-2  
[22] tibble_1.4.2

I would appreciate any ideas to address this issue.

Upvotes: 0

Views: 1148

Answers (4)

vqf
vqf

Reputation: 2628

Just a quick note to let you know that the new version of nVennR is ready. Input and output control is different now, and toVenn is deprecated, to be replaced by plotVenn. There is a vignette with several examples, one of which uses the data in this question, here.

Upvotes: 1

vqf
vqf

Reputation: 2628

Regarding labels, the short answer is that you can edit them yourself with an SVG editor, such as Inkscape. If you have it installed, you can open the figure in the editor by running showSVG(mySVG = mySVG, opacity = 0.1, systemShow=T). You can also save the figure by providing an output file (outFile) or just open the temporary file that is generated.

The somewhat longer answer is that name1, name2,... can be replaced with the names of the lists. Unfortunately, due to my limitations in R, I did not realise that this might not be straightforward. It would be easier to load each variable as a table and set the colNames. For instance,

sas <- as.table(subset(data, SAS == "Y")$Employee)
names(sas) <- 'SAS'

That label will be used at the legend. Regarding the small labels, currently there is no way for a user to change them. Those are meant to help read the location of specific regions, and when those regions are small it does not seem feasible to use longer labels. My advice would be to always use an external editor to change them. The future version will have at least the ability to remove those labels, like in the Web version.

Upvotes: 1

vqf
vqf

Reputation: 11

Nice to have feedback so fast. Perhaps we should have stated in the docummentation that this version of nVennR is preliminary. Some researchers had asked for a quick way to run nVenn, so I just wraped the C++ code into a couple of R functions. As you can see, the result is shown in the viewer window, instead of the plot window. I am learning as I go. Since I see some interest on this package, I am compiling a list of features to add to the next version. Better input options are definitely in that list. Also, more control on the output (by the way, if colors are in the way, you can just set opacity to 0).

Regarding the question, @mysteRious is right, you send lists to the function. A quick way to do it would be

sas <- subset(data, SAS == "Y")$Employee
python <- subset(data, Python == "Y")$Employee
rr <- subset(data, R == "Y")$Employee
mySVG <- toVenn(sas, python, rr)
showSVG(mySVG = mySVG, opacity = 0.1)

The next version will have a method to enter names separately (sorry about that)

Upvotes: 1

mysteRious
mysteRious

Reputation: 4294

Here is one way using the limma package in Bioconductor with your data loaded in from the dput as the variable z:

source("http://www.bioconductor.org/biocLite.R")
biocLite("limma")
library(limma)

Change all Y to TRUE and all N to FALSE:

z2 <- data.frame(lapply(z, function(x) { gsub("Y", "TRUE", x) }))
z3 <- data.frame(lapply(z2, function(x) { gsub("N", "FALSE", x) }),stringsAsFactors=FALSE)

Make sure they are all logical type:

z3$SAS <- as.logical(z3$SAS)
z3$Python <- as.logical(z3$Python)
z3$R <- as.logical(z3$R)

Now tally up all the totals for each Venn region using vennCounts:

> ( venn.totals <- vennCounts(z3[,-1]) )
  SAS Python R Counts
1   0      0 0      1
2   0      0 1      4
3   0      1 0      2
4   0      1 1      1
5   1      0 0      2
6   1      0 1      3
7   1      1 0      1
8   1      1 1      4
attr(,"class")
[1] "VennCounts"

Producing the diagram is just one more step:

vennDiagram(venn.totals)

enter image description here

Upvotes: 2

Related Questions