saeyslab / nichenetr

NicheNet: predict active ligand-target links between interacting cells
452 stars 113 forks source link

Customize Color of ligands in Circle Plot #200

Closed maxc2020 closed 11 months ago

maxc2020 commented 1 year ago

Hello NicheNetr Community,

I've used NicheNetr to look at differential receptor/ligand pairs from a control vs treated condition and generated helpful data. Thanks! I am looking for input on how to improve my data visualization. I have used code from a vignette, with minimal adaption to provide a custom list for the sender coloring:

filtered_ligands = ligand_prioritized_tbl_oi %>% filter(receiver == receiver_oi) %>% top_n(20, prioritization_score) %>% pull(ligand) %>% unique()
prioritized_tbl_oi = prioritization_tables$prioritization_tbl_ligand_receptor %>% filter(ligand %in% filtered_ligands) %>% select(niche, sender, receiver, ligand,  receptor, ligand_receptor, prioritization_score) %>% distinct() %>% inner_join(top_ligand_receptor_niche_df) %>% group_by(ligand) %>% filter(receiver == receiver_oi) %>% top_n(2, prioritization_score) %>% ungroup() 
colors_sender = c("lightgrey","skyblue1", "tomato","pink", "orange", "lightgoldenrod1", "royalblue", "palegreen") %>% magrittr::set_names(prioritized_tbl_oi$sender %>% unique() %>% sort())
colors_receiver = c("purple")  %>% magrittr::set_names(prioritized_tbl_oi$receiver %>% unique() %>% sort())
circos_output = make_circos_lr(prioritized_tbl_oi, colors_sender, colors_receiver)

My question is: how can I select specific ligands for a distinct color? I would like to highlight a select few that are most interesting, so that they are easily visible to the casual viewer. Ideally I could manually code it as a list of selected ligands to color red. After re-reviewing the NicheNetr and Circlize vignettes & past questions, I am not sure how to proceed and could use some help. thank you.

csangara commented 1 year ago

Do you want the arcs themselves to be colored, or just the text of the ligand?

maxc2020 commented 1 year ago

Ideally both, but either would be ok. I would like to focus attention on the interactions of interest.

Thanks for helping! Max

csangara commented 1 year ago

As far as I know, it's very difficult to change or highlight specific arcs because the whole plot is drawn with circlize::chordDiagram. Changing the text color is possible but requires you to modify the make_circos_lr wrapper function a little bit.

You can find the function in R/differential_nichenet_plotting.R. There's two things you have to do:

  1. Add an extra parameter in the function definition for passing the ligands you are highlighting, e.g., ligands_oi = NULL
  2. Add the following if statement under the circos.track where the text is being written:
  circos.track(track.index = 1, panel.fun = function(x, y) {
    if (CELL_META$sector.index %in% ligands_oi){
      circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index,
                  facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex = 1, col ="red")
    } else {
      circos.text(CELL_META$xcenter, CELL_META$ylim[1], CELL_META$sector.index,
                  facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5), cex = 1)
    }

  }, bg.border = NA)

This will change the text color to red if the current ligand is in the ligands_oi vector.

So I will get the following plot if I run the code from the Differential NicheNet vignette:

ligands_oi <- c("Apoa1", "Lsamp")
circos_output = make_circos_lr(prioritized_tbl_oi, colors_sender, colors_receiver, ligands_oi = ligands_oi)

image

maxc2020 commented 1 year ago

Thank you