diff --git a/NEWS.md b/NEWS.md index 38e03d1..dc7d1d3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ - Handle correlation of exactly zero or 1 in `network_plot()` (@s-scherrer, #89) +- Add `.order` argument to `rplot()` with options "default" and "alphabet" (@mattwarkentin, #99) + # corrr 0.4.2 - Updates to work with tibble 3.0.0 and dplyr 1.0.0 diff --git a/R/cor_df.R b/R/cor_df.R index 1afef5c..531532b 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -95,7 +95,7 @@ focus_if.cor_df <- function(x, .predicate, ..., mirror = FALSE) { to_keep <- map_lgl( x[, colnames(x) != "rowname"], .predicate, ... - ) + ) to_keep <- names(to_keep)[!is.na(to_keep) & to_keep] @@ -114,7 +114,10 @@ rplot.cor_df <- function(rdf, shape = 16, colours = c("indianred2", "white", "skyblue1"), print_cor = FALSE, - colors) { + colors, + .order = c("default", "alphabet")) { + + .order <- match.arg(.order) if (!missing(colors)) colours <- colors @@ -127,6 +130,11 @@ rplot.cor_df <- function(rdf, pd$size = abs(pd$r) pd$label = fashion(pd$r) + if(.order == "default") { + pd$x <- factor(pd$x, levels = row_order) + pd$y <- factor(pd$y, levels = rev(row_order)) + } + plot_ <- list( # Geoms geom_point(shape = shape), @@ -230,8 +238,8 @@ network_plot.cor_df <- function(rdf, scale_colour_gradientn(limits = c(-1, 1), colors = colours), # Plot the points geom_point(data = points, - aes(x, y), - size = 3, shape = 19, colour = "white"), + aes(x, y), + size = 3, shape = 19, colour = "white"), # Plot variable labels if (repel) ggrepel::geom_text_repel(data = points, aes(x, y, label = id), @@ -243,9 +251,9 @@ network_plot.cor_df <- function(rdf, fontface = 'bold', size = 5), # expand the axes to add space for curves expand_limits(x = c(min(points$x) - .1, - max(points$x) + .1), - y = c(min(points$y) - .1, - max(points$y) + .1) + max(points$x) + .1), + y = c(min(points$y) - .1, + max(points$y) + .1) ), # Theme and legends theme_void(), diff --git a/R/output.R b/R/output.R index cbd57ec..0dfc5a4 100644 --- a/R/output.R +++ b/R/output.R @@ -69,10 +69,15 @@ fashion.default <- function(x, decimals = 2, leading_zeros = FALSE, na_print = " #' #' @param rdf Correlation data frame (see \code{\link{correlate}}) or object #' that can be coerced to one (see \code{\link{as_cordf}}). -#' @param legend Boolean indicating whether a legend mapping the colors to the correlations should be displayed. +#' @param legend Boolean indicating whether a legend mapping the colors to the +#' correlations should be displayed. #' @param shape \code{\link{geom_point}} aesthetic. -#' @param print_cor Boolean indicating whether the correlations should be printed over the shapes. +#' @param print_cor Boolean indicating whether the correlations should be +#' printed over the shapes. #' @param colours,colors Vector of colors to use for n-color gradient. +#' @param .order Either "default", meaning x and y variables keep the same order +#' as the columns in \code{x}, or "alphabet", meaning the variables are +#' alphabetized. #' @return Plots a correlation data frame #' @export #' @examples @@ -90,7 +95,9 @@ rplot <- function(rdf, shape = 16, colours = c("indianred2", "white", "skyblue1"), print_cor = FALSE, - colors) { + colors, + .order = c("default", "alphabet")) { + .order <- match.arg(.order) UseMethod("rplot") } diff --git a/R/reshape.R b/R/reshape.R index 38ff362..e50f3c9 100644 --- a/R/reshape.R +++ b/R/reshape.R @@ -9,8 +9,8 @@ #' function, see \code{\link[dplyr]{select}}. #' #' @param x cor_df. See \code{\link{correlate}}. -#' @param ... One or more unquoted expressions separated by commas. Variable -#' names can be used as if they were positions in the data frame, so +#' @param ... One or more unquoted expressions separated by commas. Variable +#' names can be used as if they were positions in the data frame, so #' expressions like `x:y`` can be used to select a range of variables. #' @param .dots Use focus_ to do standard evaluations. See \code{\link[dplyr]{select}}. #' @param mirror Boolean. Whether to mirror the selected columns in the rows or @@ -32,7 +32,7 @@ focus <- function(x, ..., mirror = FALSE) { .dots = ..., ... = ..., mirror = mirror - ) + ) } #' Returns a correlation table with the selected fields only diff --git a/man/figures/README-combination-1.png b/man/figures/README-combination-1.png index 125e851..da3395e 100644 Binary files a/man/figures/README-combination-1.png and b/man/figures/README-combination-1.png differ diff --git a/man/focus.Rd b/man/focus.Rd index 4cc8a0f..5022d3e 100644 --- a/man/focus.Rd +++ b/man/focus.Rd @@ -12,8 +12,8 @@ focus_(x, ..., .dots, mirror) \arguments{ \item{x}{cor_df. See \code{\link{correlate}}.} -\item{...}{One or more unquoted expressions separated by commas. Variable -names can be used as if they were positions in the data frame, so +\item{...}{One or more unquoted expressions separated by commas. Variable +names can be used as if they were positions in the data frame, so expressions like `x:y`` can be used to select a range of variables.} \item{mirror}{Boolean. Whether to mirror the selected columns in the rows or diff --git a/man/network_plot.Rd b/man/network_plot.Rd index acef095..0f74271 100644 --- a/man/network_plot.Rd +++ b/man/network_plot.Rd @@ -21,7 +21,8 @@ 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}{Boolean indicating whether a legend mapping the colors to the +correlations should be displayed.} \item{colours, colors}{Vector of colors to use for n-color gradient.} diff --git a/man/rplot.Rd b/man/rplot.Rd index 7364864..7b20712 100644 --- a/man/rplot.Rd +++ b/man/rplot.Rd @@ -10,20 +10,27 @@ rplot( shape = 16, colours = c("indianred2", "white", "skyblue1"), print_cor = FALSE, - colors + colors, + .order = c("default", "alphabet") ) } \arguments{ \item{rdf}{Correlation data frame (see \code{\link{correlate}}) or object that can be coerced to one (see \code{\link{as_cordf}}).} -\item{legend}{Boolean indicating whether a legend mapping the colors to the correlations should be displayed.} +\item{legend}{Boolean indicating whether a legend mapping the colors to the +correlations should be displayed.} \item{shape}{\code{\link{geom_point}} aesthetic.} \item{colours, colors}{Vector of colors to use for n-color gradient.} -\item{print_cor}{Boolean indicating whether the correlations should be printed over the shapes.} +\item{print_cor}{Boolean indicating whether the correlations should be +printed over the shapes.} + +\item{.order}{Either "default", meaning x and y variables keep the same order +as the columns in \code{x}, or "alphabet", meaning the variables are +alphabetized.} } \value{ Plots a correlation data frame