rggplot2network-tools

How to plot multiple data (bridge-objects) in R (networktools)


I'm using package "networktools" in R (https://cran.r-project.org/web/packages/networktools/networktools.pdf). I've created a three "bridge"-objects: DataT5_SDQ_network_b, DataT6_SDQ_network_b, and DataT7_SDQ_network_b.

I've sucessfully plotted one "bridge"-object using this code:

plot(DataT7_SDQ_network_b, include=c("Bridge Expected Influence (1-step)"), theme_bw=FALSE, zscore=TRUE)

Q: How can I plot all three "bridge"-objects in the same plot (with legend)?


Solution

  • I don't think there's a way to do this inside networktools, but since the plot generic returns a ggplot object, we can harvest multiple plots and combine them with a legend like this:

    p <- lapply(list(DataT5_SDQ_network_b,
                     DataT6_SDQ_network_b,
                     DataT7_SDQ_network_b), function(x) suppressWarnings(plot(x)))
    
    p <- Map(function(a, b) { a$data$Class <- b; a}, a = p, b = c("T5", "T6", "T7"))
    
    p[[1]]$data <- do.call(rbind, lapply(p, function(x) x$data))
    
    p <- p[[1]] + aes(color = Class, group = Class)
    
    p
    

    enter image description here