Skip to content

Commit a881aa3

Browse files
committed
switch from GGally/ggnet to ggnetwork
1 parent dc47905 commit a881aa3

File tree

5 files changed

+108
-24
lines changed

5 files changed

+108
-24
lines changed

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Imports:
2525
sna,
2626
intergraph,
2727
network,
28-
GGally,
28+
ggnetwork,
29+
scales,
2930
grid,
3031
ggplot2,
3132
mwcsr,
@@ -48,7 +49,7 @@ Suggests:
4849
License: MIT + file LICENCE
4950
Encoding: UTF-8
5051
LazyData: true
51-
RoxygenNote: 7.2.3
52+
RoxygenNote: 7.3.3
5253
VignetteBuilder: knitr
5354
URL: https://github.com/ctlab/gatom/
5455
BugReports: https://github.com/ctlab/gatom/issues

NAMESPACE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ export(saveModuleToPdf)
1616
export(saveModuleToXgmml)
1717
export(scoreGraph)
1818
import(BioNet)
19-
import(GGally)
2019
import(XML)
2120
import(data.table)
21+
import(ggnetwork)
22+
import(ggplot2)
2223
import(grid)
2324
import(htmltools)
2425
import(htmlwidgets)
@@ -27,3 +28,4 @@ import(plyr)
2728
import(shinyCyJS)
2829
importFrom(methods,is)
2930
importFrom(mwcsr,normalize_sgmwcs_instance)
31+
importFrom(scales,expand_range)

R/gatom-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
#' scored metabolic graph with atom topology (\code{\link{gsEx}}),
4545
#' and metabolic module (\code{\link{mEx}}).
4646
#'
47-
#' @docType package
4847
#' @name gatom
49-
NULL
48+
#' @keywords internal
49+
"_PACKAGE"
5050

5151
#' Example metabolic graph with atom topology.
5252
#'

R/io.R

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,90 @@
1313
#'
1414
#' @export
1515
#'
16-
#' @import GGally
16+
#' @import ggnetwork
17+
#' @importFrom scales expand_range
18+
#' @import ggplot2
1719
#' @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
2223
node_attrs <- getPdfModuleAttrs(module)$produce_node_attrs
2324
edge_attrs <- getPdfModuleAttrs(module)$produce_edge_attrs
2425

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)
39100
}
40101

41102
getPdfModuleAttrs <- function(module) {

man/gatom.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)