Search code examples
rggplot2visualizationvenn-diagram

How to create a muli-set table overlap visualization in R?


Is it possible in R (preferable using ggplot2) to implement the following tabular set intersection visualization:

enter image description here

I had to pixelate column-headers and row labels but I guess the idea is still clear.

It's a bit similar to UpsetR but more table centric. Typically used venn diagrams don't apply here because it's too many sets (1 per column).

Data could be e.g.

depusers = frame_data(
~ person, ~ department, ~is_leader,
"Bob", "dev", TRUE,
"Bob", "accounting", FALSE,
"Marta", "dev", FALSE,
"Marta", "misc", FALSE,
"Max", "dev", FALSE,
"Max", "accounting", TRUE,
"Tim", "misc", TRUE,
"Tim", "security", FALSE,
"Horst", "security", FALSE,
"Tom", "management", TRUE
)

Columns should be the departments and the employees should go into the rows.


Solution

  • That's a great start M--, still I somehow missed the background tiles for visual alignment. An alternative imho slightly more pretty solution might be

    bcknd_tiles = tidyr::expand(depusers, person, department) %>% inner_join(distinct(depusers, person) %>%
        arrange(person) %>%
        mutate(check_color = as.factor(row_number() %% 2)))
    
    depusers %>% ggplot(aes(x = department, y = person, group = department)) +
        geom_tile(aes(fill = check_color), size = 3, color = "white", data = bcknd_tiles) +
        scale_fill_manual(values = c("1" = "white", "0" = "lightgray")) +
        geom_point(aes(colour = department, shape = "*", size = is_leader)) +
        scale_size_manual(values = c(3, 7)) +
        geom_line(aes(colour = department), size = 1) +
        scale_y_discrete(name = "person") +
        theme(legend.position = "none", axis.ticks = element_blank(), panel.background = element_rect(fill = 'white')) +
        ggtitle("Company structure")
    

    enter image description here