krassowski/complex-upset

Color intersections matrix and bars by degree

lemoles opened this issue · 0 comments

Objective
Looking for an easy/simple way to colour intersections matrix and bars by degree. I have tried some approaches from previous issues, and could only manage to get intersections coloured. However, still struggling to have the bars coloured and ordered. I am providing a piece of my data set, and what I could achieve. Thank you in advance.

Code

query_by_degree = function(data, groups, params_by_degree, ...) {
  intersections = unique(upset_data(data, groups)$plot_intersections_subset)
  lapply(
    intersections,
    FUN=function(x) {
      members = strsplit(x, "-", fixed=TRUE)[[1]]
      args = c(
        list(intersect=members, ...),
        params_by_degree[[length(members)]]
      )
      do.call(upset_query, args)
    }
  )
}






gesta_data <- data %>% 
  select(gesta_aro, gesta_avafetal, 
         gesta_bcardiofetais, gesta_auterina, gesta_fouranc, gesta_twelve) 

sets_gesta <- c("gesta_aro","gesta_avafetal","gesta_bcardiofetais","gesta_auterina","gesta_fouranc","gesta_twelve")


upset(gesta_data,
      sets_gesta,
      name = "",
      width_ratio=0.1,
      min_size = 1, # minimum intersection size
      min_degree = 1,
      sort_intersections_by = c("degree","cardinality"),
      sort_intersections= "descending",
      labeller = as_labeller(c("gesta_aro" = "Evaluation of obstetric risk",
                               "gesta_avafetal" = "Evaluation of fetal presentation",
                               "gesta_bcardiofetais" = "Evaluation of fetal heart rate",
                               "gesta_auterina" = "Evaluation of uterine height",
                               "gesta_fouranc" = "Completed at least four antenatal care visits",
                               "gesta_twelve" = "First antenatal care within 12 weeks")),
      
      themes = upset_modify_themes(list( "intersections_matrix" = theme(text=element_text(size = 17,
                                                                                          color = "black")),
                                         "overall_sizes" = theme(text = element_text(size = 17,
                                                                                     color = "black")))),
      queries = query_by_degree(gesta_data, 
                                sets_gesta,
                                params_by_degree=list(
                                  "1"=list(color="red", fill="red"),
                                  "2"=list(color="red", fill="red"),
                                  "3"=list(color="red", fill="red"),
                                  "4"=list(color="yellow", fill="yellow"),
                                  "5"=list(color="darkgreen", fill="darkgreen"),
                                  "6"=list(color="darkgreen", fill="darkgreen")),
                                only_components=c("intersections_matrix", "Intersection size")),
      base_annotations = list("Intersection ratio" = 
                                intersection_ratio(text = list(size = 3.75),
                                                   bar_number_threshold = 1,
                                                   text_colors =c(on_background="black", on_bar="white"),
                                                   text_mapping=aes(label=!!upset_text_percentage(digits = 1))) +
                                ylab("Percent") +
                                theme(axis.text.y = element_text(color="black", 
                                                                 size = 13),
                                      axis.title.y = element_text(color="black",
                                                                  size = 13)) +
                                scale_y_continuous(labels=scales::percent_format(),
                                                   limits = c(0, 0.9),
                                                   breaks=seq(from = 0,
                                                              to = 1,
                                                              by = 0.15))),
      
      set_sizes = FALSE)

Screenshot or illustration
Screenshot 2024-08-22 110051

gesta_data.xlsx

Context (required)

ComplexUpset version: 1.3.3

R version details
4.3.3 
R session information
R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)