UpSetR is the Greatest Set Visualization Since the Venn Diagram | by Eliana Grosof | Towards Data Science

I firmly believe that UpSetR is the most beautiful, intuitive set intersection visualization of large, complex sets I’ve ever seen.
Venn Diagrams make me smile, but UpSetR makes me feel powerful. UpSetR can visualize data Venn Diagrams can only dream about.
But first, let’s review Venn Diagrams.
Venn Diagrams are ubiquitous in our culture today. They’re easy to read, effective at getting information across, and generally fun to play with. But if you want to visualize more than three categories, what do you do?
Let’s look at a simple example.
Suppose we have a dataset about countries in the world, and we have a little bit of information about each.
A Venn Diagram visualizing the similarities and differences between the different countries might look like this:
This diagram looks pretty, but I’m not able to represent much, and it can start to look disorganized really fast.
What if I wanted to compare another country? Well, I’m out of luck. Do you really want to try to decipher something like this? What about this?
Yeah, I didn’t think so.
UpSet is a very new visualization technique that solves this problem by presenting the information in a table format with bar graphs. You can have as many things in each category as you want, and find the intersections between them easily. UpSetR is an R package that implements it.
Have a dataset from IMDB and see how many movies share the same actors, and who they are? No problem! Want to see if cancer patients have the same set of mutations in their genes, and what they are? UpSetR is here for you!
UpSetR is best for finding patterns within complex datasets with a lot of features. It does this by grouping data points that have many of the same values across different features together. That is, UpSetR finds the largest intersecting sets.
It is best for variables that are already binary (that means two categories), or can be converted into them. It’s important to note that one-hot encoding can turn any categorical feature into a binary feature. In fact, the current implementations only work for binary variables.
I’m in the process of developing a classification model for this Kaggle dataset, and I am trying to understand how the different columns relate to each other.
The dataset contains information about the attitudes of technology companies towards mental health, and the mental health of their employees. The survey data contains answers to everything from whether the employer provides mental health benefits to whether the employee themself has been treated for mental health issues.
At this point, it’s very unclear how each of these features relate to each other, and I’d like to get a sense of what’s going on.
Previously, I’ve cleaned the data a little bit, so I’ll feed it into R now. However, before I create the UpSetR visualization, I want to one-hot encode my data because it’s mostly categorical data.
One-hot encoding
Without getting too in depth, one-hot encoding is a way of turning categorical variables into binary variables by making new columns. For example, our work_interfere column has 3 options: often, sometimes, never. After we one-hot encode our data, we will have 3 columns: work_intefere_often, work_interfere_sometimes, work_interfere_never, each containing a 1 if that value applies and a 0 if not. There’s a lot of great tutorials about it on the internet if you want to learn more.
Here’s the R code:
data = read.csv('survey_data.csv', na.strings="Not Applicable")#install packages (can skip if already installed)install.packages("mltools") install.packages("data.table")install.packages("UpSetR")#one-hot encodelibrary(mltools)library(data.table)newdata <- one_hot(as.data.table(data))#create the visualizationlibrary(UpSetR)upset(newdata, order.by=”freq”, main.bar.color = “#995ee1”, sets.bar.color = “#995ee1”, group.by = ‘degree’) 
You can download the survey data here.
You’ll end up with this lovely thing:
How to interpret it
Left side bars
On the left side, there are bars that represent the total size of each set. In the case of Gender_Male, we can see that there are something like 950 survey respondents who identified their gender as Male. These sets are not disjoint — that is, mental_interview_No includes all of the people regardless of Gender who answered “No” to the question “Would you bring up a mental health issue with a potential employer in an interview?”
Top bars
Across the top, we can see the size of each intersecting set. If a black dot is filled in, then that category is included in the set. If the dot is gray, then that category is not in the set. Note that the sets are disjoint, which means that they are non-overlapping. You can see roughly every data point represented uniquely in the bars across the top, unlike the bars on the left.
Example 1: Since all of the dots on the first bar from the left are filled in, that bar represents all 475 of the people who responded Male to the question represented by the Gender variable, “No” to mental_health_interview, obs_consequence, and self_employed, and “Yes” to tech_company.
Example 2: However, the second set from the right represents a single man (Gender_Male) who had not heard of or observed negative consequences for coworkers with mental health conditions in their workplace (obs_consequence) but was self-employed at a tech company and would mention mental health in an interview. We know that the worker was self-employed and would have mentioned mental health in an interview, based on the fact that certain dots were not selected in the visualization and our knowledge of the other possible options.
In this visualization, it’s very important to think about the information that does not appear in the visualization, as in the second example. In some cases, that’s the most interesting part.
Let’s look at the first column from the left (Example 1).
I know that I have about 1200 data points, 988 of whom identify as male.The fact that 475 men said that they would not bring up a mental health issue with a potential employer in an interview mental_health_interview_No, were currently working for a tech company tech_company_Yes, had not heard of or observed negative consequences for coworkers with mental health conditions in their workplace obs_consequence_No, and were not self-employed self_employed_No is significant because it reveals that nearly half of men (48%) and over a third (33%) of all survey-takers responded in this exact same way.
If you add in the 132 people who were not men who also responded the same way to those four questions, as we can see in the second column from the left, then you realize that nearly HALF of the entire dataset answered those four questions exactly the same way (48%).
Interpretation
These results suggest to me that people, perhaps especially men, don’t talk openly about mental health in their workplace. This is an interesting preliminary finding.
Potential Improvements
If I want to improve my analysis, I could turn ordinal variables (variables with a sense of order, like a scale from 1–5, or “none, some, a lot”) into binary variables that preserve some sense of hierarchy by binning them (separating related values into two composite categories). This would allow them to factor more strongly into UpSetR’s analysis because UpSetR would have fewer options to consider.
I adore the UpSetR package and think it’s a beautiful way to do exploratory data analysis. The documentation for it is decent, and there’s a Python version of it available.
If you have any questions or comments, please reach out! I hope you learn to love this visualization as much as I do.