Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aaronxs opened this issue Jul 4, 2018 · 4 comments
Open

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

aaronxs opened this issue Jul 4, 2018 · 4 comments

Comments

@aaronxs
Copy link

aaronxs commented Jul 4, 2018

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
Copy link
Owner

mattflor commented Jul 5, 2018

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
Copy link
Author

aaronxs commented Jul 5, 2018

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
Copy link
Author

aaronxs commented Jul 6, 2018

@timdisher thought you might be interested in this as well

@mattflor
Copy link
Owner

mattflor commented Jul 7, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants