Skip to content

Commit f6b190f

Browse files
added dataset, new function and incremented version
1 parent f5660ec commit f6b190f

File tree

12 files changed

+262
-14
lines changed

12 files changed

+262
-14
lines changed

DESCRIPTION

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: ig.degree.betweenness
22
Type: Package
33
Title: "Smith-Pittman Community Detection Algorithm for 'igraph' Objects (2024)"
4-
Version: 0.1.1
4+
Version: 0.2.0
55
Authors@R: c(
66
person(
77
"Benjamin",
@@ -42,7 +42,11 @@ Imports:
4242
igraphdata,
4343
rlist,
4444
BBmisc,
45-
qgraph
45+
qgraph,
46+
dplyr,
47+
tibble,
48+
tidyr,
49+
ggplot2
4650
Depends:
4751
R (>= 4.1.0)
4852
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(cluster_degree_betweenness)
4+
export(plot_node_degrees)
45
export(plot_simplified_edgeplot)
56
export(prep_unlabeled_graph)
67
import(igraphdata)

R/oncology_network.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#' @source Simulated data based on oncology clinical trial enrollment patterns.
1414
#' @name oncology_network
1515
#' @docType data
16-
#' @examples
17-
#' library(ig.degree.betweenness)
18-
#' data(oncology_network)
19-
#' plot_simplified_edgeplot(oncology_network)
16+
#' @usage
17+
#' oncology_network
2018
NULL

R/plot_node_degrees.R

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#' Visualize Node Degree Distribution in a Network Graph
2+
#'
3+
#' Generates a horizontal bar‐style plot of node degrees for an \code{igraph} network.
4+
#' For undirected graphs, it shows each node’s total degree.
5+
#' For directed graphs, it displays in‐degrees (as negative bars) alongside out‐degrees.
6+
#'
7+
#' @param graph An \code{igraph} object. Can be either directed or undirected.
8+
#'
9+
#' @return A \code{ggplot} object:
10+
#' \itemize{
11+
#' \item \strong{Undirected graphs:} A bar for each node showing its total degree.
12+
#' \item \strong{Directed graphs:} Split bars per node with negative values for in‐degree
13+
#' (pointing left) and positive values for out‐degree (pointing right).
14+
#' }
15+
#'
16+
#' @details
17+
#' This function computes:
18+
#' \describe{
19+
#' \item{Total degree}{Number of edges incident on each node (for undirected graphs).}
20+
#' \item{In‐degree}{Number of incoming edges per node (for directed graphs).}
21+
#' \item{Out‐degree}{Number of outgoing edges per node (for directed graphs).}
22+
#' }
23+
#' For directed graphs, in‐degrees are negated so that bars extend leftward,
24+
#' providing an immediate visual comparison to out‐degrees.
25+
#'
26+
#' Internally, it uses:
27+
#' \itemize{
28+
#' \item \code{igraph::degree()} to compute degrees,
29+
#' \item \code{dplyr} and \code{tidyr} for reshaping the data,
30+
#' \item \code{ggplot2} for plotting.
31+
#' }
32+
#'
33+
#' @section Customization:
34+
#' You can modify the returned \code{ggplot} with additional layers, themes, or labels.
35+
#' For example, to add a title or change colors:
36+
#' \preformatted{
37+
#' plot_node_degrees(g) +
38+
#' ggtitle("Degree Distribution") +
39+
#' scale_fill_manual(values = c("in_degree" = "steelblue", "out_degree" = "salmon"))
40+
#' }
41+
#' @examples
42+
#' library(ig.degree.betweenness)
43+
#' library(igraphdata)
44+
#' data("karate")
45+
#' data("oncology_network")
46+
#' plot_node_degrees(oncology_network)
47+
#' plot_node_degrees(karate)
48+
#' @export
49+
plot_node_degrees <- function(graph){
50+
# ensure available
51+
graph <- graph
52+
53+
# total degree
54+
all_degree <- tibble::enframe(
55+
igraph::degree(graph),
56+
name = "node",
57+
value = "degree"
58+
)
59+
60+
if (igraph::is_directed(graph)) {
61+
in_degree <- tibble::enframe(
62+
igraph::degree(graph, mode = "in"),
63+
name = "node",
64+
value = "in_degree"
65+
)
66+
out_degree <- tibble::enframe(
67+
igraph::degree(graph, mode = "out"),
68+
name = "node",
69+
value = "out_degree"
70+
)
71+
72+
df <- dplyr::full_join(in_degree, out_degree, by = "node") |>
73+
dplyr::full_join(all_degree, by = "node") |>
74+
dplyr::mutate(in_degree = - .data$in_degree) |>
75+
tidyr::pivot_longer(
76+
cols = c("in_degree","out_degree"),
77+
names_to = "name",
78+
values_to = "in-degree/out-degree"
79+
)
80+
81+
p <- ggplot2::ggplot(
82+
df,
83+
ggplot2::aes(
84+
y = stats::reorder(.data$node, .data$degree),
85+
x = .data$`in-degree/out-degree`,
86+
fill = .data$name
87+
)
88+
) +
89+
ggplot2::geom_col() +
90+
ggplot2::theme_minimal() +
91+
ggplot2::theme(
92+
axis.title.y = ggplot2::element_blank(),
93+
legend.title = ggplot2::element_blank(),
94+
legend.position = "bottom"
95+
)
96+
97+
} else {
98+
p <- ggplot2::ggplot(
99+
all_degree,
100+
ggplot2::aes(
101+
y = stats::reorder(.data$node, .data$degree),
102+
x = .data$degree
103+
)
104+
) +
105+
ggplot2::geom_col() +
106+
ggplot2::theme_minimal() +
107+
ggplot2::theme(axis.title.y = ggplot2::element_blank())
108+
}
109+
110+
p
111+
}

R/zzz.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
utils::globalVariables(c(".data"))

cran-comments.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
## R CMD check results
22

3-
0 errors | 0 warnings | 1 note
3+
0 errors | 0 warnings | 0 note
4+
5+
## For Version 0.2.0 CRAN submission
6+
7+
* Added the `oncology_network` dataset
8+
* Created `plot_node_degrees` function
9+
* Updated dependencies
410

511
## For Version 0.1.1 CRAN submission
612

7-
* Fix grep issue on subgraph identification.
8-
* Fix typo in cluster_degree_betweenness
13+
* Fix `grep` issue on subgraph identification.
14+
* Fix typo in `cluster_degree_betweenness`
915

1016
## For Version 0.1.0 CRAN submission
1117

data/oncology_network.rda

-249 Bytes
Binary file not shown.

man/oncology_network.Rd

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

man/plot_node_degrees.Rd

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Install relevant libraries
2+
# pkgs <- c("dplyr","tibble","tidyr","tidygraph",
3+
# "igraph","ig.degree.betweenness")
4+
# install.packages(pkgs)
5+
# Load data
6+
tuesdata <- tidytuesdayR::tt_load(2024, week = 44)
7+
monster_movie_genres <- tuesdata$monster_movie_genres
8+
9+
# Prepare data for adjacency matrix
10+
dummy_matrix <- monster_movie_genres |>
11+
dplyr::mutate(value = 1) |>
12+
tidyr::pivot_wider(
13+
id_cols = tconst,
14+
names_from = genres,
15+
values_from = value,
16+
values_fill = 0
17+
) |>
18+
dplyr::select(-tconst) |>
19+
as.matrix.data.frame()
20+
21+
# Construct adjacency matrix
22+
genre_adj_matrix <- t(dummy_matrix) %*% dummy_matrix
23+
# Setting self loops to zero
24+
diag(genre_adj_matrix) <- 0
25+
26+
# Construct the graph
27+
monster_movie_network <- genre_adj_matrix |>
28+
as.data.frame() |>
29+
tibble::rownames_to_column() |>
30+
tidyr::pivot_longer(cols = Comedy:War) |>
31+
dplyr::rename(c(from = rowname, to = name)) |>
32+
dplyr::rowwise() |>
33+
dplyr::mutate(combo = paste0(sort(c(from, to)), collapse = ",")) |>
34+
dplyr::arrange(combo) |>
35+
dplyr::distinct(combo, .keep_all = TRUE) |>
36+
dplyr::select(-combo) |>
37+
tidyr::uncount(value) |>
38+
tidygraph::as_tbl_graph() |>
39+
as.igraph()
40+
41+
# Resize nodes based on node degree
42+
43+
VS <- igraph::degree(monster_movie_network) * 0.1
44+
45+
ig.degree.betweenness::plot_simplified_edgeplot(
46+
monster_movie_network,
47+
vertex.size = VS,
48+
edge.arrow.size = 0.001
49+
)
50+
51+
52+
save(monster_movie_network, file = "monster_movie_network.rda")

0 commit comments

Comments
 (0)