diff options
| author | Chris Sobczak <chris@sobczak.family> | 2026-06-12 17:41:21 -0700 |
|---|---|---|
| committer | Chris Sobczak <chris@sobczak.family> | 2026-06-12 17:41:21 -0700 |
| commit | 48fe2c177b746c4a4957f7aa6cbec1cb50cf22b4 (patch) | |
| tree | 842c3a7067b47795ef1688303f38e9c6ad81f32c | |
| parent | 27bffa864ac6ebfa9bc5ad4c577d5da02cd3bac4 (diff) | |
Create networkplot
| -rw-r--r-- | .Rbuildignore | 1 | ||||
| -rw-r--r-- | DESCRIPTION | 3 | ||||
| -rw-r--r-- | NAMESPACE | 5 | ||||
| -rw-r--r-- | R/corplot.R | 3 | ||||
| -rw-r--r-- | R/networkplot.R | 99 | ||||
| -rw-r--r-- | man/networkplot.Rd | 28 | ||||
| -rw-r--r-- | man/saveplot.Rd | 34 |
7 files changed, 134 insertions, 39 deletions
diff --git a/.Rbuildignore b/.Rbuildignore index 2a2cb83..d74dc1d 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,2 +1,3 @@ ^LICENSE\.md$ ^README\.Rmd$ +^R/saveplot\.R$ diff --git a/DESCRIPTION b/DESCRIPTION index 0152b88..186479f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,8 @@ Roxygen: list(markdown = TRUE) RoxygenNote: 8.0.0.9000 Imports: ggplot2, - ragg, + ggraph, + igraph, reshape2 Suggests: testthat (>= 3.0.0) @@ -2,7 +2,8 @@ export(corplot) export(gg_csobczak) -export(saveplot) +export(networkplot) import(ggplot2) -import(ragg) +import(ggraph) +import(igraph) importFrom(reshape2,melt) diff --git a/R/corplot.R b/R/corplot.R index 4a9335e..08221a4 100644 --- a/R/corplot.R +++ b/R/corplot.R @@ -1,4 +1,3 @@ -utils::globalVariables(c('i', 'j', 'value')) #' Correlation Plot (Upper Triangle) #' #' Minimal ggplot2 correlation plot showing the upper triangle @@ -51,7 +50,7 @@ corplot <- function(R, title = NULL){ # And plot it p <- ggplot2::ggplot( data = df, - ggplot2::aes(x = j, y = i, fill = value) + ggplot2::aes(x = .data$j, y = .data$i, fill = .data$value) ) + ggplot2::geom_tile() + ggplot2::scale_fill_gradient2( diff --git a/R/networkplot.R b/R/networkplot.R new file mode 100644 index 0000000..cb6aad8 --- /dev/null +++ b/R/networkplot.R @@ -0,0 +1,99 @@ +#' Network Plot +#' +#' Draw an undirected graph from a square matrix +#' where each variable is a node and the pairwise +#' entries are the strength of the relationship +#' (ex: correlation, partial correlation) +#' +#' @param R A square numeric matrix with names columns and rows +#' @param title Optional plot title +#' +#' @return A ggraph object +#' +#' @examples +#' X <- matrix(data = rnorm(100), nrow = 10) +#' R <- cor(X) +#' networkplot(R, title = 'Example Correlation Plot') +#' +#' @import ggraph +#' @import igraph +#' +#' @export +networkplot <- function(R, title = NULL){ + Rm <- tryCatch({ + if(is.matrix(R)){ + if(nrow(R) != ncol(R)) stop('R must be square') + Rm <- R + }else if(is.vector(R)){ + m <- length(R) + n <- (1 + sqrt(1 + 8 * m)) / 2 + if(n != floor(n)) stop('Vector length not compatible with upper triangle') + n <- as.integer(n) + Rm <- matrix(0, n, n) + Rm[upper.tri(Rm)] <- R + Rm <- Rm + t(Rm) + diag(Rm) <- 1 + }else{ + Rm <- as.matrix(R) + if(nrow(Rm) != ncol(Rm)) stop('R must be square') + } + + if(is.null(rownames(Rm))){ + rownames(Rm) <- as.character(seq_len(nrow(Rm))) + } + + if(is.null(colnames(Rm))){ + colnames(Rm) <- as.character(seq_len(ncol(Rm))) + } + + Rm + }, error = function(e){ + stop(paste('Invalid input for networkplot:', e$message)) + }) + + idx <- which(upper.tri(Rm), arr.ind = TRUE) + edges <- data.frame( + from = rownames(Rm)[idx[,1]], + to = colnames(Rm)[idx[,2]], + r = Rm[idx] + ) + g <- igraph::graph_from_data_frame( + edges, + directed = FALSE + ) + + p <- ggraph::ggraph(graph = g, layout = 'circle') + + ggraph::geom_edge_arc( + ggplot2::aes( + edge_alpha = abs(.data$r), + edge_color = .data$r + ), + strength = 0.5 + ) + + ggraph::scale_edge_color_gradient2( + low = 'red', + mid = 'white', + high = 'blue', + midpoint = 0, + limits = c(-1, 1), + guide = 'none' + ) + + ggraph::geom_node_point(size = 3) + + ggraph::geom_node_text( + ggplot2::aes(label = .data$name), + repel = TRUE, + size = 3 + ) + + ggplot2::theme_void() + + ggplot2::theme(legend.position = 'none') + + if(!is.null(title)){ + p <- p + + ggplot2::ggtitle(title) + + ggplot2::theme( + plot.title = ggplot2::element_text(hjust = 0.5) + ) + } + + return(p) +} diff --git a/man/networkplot.Rd b/man/networkplot.Rd new file mode 100644 index 0000000..055bc46 --- /dev/null +++ b/man/networkplot.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/networkplot.R +\name{networkplot} +\alias{networkplot} +\title{Network Plot} +\usage{ +networkplot(R, title = NULL) +} +\arguments{ +\item{R}{A square numeric matrix with names columns and rows} + +\item{title}{Optional plot title} +} +\value{ +A ggraph object +} +\description{ +Draw an undirected graph from a square matrix +where each variable is a node and the pairwise +entries are the strength of the relationship +(ex: correlation, partial correlation) +} +\examples{ +X <- matrix(data = rnorm(100), nrow = 10) +R <- cor(X) +networkplot(R, title = 'Example Correlation Plot') + +} diff --git a/man/saveplot.Rd b/man/saveplot.Rd deleted file mode 100644 index 170e077..0000000 --- a/man/saveplot.Rd +++ /dev/null @@ -1,34 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/saveplot.R -\name{saveplot} -\alias{saveplot} -\title{Save a ggplot object} -\usage{ -saveplot(p, filename, units = "in", width = 10, height = 10, dpi = 600) -} -\arguments{ -\item{p}{ggplot object you want to save} - -\item{filename}{The name of the file you want to export to} - -\item{units}{The units for dimensions of the plot (default inches)} - -\item{width}{Width of the plot in units (default 10)} - -\item{height}{Height of the plot in units (default 10)} - -\item{dpi}{Dots per inch (detault 300)} -} -\value{ -No return value -} -\description{ -Standard method for saving ggplot objects for best -quality for the desired use case -} -\examples{ -df <- data.frame(x = rnorm(100)) -p <- ggplot2::ggplot(data = df, ggplot2::aes(x = x)) + ggplot2::geom_histogram() -saveplot(p, filename = 'plot.png') - -} |
