Transmission chain using epicontacts

Hi all, I have been searching for solutions to this but haven’t been able to find any.

I am using the epicontacts package and following the Handbook to produce an outbreak transmission chain. I would like to include cases with no known epi link.

I was previously able to include cases without a known infector (they would float on their own). I then re-downloaded the package to fix a bug with the col_pal function and now it automatically assigns NA_x to them. Is there a way to stop it doing this?

Any help would be really appreciated.


1 Like

Thanks for your post @mdal !

I am tagging package author @finlaycampbell to see if he has any suggestions.


Hi @mdal - I’m happy to help with this. We had to change this because all NA ID labels were being incorrectly grouped as a single ID.

What kind of behaviour exactly would you like? Would you like the label “NA_X” to be removed? If it’s just that you could manually edit the ID in the linelist/contacts to be a blank character " ". If there something else I’d be happy to look into it.

1 Like

Thanks for connecting us @neale!

Hi @finlaycampbell, thanks so much for your willingness to help.

I would like to be able to include cases with unknown infectors without having an extra node assigned to them. Currently, the NAs show up as nodes making the cluster look larger than it is - because I have 13 cases with unknown epi links, it looks like I have 13 extra cases.

Previously, I did not use drop_na(infector) when generating contacts, then when I used make_epicontacts, those with unknown infectors just remained NA. When I plotted the epicontacts object, cases with unknown infectors would appear as individual nodes or as the first node in the transmission chain.

I have copied an example image below. Ideally, the NA_4 node wouldn’t exist.

I am still relatively new to R so I’m sorry if I’m missing something obvious!



1 Like

Ah I see what you mean!

I feel like this should only be happening if you include a contact between an NA and 19 in your contacts dataframe - is this the case? If you just include case 19 in the linelist (and no NA -> 19 contact in the contacts dataframe) then the case should just appear without any links to it. So yes, doing drop_na(infector) in the contacts dataframe before you do make_epicontacts should hopefuly fix the problem? The update to the package update is working as intended - if you have a contact between an NA and a case that implies we have a missing case!


It’s definitely closer! Using drop_na(infector) removes the issue of generating NA nodes but unfortunately it also removes the cases without any other links. Is there a way for these cases to still appear as individual/unlinked nodes?

Also my arrows have now disappeared - is there a quick fix to this?

Thanks so much for your help! This is a much friendlier forum than some of the others out there :slight_smile:


You’ll have to set the argument thin = FALSE - does that work? The default is currently set to TRUE, maybe that’s not the best choice!


Yes, perfect! Thank you so much!!


Hi @finlaycampbell, I’m sorry for the multiple questions but is there a fix to have arrows show on the transmission chain? They stopped showing when I re-downloaded the package.
Thanks very much

1 Like

Hmm, did that change with the upate? That’s weird. Have you set directed = TRUE in the make_epicontacts call?

1 Like

Thanks for your reply @finlaycampbell.

Yes to both. I set directed = TRUE and it’s stopped working with the new download of the package.

Oddly, arrows show if I add a temporal x_axis but I don’t want to do this as this is messy with the outbreak I’m working with.

Any ideas?


Thanks for pointing that out, turns out there was a little bug that I hadn’t caught! I’ve just pushed an update, if you re-install the package hopefully the problem will be fixed :slight_smile:


Thanks so much Finlay. Unfortunately still not working here but I’m really not sure why. I’ll keep working on it but if you think of anything else, any help is appreciated.
Thanks again

1 Like

Hmm, could you first try updating your visNetwork package? If that doesn’t work, could you post a minimum working example so I can try to recreate the issue on my end? Thanks!


@mdal here is a video on how to make a minimum reproducible example:


Hi @finlaycampbell - thanks so much! Please find a reproducible example below. Next to the plot function I have made comments of commands that aren’t working for me. If you are able to help, these are the functions I would like to be able to change on the transmission chain:

  • Arrows to appear between nodes
  • Set the position of unlinked nodes (top or bottom)
  • Have transmission lines be fixed straight
  • Show the legend in two columns and set font size of legend text
#install and load packages
pacman::p_load(rio, here, tidyverse, remotes, visNetwork, epicontacts)

#generate data sample 
demodata <- data.frame(
  stringsAsFactors = FALSE,
  case_id = c(1:40), 
  date_onset = c("2019-05-01", "2019-05-05", "2019-05-08", "2019-05-23"),
  species = c("test_1", "test_2", "test_3", "test_4", "other"), 
  infector_1 = c("3", "1", "1", "1", "4", "1", "4", "7", "8", "3", NA, "1", "10", "5", "11", 
                 NA, NA, NA, NA, NA, "40", "40", NA, NA, NA, NA, NA,"30", "27", NA, 
                 "33", NA, "28", NA, "34", "19", "38", "37", NA, "3" ), 
  contact_type = c("Direct", "Direct", "Indirect", "Indirect", "Direct"))

#change date class 
demodata  <- demodata  %>% 
  mutate(date_onset = as.Date(date_onset)) 

#Change to factors and make labels 
demodata$species <- factor(demodata$species, labels = c("Test 1",            
                                                        "Test 2", 
                                                        "Test 3", 
                                                        "Test 4", 
demodata$contact_type <- factor(demodata$contact_type)

#Generate contacts ####
contacts <- demodata %>%
    infector = infector_1,
    case_id = case_id,
    location = contact_type,
    date_onset = date_onset
  ) %>% 

#Generate epicontacts object  ####
epic <- make_epicontacts(
  linelist = demodata,
  contacts = contacts,
  id = "case_id",
  from = "infector",
  to = "case_id",
  directed = TRUE

#colour palette for nodes 
mycols <- c(
  `Test 1` = "#440154", 
  `Test 2` = "#3b528b", 
  `Test 3` = "#5ec962",
  `Test 4` = "#fde725", 
  Other = "grey") 

#plot the transmission chain ####
  x_axis = NULL, 
  node_color = "species",      
  node_size = 10,               
  col_pal = mycols,            
  thin = FALSE,                
  label = "id",                 
  unlinked_pos = "top",         #not working - position of unlinked nodes 
  edge_flex = FALSE,            #not working - edges to remain straight when moved
  edge_color = 'location',      
  edge_col_pal = c(Direct = "black", Indirect = "grey"), 
  edge_linetype = 'location',   
  edge_width = 2,               
  font_size = 20,               
  arrow_size = 0.5,             #not working - no arrows showing 
  legend = TRUE,                
  legend_ncol = 2,              #not working - show legend in two columns
  height = 800,
  width = 1000


Thanks for the reproducible example! Here is the figure I get on my end, which I think addresses most of the issues you have raised above. I made one change by setting the legend_width = 0.2, which will increase space given to the legend and therefore the size of the text and elements.

The fact that these features aren’t working on your end suggest it’s a dependency issue. I would try re-installing visNetwork (install.packages("visNetwork") and check packageVersion("visNetwork") to see that it is at version 2.1.2.

Two of the features you mentioned (setting position of unlinked nodes, fixing transmission links to be straight) are only available for network where the the x_axis argument is specified.


Thanks @finlaycampbell - it must be an issue on my end. I’m using visNetwork 2.1.2 and epicontacts 1.1.2.
I re-installed both today and now receive the following error:
Error in pal(nlev) : could not find function “pal”
I didn’t change any of the code. I’ll try on another computer or work with what I have.
Thanks for all your help.