Skip to content

Commit

Permalink
fixed CRAN issues and removed dependencies (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
schochastics authored Sep 23, 2024
1 parent bfb4bb3 commit bf32fe9
Show file tree
Hide file tree
Showing 35 changed files with 1,246 additions and 392 deletions.
11 changes: 3 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: graphlayouts
Title: Additional Layout Algorithms for Network Visualizations
Version: 1.1.1
Version: 1.1.1.9000
Authors@R: person("David", "Schoch", email = "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2952-4812"))
Description: Several new layout algorithms to visualize networks are provided which are not part of 'igraph'.
Expand All @@ -13,19 +13,14 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Imports:
igraph,
igraph (>= 2.0.0),
Rcpp
Suggests:
oaqc,
testthat,
ggraph,
ggplot2,
knitr,
rmarkdown,
uwot
LinkingTo:
Rcpp,
RcppArmadillo
RoxygenNote: 7.3.0
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export(layout_with_sparse_stress)
export(layout_with_stress)
export(layout_with_stress3D)
export(layout_with_umap)
export(oaqc)
export(reorder_edges)
importFrom(Rcpp,sourceCpp)
useDynLib(graphlayouts, .registration = TRUE)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# graphlayouts 1.2.0

* ported relevant code from archived R package oaqc (#83)
* fixed igraph deprecation warnings and require igraph >= 2.0.0
* removed vignette and point to tutorial
* removed dependency of ggraph

# graphlayouts 1.1.1

* fixed bug in disconnected layouts #80
Expand Down
145 changes: 72 additions & 73 deletions R/annotate_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,46 @@
#' @return concentric circles around origin
#' @examples
#' library(igraph)
#' library(ggraph)

#' g <- sample_gnp(10,0.4)
#' g <- sample_gnp(10, 0.4)
#'
#' \dontrun{
#' ggraph(g,layout = "centrality",centrality = degree(g))+
#' draw_circle(use = "cent")+
#' geom_edge_link()+
#' geom_node_point(shape = 21,fill = "grey25",size = 5)+
#' theme_graph()+
#' coord_fixed()
#' library(ggraph)
#' ggraph(g, layout = "centrality", centrality = degree(g)) +
#' draw_circle(use = "cent") +
#' geom_edge_link() +
#' geom_node_point(shape = 21, fill = "grey25", size = 5) +
#' theme_graph() +
#' coord_fixed()
#' }
#' @export
#'

draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
}
if (!use %in% c("focus", "cent")) {
stop("use must be one of 'focus' or 'cent'")
}
if (use == "focus" && missing(max.circle)) {
stop("max.circle missing. Should be set to the max distance from focal node.")
}
dat <- data.frame()
if (use == "cent") {
for (d in seq(0, 100, 20) * 2) {
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d))
tmp[["grp"]] <- d
dat <- rbind(dat, tmp)
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
}
if (!use %in% c("focus", "cent")) {
stop("use must be one of 'focus' or 'cent'")
}
if (use == "focus" && missing(max.circle)) {
stop("max.circle missing. Should be set to the max distance from focal node.")
}
} else if (use == "focus") {
for (d in 1:max.circle) {
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d))
tmp[["grp"]] <- d
dat <- rbind(dat, tmp)
dat <- data.frame()
if (use == "cent") {
for (d in seq(0, 100, 20) * 2) {
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = d))
tmp[["grp"]] <- d
dat <- rbind(dat, tmp)
}
} else if (use == "focus") {
for (d in 1:max.circle) {
tmp <- as.data.frame(circleFun(center = c(0, 0), diameter = 2 * d))
tmp[["grp"]] <- d
dat <- rbind(dat, tmp)
}
}
}
circs <- ggplot2::geom_path(data = dat, ggplot2::aes_(x = ~x, y = ~y, group = ~grp), col = col, alpha = 0.5)
return(circs)
circs <- ggplot2::geom_path(data = dat, ggplot2::aes_(x = ~x, y = ~y, group = ~grp), col = col, alpha = 0.5)
return(circs)
}


Expand All @@ -62,62 +61,62 @@ draw_circle <- function(col = "#00BFFF", use = "focus", max.circle) {
#' @return annotated concentric circles around origin
#' @examples
#' library(igraph)
#' library(ggraph)
#'
#' g <- sample_gnp(10, 0.4)
#' \dontrun{
#' library(ggraph)
#' ggraph(g, layout = "centrality", centrality = closeness(g)) +
#' draw_circle(use = "cent") +
#' annotate_circle(closeness(g), pos = "bottom", format = "scientific") +
#' geom_edge_link() +
#' geom_node_point(shape = 21, fill = "grey25", size = 5) +
#' theme_graph() +
#' coord_fixed()
#' draw_circle(use = "cent") +
#' annotate_circle(closeness(g), pos = "bottom", format = "scientific") +
#' geom_edge_link() +
#' geom_node_point(shape = 21, fill = "grey25", size = 5) +
#' theme_graph() +
#' coord_fixed()
#' }
#' @export
#'
annotate_circle <- function(cent, col = "#00BFFF", format = "", pos = "top", text_size = 3) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
}
if (length(cent) == 1) {
cent <- seq(1, cent, 1)
dat_annot <- data.frame(y = seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
} else {
dat_annot <- data.frame(y = 100 - seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
}
vju <- 0
if (format == "scientific") {
dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE)
}
if (pos == "bottom") {
dat_annot[["y"]] <- -dat_annot[["y"]]
vju <- 1
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 needed for this function to work. Please install it.", call. = FALSE)
}
if (length(cent) == 1) {
cent <- seq(1, cent, 1)
dat_annot <- data.frame(y = seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
} else {
dat_annot <- data.frame(y = 100 - seq(0, 100, 20), x = 0, val = interpolate_cent(cent, seq(0, 100, 20)))
dat_annot[["val"]] <- round(dat_annot[["val"]], 8)
}
vju <- 0
if (format == "scientific") {
dat_annot[["val"]] <- format(dat_annot[["val"]], scientific = TRUE)
}
if (pos == "bottom") {
dat_annot[["y"]] <- -dat_annot[["y"]]
vju <- 1
}

circs <- ggplot2::geom_text(
data = dat_annot, ggplot2::aes_(x = ~x, y = ~y, label = ~val),
col = col, vjust = vju, size = text_size
)
return(circs)
circs <- ggplot2::geom_text(
data = dat_annot, ggplot2::aes_(x = ~x, y = ~y, label = ~val),
col = col, vjust = vju, size = text_size
)
return(circs)
}


# helper -----
circleFun <- function(center = c(0, 0), diameter = 1, npoints = 100) {
r <- diameter / 2
tt <- seq(0, 2 * pi, length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
r <- diameter / 2
tt <- seq(0, 2 * pi, length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}

interpolate_cent <- function(cent, x) {
a <- min(cent)
b <- max(cent)
alpha <- 100 / (b - a)
beta <- -100 / (b - a) * a
(x - beta) / alpha
a <- min(cent)
b <- max(cent)
alpha <- 100 / (b - a)
beta <- -100 / (b - a) * a
(x - beta) / alpha
}
31 changes: 15 additions & 16 deletions R/graph_manipulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#' @return manipulated graph
#' @examples
#' library(igraph)
#' library(ggraph)
#'
#' g <- sample_gnp(10, 0.5)
#' E(g)$attr <- 1:ecount(g)
Expand All @@ -24,21 +23,21 @@ NULL
#' @export

reorder_edges <- function(g, attr, desc = TRUE) {
if (!"name" %in% igraph::vertex_attr_names(g)) {
igraph::V(g)$name <- 1:igraph::vcount(g)
}
edges_df <- igraph::as_data_frame(g, what = "edges")
edges_df <- edges_df[order(edges_df[[attr]], decreasing = desc), ]
if (!"name" %in% igraph::vertex_attr_names(g)) {
igraph::V(g)$name <- 1:igraph::vcount(g)
}
edges_df <- igraph::as_data_frame(g, what = "edges")
edges_df <- edges_df[order(edges_df[[attr]], decreasing = desc), ]

vertices <- igraph::as_data_frame(g, what = "vertices")
vattrs <- igraph::vertex_attr_names(g)
idname <- which(vattrs == "name")
vertices <- vertices[, c(idname, setdiff(seq_along(vattrs), idname))]
vertices <- igraph::as_data_frame(g, what = "vertices")
vattrs <- igraph::vertex_attr_names(g)
idname <- which(vattrs == "name")
vertices <- vertices[, c(idname, setdiff(seq_along(vattrs), idname))]

gn <- igraph::graph_from_data_frame(
d = edges_df,
directed = igraph::is.directed(g),
vertices = vertices
)
gn
gn <- igraph::graph_from_data_frame(
d = edges_df,
directed = igraph::is_directed(g),
vertices = vertices
)
gn
}
2 changes: 1 addition & 1 deletion R/graphlayouts.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
#' \item `layout_as_dynamic()` for longitudinal network data
#' }
#'
#' A detailed tutorial can be found at <https://www.mr.schochastics.net/material/netVizR/>
#' A detailed tutorial can be found at <https://schochastics.github.io/netVizR/>
#' @keywords internal
"_PACKAGE"
16 changes: 6 additions & 10 deletions R/layout_backbone.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,13 @@ layout_as_backbone <- function(g, keep = 0.2, backbone = TRUE) {
if (igraph::ecount(g) == 0) {
stop("graph is empty")
}

if (!requireNamespace("oaqc", quietly = TRUE)) {
stop("oaqc is needed for this function to work. Please install it.", call. = FALSE)
}
if (igraph::any_multiple(g)) {
stop("backbone layout does not work with multiple edges.")
}
if (igraph::is_directed(g)) {
stop("backbone layout does not work with directed edges.")
}
if (any(igraph::is.loop(g))) {
if (igraph::any_loop(g)) {
stop("backbone layout does not work with loops.")
}

Expand All @@ -49,11 +45,11 @@ layout_as_backbone <- function(g, keep = 0.2, backbone = TRUE) {
}

# weighting ----
orbs <- oaqc::oaqc(igraph::get.edgelist(g, names = FALSE) - 1, non_ind_freq = T)
orbs <- oaqc(igraph::as_edgelist(g, names = FALSE) - 1, non_ind_freq = T)
e11 <- orbs$e_orbits_non_ind[, 11]

qu <- rep(0, igraph::vcount(g))
el <- igraph::get.edgelist(g, names = FALSE)
el <- igraph::as_edgelist(g, names = FALSE)
el <- cbind(el, e11)
for (e in seq_len(nrow(el))) {
qu[el[e, 1]] <- qu[el[e, 1]] + el[e, 3]
Expand Down Expand Up @@ -95,7 +91,7 @@ layout_as_backbone <- function(g, keep = 0.2, backbone = TRUE) {
#-------------------------------------------------------------------------------

umst <- function(g) {
el <- igraph::get.edgelist(g, names = FALSE)
el <- igraph::as_edgelist(g, names = FALSE)
el <- cbind(el, igraph::E(g)$weight)
el <- el[order(el[, 3], decreasing = TRUE), ]
el <- cbind(el, rank(-el[, 3]))
Expand Down Expand Up @@ -131,7 +127,7 @@ umst <- function(g) {


backbone_edges <- function(g, g_lay) {
tmp <- rbind(igraph::get.edgelist(g_lay), igraph::get.edgelist(g, names = FALSE))
tmp <- rbind(igraph::as_edgelist(g_lay), igraph::as_edgelist(g, names = FALSE))
which(duplicated(tmp)) - igraph::ecount(g_lay)
}

Expand All @@ -142,7 +138,7 @@ max_prexif_jaccard <- function(g) {
el_tbl <- igraph::as_data_frame(g, "edges")

N_ranks <- lapply(1:igraph::vcount(g), get_rank, el_tbl = el_tbl)
el <- igraph::get.edgelist(g, names = FALSE)
el <- igraph::as_edgelist(g, names = FALSE)
new_w <- reweighting(el - 1, N_ranks)
new_w
}
Expand Down
8 changes: 4 additions & 4 deletions R/layout_multilevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ layout_as_multilevel <- function(g, type = "all", FUN1, FUN2,
if (ignore_iso) {
iso1 <- which(igraph::degree(g1) == 0)
iso2 <- which(igraph::degree(g2) == 0)
g1 <- igraph::delete.vertices(g1, iso1)
g2 <- igraph::delete.vertices(g2, iso2)
g1 <- igraph::delete_vertices(g1, iso1)
g2 <- igraph::delete_vertices(g2, iso2)
}

if (is.null(params1)) {
Expand Down Expand Up @@ -136,7 +136,7 @@ layout_as_multilevel <- function(g, type = "all", FUN1, FUN2,

if (ignore_iso) {
iso1 <- which(igraph::degree(g1) == 0)
g1 <- igraph::delete.vertices(g1, iso1)
g1 <- igraph::delete_vertices(g1, iso1)
}

if (is.null(params1)) {
Expand Down Expand Up @@ -182,7 +182,7 @@ layout_as_multilevel <- function(g, type = "all", FUN1, FUN2,

if (ignore_iso) {
iso2 <- which(igraph::degree(g2) == 0)
g2 <- igraph::delete.vertices(g2, iso2)
g2 <- igraph::delete_vertices(g2, iso2)
}

if (is.null(params2)) {
Expand Down
Loading

0 comments on commit bf32fe9

Please sign in to comment.