Search code examples
rvenn-diagram

Making a Venn Diagram from a Dataframe


Hi so I have a dataframe that looks like this:

df1:

Name Event1 Event 2 Event3 Event4
Amanda 1 1 1 1
Emily 0 0 1 1
George 1 0 0 0
Baker 0 0 1 0
Lee 1 1 1 1
Jorge 0 1 0 0

My goal is to create a venn diagram, where there are four events that makes the four circles in a venn diagram. And say if within a row, if all four events are equal to 1, then that would contribute to the inner most middle value. Whereas if there was a 1 for event 1 and event 3, I would have a value that is tallied for the area that engulfs the venn diagram ring for event 1 and 3.

I looked into using the ggvenn package and the VennDiagram approach, but neither is working in my favor and I would appreciate any help please. Thank you!

I went ahead and posted the code below for the data frame:

# Creating the df1
Name <- c("Amanda", "Emily", "George", "Baker", "Lee", "Jorge")
Event1 <- c(1,0,1,0,1,0)
Event2 <- c(1,0,0,0,1,1)
Event3 <- c(1,1,0,1,1,0)
Event4 <- c(1,1,0,0,1)
df1 <- data.frame(Name, Event1, Event2, Event3, Event4)

Solution

  • If you change the order of columns in your dataframe and 'convert' each Event column from "1/0" to "TRUE/FALSE" the ggvenn package works as expected, i.e.

    library(tidyverse)
    #install.packages("ggvenn")
    library(ggvenn)
    #> Loading required package: grid
    
    Name <- c("Amanda", "Emily", "George", "Baker", "Lee", "Jorge")
    Event1 <- c(1,0,1,0,1,0)
    Event2 <- c(1,0,0,0,1,1)
    Event3 <- c(1,1,0,1,1,0)
    Event4 <- c(1,1,0,0,1, 0)
    df1 <- data.frame(Event1, Event2, Event3, Event4, Name)
    
    df1 %>%
      mutate(across(starts_with("Event"), as.logical)) %>%
      ggplot() +
      geom_venn(aes(A = Event1, B = Event2, C = Event3, D = Event4),
                set_names = Name)
    

    Created on 2022-07-05 by the reprex package (v2.0.1)

    I was getting an error when "Name" was the first column in the dataframe ('column 1 isn't logical') so moving that column and changing the 0/1 to TRUE/FALSE made sense.