|
13 | 13 | #' |
14 | 14 | #' @export |
15 | 15 | #' |
16 | | -#' @import GGally |
| 16 | +#' @import ggnetwork |
| 17 | +#' @importFrom scales expand_range |
| 18 | +#' @import ggplot2 |
17 | 19 | #' @import igraph |
18 | | -saveModuleToPdf <- function(module, file, name=NULL, n_iter=100, force=1e-5) { |
19 | | - |
20 | | - pdflayout <- getModulePdfLayout(module, n_iter, force) |
21 | | - layout2 <- pdflayout$layout2 |
| 20 | +saveModuleToPdf <- function(module, file, name = NULL, n_iter = 100, force = 1e-5) { |
| 21 | + pdflayout <- getModulePdfLayout(module, n_iter, force) |
| 22 | + layout2 <- pdflayout$layout2 |
22 | 23 | node_attrs <- getPdfModuleAttrs(module)$produce_node_attrs |
23 | 24 | edge_attrs <- getPdfModuleAttrs(module)$produce_edge_attrs |
24 | 25 |
|
25 | | - pdf(file=file, width=pdflayout$gwidth, height=pdflayout$gheight) |
26 | | - plot(ggnet2(module, mode=layout2$layouts[[length(layout2$layouts)]], |
27 | | - layout.exp=0.3 * (60 / length(V(module))), |
28 | | - size=node_attrs$width, max_size=25, node.color=node_attrs$color, |
29 | | - node.label=V(module)$label, label.size=node_attrs$fontsize, label.color="grey13", |
30 | | - edge.size=edge_attrs$penwidth, edge.color=edge_attrs$color, |
31 | | - edge.label.fill=NA, |
32 | | - edge.label=E(module)$label, edge.label.size=edge_attrs$fontsize, |
33 | | - legend.size=0, legend.position="up") + |
34 | | - ggplot2::ggtitle(name) + |
35 | | - ggplot2::theme(plot.title= |
36 | | - ggplot2::element_text(size=max(c(node_attrs$fontsize, edge_attrs$fontsize)) * 5))) |
37 | | - dev.off() |
38 | | - return(invisible(NULL)) |
| 26 | + # below is ChatGPT-based rewrite from ggnet |
| 27 | + coords <- layout2$layouts[[length(layout2$layouts)]] |
| 28 | + coords <- as.matrix(coords) |
| 29 | + |
| 30 | + |
| 31 | + el <- igraph::as_edgelist(module, names = FALSE) |
| 32 | + net <- network::network( |
| 33 | + el, |
| 34 | + directed = igraph::is_directed(module), |
| 35 | + matrix.type = "edgelist" |
| 36 | + ) |
| 37 | + |
| 38 | + network::set.vertex.attribute(net, "node_size", node_attrs$width) |
| 39 | + network::set.vertex.attribute(net, "node_color", node_attrs$color) |
| 40 | + network::set.vertex.attribute(net, "node_label", V(module)$label) |
| 41 | + |
| 42 | + network::set.edge.attribute(net, "edge_size", edge_attrs$penwidth) |
| 43 | + network::set.edge.attribute(net, "edge_color", edge_attrs$color) |
| 44 | + network::set.edge.attribute(net, "edge_label", E(module)$label) |
| 45 | + |
| 46 | + net_df <- ggnetwork::ggnetwork( |
| 47 | + net, |
| 48 | + layout = coords, |
| 49 | + scale = FALSE |
| 50 | + ) |
| 51 | + |
| 52 | + # scaling the x as in ggnet |
| 53 | + layout_exp <- 0.3 * (60 / length(V(module))) |
| 54 | + |
| 55 | + x_range <- range(net_df$x, na.rm = TRUE) |
| 56 | + x_limits <- scales::expand_range(x_range, layout_exp / 2) |
| 57 | + |
| 58 | + |
| 59 | + p <- ggplot2::ggplot( |
| 60 | + net_df, |
| 61 | + ggplot2::aes(x = x, y = y, xend = xend, yend = yend) |
| 62 | + ) + |
| 63 | + ggnetwork::geom_edges( |
| 64 | + ggplot2::aes(size = edge_size, colour = edge_color), |
| 65 | + show.legend = FALSE |
| 66 | + ) + |
| 67 | + ggnetwork::geom_edgetext( |
| 68 | + ggplot2::aes(label = edge_label), |
| 69 | + size = edge_attrs$fontsize, |
| 70 | + colour = "grey13", |
| 71 | + fill = NA, # <-- transparent background |
| 72 | + show.legend = FALSE |
| 73 | + ) + |
| 74 | + ggnetwork::geom_nodes( |
| 75 | + ggplot2::aes(size = node_size, colour = node_color), |
| 76 | + show.legend = FALSE |
| 77 | + ) + |
| 78 | + ggnetwork::geom_nodetext( |
| 79 | + ggplot2::aes(label = node_label), |
| 80 | + size = node_attrs$fontsize, |
| 81 | + colour = "grey13" |
| 82 | + ) + |
| 83 | + ggplot2::scale_size_identity() + |
| 84 | + ggplot2::scale_colour_identity() + |
| 85 | + ggnetwork::theme_blank() + |
| 86 | + ggplot2::ggtitle(name) + |
| 87 | + ggplot2::theme( |
| 88 | + plot.title = ggplot2::element_text( |
| 89 | + size = max(c(node_attrs$fontsize, edge_attrs$fontsize)) * 5 |
| 90 | + ), |
| 91 | + legend.position = "none" |
| 92 | + ) + |
| 93 | + scale_x_continuous(breaks = NULL, limits = x_limits) + |
| 94 | + scale_y_continuous(breaks = NULL) |
| 95 | + |
| 96 | + ggsave(filename=file, plot=p, device="pdf", |
| 97 | + width = pdflayout$gwidth, height = pdflayout$gheight) |
| 98 | + |
| 99 | + invisible(NULL) |
39 | 100 | } |
40 | 101 |
|
41 | 102 | getPdfModuleAttrs <- function(module) { |
|
0 commit comments