mattflor / chorddiag

R interface to D3 chord diagrams
159 stars 44 forks source link

Is it possible to add additional labels on the chord diagram? #22

Open aaronxs opened 6 years ago

aaronxs commented 6 years ago

Hi Matt,

I was wondering would chorddiag currently able to add additonal "labels" to the link in the chord diagram?

For example, in the first example here, if the size of the links represent the number of students, is it possible to add additional texts that would be displayed when you hover around the links? An example is adding the name of the teachers for each class/section as well as the time so that when you however around the links, not only it will show you the number of students but also the teacher's name and time of the class.

Thanks! Aaron

mattflor commented 6 years ago

There's the tooltipNames argument (defaults to the groupNames argument; you can use HTML tags when you define your own tooltipNames). So maybe you can tinker with that argument along the following lines:

library(chorddiag)

students = data.frame(Math = c(50, 25, 5, 12),
                      Art = c(10, 55, 5, 20),
                      Science = c(45,12,29, 20),
                      PE = c(24,67,27,15))
students = as.matrix(students)

sections <- paste("Section", LETTERS[1:4])
courses <- colnames(students)
dimnames(students) <- list(Section = sections,
                           Course = courses)
teachers <- c("Smith", "Johnson", "Williams", "Brown", "Jones", "Miller", "Davis", "Garcia")
tooltip_names <- paste(paste0("<b>", c(sections, courses), "</b>"), 
                       paste0("<br/>Teacher: ", teachers, "<br/>"))

chorddiag(students, type = "bipartite", showTicks = F, margin = 90,
          groupnameFontsize = 14, groupnamePadding = 10,
          tooltipNames = tooltip_names)
aaronxs commented 6 years ago

Hi Matt,

Thank you for your answer! Sorry but after working through the answer you provided I realized it might not solve the problem I have on hand . The reason being in my problem each combination of "course" and "section" have a different set of "teachers".

What I am trying to create is a network diagram that shows how drugs are connected (through clinical trials). The nodes here are the drugs. The links connecting the nodes (drugs) represent the trials involving the drugs and the number of patients in these trials determines the width/size of the links.

What I would like to do is to potentially add the list of the studies that connect the nodes to the label of each link. It would also be nice to remove the little right arrow currently showing in the label since it's kind of misleading in this case to say "Drug1 => Drug2".

Here are the codes and data sets I used in the example:

library(tidyverse);library(magrittr);library(chorddiag)

# dataframe with the  treatments names (the nodes)
trt <- read.table("TreatmentCoding1.txt", stringsAsFactors = TRUE, header = T)
# dataframe with all the study list (the links)
datt <- read.table("Sample_Data_Study1.txt", stringsAsFactors = FALSE, header = T)

datt %<>%
  # get the names of the drugs from the treatment numbers
  mutate(trt1 = trt$name[match(treat1num, trt$treatment)],
         trt2 = trt$name[match(treat2num, trt$treatment)]) %>%
  select(trt1, trt2, patient_count) %>%
  # convert data from long (adjacency list) to wide (adjacency matrix)
  spread(key = trt2, value = patient_count, drop = F, fill = 0) %>%
  column_to_rownames("trt1") %>%
  as.matrix()

# plot with chorddiag
chorddiag(datt, 
          type = "directional",
          showTicks = F, margin = 90,
          groupnameFontsize = 14, 
          showZeroTooltips = F)

TreatmentCoding1.txt

Sample_Data_Study1.txt

Thank you!! Aaron

aaronxs commented 6 years ago

@timdisher thought you might be interested in this as well

mattflor commented 6 years ago

I'm afraid that listing studies in the chord tooltips is not possible (at least I don't know how to do it right now) but you can use the tooltipGroupConnector to replace that arrow head and maybe add a 'patients' unit to the tooltips like this:

chorddiag(datt, 
          type = "directional",
          showTicks = F, margin = 90,
          groupnameFontsize = 14, 
          tooltipGroupConnector = " & ",
          tooltipUnit = " patients",
          showZeroTooltips = F)