如何在 R 中创建 muli-set table 重叠可视化?

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

是否可以在 R 中(最好使用 ggplot2)实现以下表格集交集可视化:

我不得不像素化 column-headers 和行标签,但我想这个想法仍然很清楚。

它与 UpsetR 有点相似,但更以 table 为中心。通常使用的维恩图不适用于此处,因为它的集合太多(每列 1 个)。

数据可以是例如

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
)

列应该是部门,员工应该进入行。

我认为这就足够了,或者至少是一个很好的起点。

library(data.table) 
setDT(df)[, id := .GRP, by = person] #make an id for each person


library(ggplot2)
ggplot(df, aes(x=department, y=id,group = department)) + 
 geom_point(aes(colour = department, shape = "a"), size = 2) +
 geom_line(aes(colour = department), size = 1) +
  scale_y_continuous(name="person",
                     breaks = seq(1,max(df$id)), labels = unique(df$person)) + 
  theme(legend.position="none")

这会给你:

这是一个很好的开始 M--,但我仍然以某种方式错过了用于视觉对齐的背景图块。恕我直言,另一个更漂亮的解决方案可能是

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")