From 1e0f1eaf0d19caf605d4b8cfffba710326d73730 Mon Sep 17 00:00:00 2001 From: Daryn Ramsden Date: Sun, 8 May 2022 22:53:53 -0400 Subject: [PATCH 1/3] Adding an option for network plot to map the color range only to the range of correlations in the data and not to the full range from -1 to 1 --- NEWS.md | 1 + R/cor_df.R | 17 +++++++++++++---- R/output.R | 7 ++++++- man/colpair_map.Rd | 3 +-- man/dice.Rd | 1 - man/focus.Rd | 8 ++++---- man/network_plot.Rd | 9 ++++++--- man/rearrange.Rd | 4 ++-- man/shave.Rd | 4 ++-- man/stretch.Rd | 10 +++++----- 10 files changed, 40 insertions(+), 24 deletions(-) diff --git a/NEWS.md b/NEWS.md index fe9f957..2eb2da0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ - `correlate()` now removes non-numeric columns from data frame inputs (@thisisdaryn, #139). - `autoplot.cor_df()` method have been added fot quick generation of correlation chart. +- `network_plot()` now allows the user the option to map the color range to the range of correlations that are in the input `rdf` (@thisisdaryn, #158). # corrr 0.4.3 diff --git a/R/cor_df.R b/R/cor_df.R index b900143..bb57c58 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -178,11 +178,13 @@ rplot.cor_df <- function(rdf, #' @export network_plot.cor_df <- function(rdf, min_cor = .30, - legend = TRUE, + legend = c("full", "range", "none"), colours = c("indianred2", "white", "skyblue1"), repel = TRUE, curved = TRUE, colors) { + legend <- match.arg(legend) + if (min_cor < 0 || min_cor > 1) { rlang::abort("min_cor must be a value ranging from zero to one.") } @@ -260,6 +262,13 @@ network_plot.cor_df <- function(rdf, } } + if(legend %in% c("full", "none")){ + legend_range = c(-1, 1) + } + else if(legend == "range"){ + legend_range = c(min(rdf[row(rdf)!=col(rdf)]), + max(rdf[row(rdf)!=col(rdf)])) + } plot_ <- list( # For plotting paths if (curved) { @@ -284,7 +293,7 @@ network_plot.cor_df <- function(rdf, }, scale_alpha(limits = c(0, 1)), scale_size(limits = c(0, 1)), - scale_colour_gradientn(limits = c(-1, 1), colors = colours), + scale_colour_gradientn(limits = legend_range, colors = colours), # Plot the points geom_point( data = points, @@ -322,8 +331,8 @@ network_plot.cor_df <- function(rdf, # Theme and legends theme_void(), guides(size = "none", alpha = "none"), - if (legend) labs(colour = NULL), - if (!legend) theme(legend.position = "none") + if (legend != "none") labs(colour = NULL), + if (legend == "none") theme(legend.position = "none") ) ggplot() + plot_ diff --git a/R/output.R b/R/output.R index d280cca..8be3edb 100644 --- a/R/output.R +++ b/R/output.R @@ -125,6 +125,11 @@ rplot.default <- function(rdf, ...) { #' #' @param min_cor Number from 0 to 1 indicating the minimum value of #' correlations (in absolute terms) to plot. +#' @param legend Either "full", meaning the color range is mapped to correlation +#' values from -1 to 1 and a legend is displayed, or "range", meaning the color +#' range is mapped to the range of correlation values in \code{rdf}, or "none" +#' meaning the color range is mapped to the range -1 to 1 with no legend +#' displayed. #' @param colours,colors Vector of colors to use for n-color gradient. #' @param repel Should variable labels repel each other? If TRUE, text is added #' via \code{\link[ggrepel]{geom_text_repel}} instead of \code{\link[ggplot2]{geom_text}} @@ -141,7 +146,7 @@ rplot.default <- function(rdf, ...) { #' network_plot(x, min_cor = .7, colors = c("red", "green"), legend = TRUE) network_plot <- function(rdf, min_cor = .3, - legend = TRUE, + legend = c("full", "range", "none"), colours = c("indianred2", "white", "skyblue1"), repel = TRUE, curved = TRUE, diff --git a/man/colpair_map.Rd b/man/colpair_map.Rd index c4731e0..3f7590b 100644 --- a/man/colpair_map.Rd +++ b/man/colpair_map.Rd @@ -28,10 +28,9 @@ of its columns. The result is a correlation data frame (see colpair_map(mtcars, cov) ## Function to get the p-value from a t-test: -calc_p_value <- function(vec_a, vec_b){ +calc_p_value <- function(vec_a, vec_b) { t.test(vec_a, vec_b)$p.value } colpair_map(mtcars, calc_p_value) - } diff --git a/man/dice.Rd b/man/dice.Rd index 8ae25c0..3f36a16 100644 --- a/man/dice.Rd +++ b/man/dice.Rd @@ -17,5 +17,4 @@ Returns a correlation table with the selected fields only \examples{ dice(correlate(mtcars), mpg, wt, am) - } diff --git a/man/focus.Rd b/man/focus.Rd index 0ee46dc..87a4c6d 100644 --- a/man/focus.Rd +++ b/man/focus.Rd @@ -36,10 +36,10 @@ function, see \code{\link[dplyr]{select}}. \examples{ library(dplyr) x <- correlate(mtcars) -focus(x, mpg, cyl) # Focus on correlations of mpg and cyl with all other variables -focus(x, -disp, - mpg, mirror = TRUE) # Remove disp and mpg from columns and rows +focus(x, mpg, cyl) # Focus on correlations of mpg and cyl with all other variables +focus(x, -disp, -mpg, mirror = TRUE) # Remove disp and mpg from columns and rows x <- correlate(iris[-5]) -focus(x, -matches("Sepal")) # Focus on correlations of non-Sepal - # variables with Sepal variables. +focus(x, -matches("Sepal")) # Focus on correlations of non-Sepal +# variables with Sepal variables. } diff --git a/man/network_plot.Rd b/man/network_plot.Rd index 0f74271..d56c7d1 100644 --- a/man/network_plot.Rd +++ b/man/network_plot.Rd @@ -7,7 +7,7 @@ network_plot( rdf, min_cor = 0.3, - legend = TRUE, + legend = c("full", "range", "none"), colours = c("indianred2", "white", "skyblue1"), repel = TRUE, curved = TRUE, @@ -21,8 +21,11 @@ that can be coerced to one (see \code{\link{as_cordf}}).} \item{min_cor}{Number from 0 to 1 indicating the minimum value of correlations (in absolute terms) to plot.} -\item{legend}{Boolean indicating whether a legend mapping the colors to the -correlations should be displayed.} +\item{legend}{Either "full", meaning the color range is mapped to correlation +values from -1 to 1 and a legend is displayed, or "range", meaning the color +range is mapped to the range of correlation values in \code{rdf}, or "none" +meaning the color range is mapped to the range -1 to 1 with no legend +displayed.} \item{colours, colors}{Vector of colors to use for n-color gradient.} diff --git a/man/rearrange.Rd b/man/rearrange.Rd index fbd9993..5d2161f 100644 --- a/man/rearrange.Rd +++ b/man/rearrange.Rd @@ -27,6 +27,6 @@ together. x <- correlate(mtcars) rearrange(x) # Default settings -rearrange(x, method = "HC") # Different seriation method -rearrange(x, absolute = FALSE) # Not using absolute values for arranging +rearrange(x, method = "HC") # Different seriation method +rearrange(x, absolute = FALSE) # Not using absolute values for arranging } diff --git a/man/shave.Rd b/man/shave.Rd index cf01b92..01dea70 100644 --- a/man/shave.Rd +++ b/man/shave.Rd @@ -21,6 +21,6 @@ missing values. } \examples{ x <- correlate(mtcars) -shave(x) # Default; shave upper triangle -shave(x, upper = FALSE) # shave lower triangle +shave(x) # Default; shave upper triangle +shave(x, upper = FALSE) # shave lower triangle } diff --git a/man/stretch.Rd b/man/stretch.Rd index 3da5f70..20462c0 100644 --- a/man/stretch.Rd +++ b/man/stretch.Rd @@ -25,10 +25,10 @@ data frame. The term column is handled automatically. } \examples{ x <- correlate(mtcars) -stretch(x) # Convert all to long format -stretch(x, na.rm = TRUE) # omit NAs (diagonal in this case) +stretch(x) # Convert all to long format +stretch(x, na.rm = TRUE) # omit NAs (diagonal in this case) -x <- shave(x) # use shave to set upper triangle to NA and then... -stretch(x, na.rm = TRUE) # omit all NAs, therefore keeping each - # correlation only once. +x <- shave(x) # use shave to set upper triangle to NA and then... +stretch(x, na.rm = TRUE) # omit all NAs, therefore keeping each +# correlation only once. } From 3fe2c27c71a2e218fe29ba1501167cf8253c1b94 Mon Sep 17 00:00:00 2001 From: Julia Silge Date: Thu, 12 May 2022 21:39:56 -0600 Subject: [PATCH 2/3] Adjust documentation for new options --- R/output.R | 9 ++++----- man/network_plot.Rd | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/R/output.R b/R/output.R index 8be3edb..cc79718 100644 --- a/R/output.R +++ b/R/output.R @@ -125,11 +125,10 @@ rplot.default <- function(rdf, ...) { #' #' @param min_cor Number from 0 to 1 indicating the minimum value of #' correlations (in absolute terms) to plot. -#' @param legend Either "full", meaning the color range is mapped to correlation -#' values from -1 to 1 and a legend is displayed, or "range", meaning the color -#' range is mapped to the range of correlation values in \code{rdf}, or "none" -#' meaning the color range is mapped to the range -1 to 1 with no legend -#' displayed. +#' @param legend How should the colors and legend for the correlation values be +#' displayed? The options are "full" (the default) for -1 to 1 with a legend, +#' "range" for the range of correlation values in \code{rdf} with a legend, +#' or "none" for colors between -1 to 1 with no legend displayed. #' @param colours,colors Vector of colors to use for n-color gradient. #' @param repel Should variable labels repel each other? If TRUE, text is added #' via \code{\link[ggrepel]{geom_text_repel}} instead of \code{\link[ggplot2]{geom_text}} diff --git a/man/network_plot.Rd b/man/network_plot.Rd index d56c7d1..27757bf 100644 --- a/man/network_plot.Rd +++ b/man/network_plot.Rd @@ -21,11 +21,10 @@ that can be coerced to one (see \code{\link{as_cordf}}).} \item{min_cor}{Number from 0 to 1 indicating the minimum value of correlations (in absolute terms) to plot.} -\item{legend}{Either "full", meaning the color range is mapped to correlation -values from -1 to 1 and a legend is displayed, or "range", meaning the color -range is mapped to the range of correlation values in \code{rdf}, or "none" -meaning the color range is mapped to the range -1 to 1 with no legend -displayed.} +\item{legend}{How should the colors and legend for the correlation values be +displayed? The options are "full" (the default) for -1 to 1 with a legend, +"range" for the range of correlation values in \code{rdf} with a legend, +or "none" for colors between -1 to 1 with no legend displayed.} \item{colours, colors}{Vector of colors to use for n-color gradient.} From 80f7a16361e9c199d3a361f3bb9e6e0a175e6837 Mon Sep 17 00:00:00 2001 From: Daryn Ramsden Date: Sun, 15 May 2022 14:26:04 -0400 Subject: [PATCH 3/3] Update R/cor_df.R Co-authored-by: Julia Silge --- R/cor_df.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/cor_df.R b/R/cor_df.R index bb57c58..ad45fdb 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -183,7 +183,7 @@ network_plot.cor_df <- function(rdf, repel = TRUE, curved = TRUE, colors) { - legend <- match.arg(legend) + legend <- rlang::arg_match(legend) if (min_cor < 0 || min_cor > 1) { rlang::abort("min_cor must be a value ranging from zero to one.")