From ab056fc6b0e09dba30c3f57f47b654e493e1e90f Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 18:35:01 +0000 Subject: [PATCH 1/7] Make network_plot work with 2 variables. Close #118 --- R/cor_df.R | 9 +++++++-- tests/testthat/test-plots.R | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/R/cor_df.R b/R/cor_df.R index 0d17de4..4d15cd4 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -192,8 +192,13 @@ network_plot.cor_df <- function(rdf, rdf <- as_matrix(rdf, diagonal = 1) distance <- 1 - abs(rdf) - # Use multidimensional Scaling to obtain x and y coordinates for points. - points <- suppressWarnings(stats::cmdscale(distance)) + points <- if (ncol(rdf) == 2) { + # 2 vars: 2 opposing points + matrix(c(0, -0.1, 0, 0.1), ncol = 2, dimnames = list(colnames(rdf))) + } else { + # More than 2 vars: multidimensional scaling to obtain x and y coordinates for points. + suppressWarnings(stats::cmdscale(distance, k = 2)) + } if(ncol(points) < 2){ diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index 12265ae..9fc8b30 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -2,16 +2,24 @@ d <- datasets::anscombe[, 1:7] d[1, 1] <- NA d <- correlate(d) -context("network") +context("network_plot") test_that("Network plot works", { expect_s3_class(network_plot(d), "ggplot") expect_s3_class(network_plot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot") }) + +test_that("Network plot works with 2 variables", { + d2 <- correlate(datasets::anscombe[c("x1", "y1")]) + + expect_s3_class(network_plot(d2), "ggplot") + expect_s3_class(network_plot(d2, colors = c("indianred2", "white", "skyblue1")), "ggplot") +}) + context("rplot") -test_that("Network plot works", { +test_that("rplot works", { expect_s3_class(rplot(d), "ggplot") expect_s3_class(rplot(d, colors = c("indianred2", "white", "skyblue1")), "ggplot") }) From 170e5717ec0119de152608936aa4f86d97252229 Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 18:43:14 +0000 Subject: [PATCH 2/7] Fix subsetting that failed on single column df. Close #119 --- R/cor_df.R | 6 +++--- R/reshape.R | 2 +- R/utility.R | 2 +- tests/testthat/test-as_cordf.R | 6 ++++++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/R/cor_df.R b/R/cor_df.R index 4d15cd4..637c94c 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -5,7 +5,7 @@ as_matrix.cor_df <- function(x, diagonal) { # Separate rownames row_name <- x$rowname - x <- x[, colnames(x) != "rowname"] + x <- x[colnames(x) != "rowname"] # Convert to matrix and set rownames class(x) <- "data.frame" x <- as.matrix(x) @@ -22,7 +22,7 @@ shave.cor_df <- function(x, upper = TRUE) { # Separate rownames row_name <- x$rowname - x <- x[, colnames(x) != "rowname"] + x <- x[colnames(x) != "rowname"] # Remove upper matrix if (upper) { @@ -93,7 +93,7 @@ focus_if.cor_df <- function(x, .predicate, ..., mirror = FALSE) { # Identify which variables to keep to_keep <- map_lgl( - x[, colnames(x) != "rowname"], + x[colnames(x) != "rowname"], .predicate, ... ) diff --git a/R/reshape.R b/R/reshape.R index e50f3c9..5577c26 100644 --- a/R/reshape.R +++ b/R/reshape.R @@ -124,7 +124,7 @@ stretch <- function(x, na.rm = FALSE, remove.dups = FALSE) { stretch.cor_df <- function(x, na.rm = FALSE, remove.dups = FALSE) { if(remove.dups) x <- shave(x) row_name <- x$rowname - x <- x[, colnames(x) != "rowname"] + x <- x[colnames(x) != "rowname"] tb <- imap_dfr(x, ~tibble(x = .y, y = row_name, r = .x)) if(na.rm) tb <- tb[!is.na(tb$r), ] if(remove.dups) { diff --git a/R/utility.R b/R/utility.R index efef515..d85cff9 100644 --- a/R/utility.R +++ b/R/utility.R @@ -20,7 +20,7 @@ as_cordf <- function(x, diagonal = NA) { } x <- as.data.frame(x) row_name <- x$rowname - x <- x[, colnames(x) != "rowname"] + x <- x[colnames(x) != "rowname"] rownames(x) <- row_name if(ncol(x) != nrow(x)) { stop("Input object x is not a square. ", diff --git a/tests/testthat/test-as_cordf.R b/tests/testthat/test-as_cordf.R index 3e4e0ca..34aab94 100644 --- a/tests/testthat/test-as_cordf.R +++ b/tests/testthat/test-as_cordf.R @@ -18,3 +18,9 @@ test_that("Diagonal sets correctly", { expect_equal(all(is.na(diag(as.matrix(as_cordf(d, diagonal = NA)[, -1])))), TRUE) expect_equal(all(diag(as.matrix(as_cordf(d, diagonal = 100)[, -1] == 100))), TRUE) }) + +test_that("as_cordf handles single correlation", { + d1 <- cor(mtcars["cyl"]) + expect_s3_class(as_cordf(d1), "cor_df") + expect_equal(colnames(as_cordf(d1)), c("rowname", colnames(d1))) +}) From ce1f5c112e67be2d832294216edd336aedc07528 Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 19:25:28 +0000 Subject: [PATCH 3/7] correlate() no longer sets the diagonal of 1x1 correlation matrix. As a side effect, correlate() now works with numeric vectors and one-column dfs. Close #120. Close #121. --- R/utility.R | 2 +- tests/testthat/test-correlate.R | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/R/utility.R b/R/utility.R index d85cff9..e09d70c 100644 --- a/R/utility.R +++ b/R/utility.R @@ -26,7 +26,7 @@ as_cordf <- function(x, diagonal = NA) { stop("Input object x is not a square. ", "The number of columns must be equal to the number of rows.") } - diag(x) <- diagonal + if (ncol(x) > 1) diag(x) <- diagonal new_cordf(x, names(x)) } diff --git a/tests/testthat/test-correlate.R b/tests/testthat/test-correlate.R index ea970f1..5062d56 100644 --- a/tests/testthat/test-correlate.R +++ b/tests/testthat/test-correlate.R @@ -18,3 +18,16 @@ test_that("Diagonal sets correctly", { expect_equal(all(is.na(diag(as.matrix(correlate(d, diagonal = NA)[, -1])))), TRUE) expect_equal(all(diag(as.matrix(correlate(d, diagonal = 100)[, -1] == 100))), TRUE) }) + + +test_that("correlate works with numeric vectors", { + expect_equal(correlate(x = 1:10, y = 1:10)[[2]], 1) + expect_equal(correlate(x = 1:10, y = -(1:10), diagonal = 0)[[2]], -1) +}) + +test_that("correlate works with a one-column data.frame", { + var <- "Sepal.Length" + expect_equal(correlate(datasets::iris[var])[[1]], var) + expect_equal(correlate(datasets::iris[var])[[2]], 1) + +}) From dabde2b7ed98ef9fb652daee2128871c18b54bff Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 19:30:40 +0000 Subject: [PATCH 4/7] network_plot() now works with a single variable. Close #118 again :) --- R/cor_df.R | 5 ++++- tests/testthat/test-plots.R | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/R/cor_df.R b/R/cor_df.R index 637c94c..7f26bbc 100644 --- a/R/cor_df.R +++ b/R/cor_df.R @@ -192,7 +192,10 @@ network_plot.cor_df <- function(rdf, rdf <- as_matrix(rdf, diagonal = 1) distance <- 1 - abs(rdf) - points <- if (ncol(rdf) == 2) { + points <- if (ncol(rdf) == 1) { + # 1 var: a single central point + matrix(c(0, 0), ncol = 2, dimnames = list(colnames(rdf))) + } else if (ncol(rdf) == 2) { # 2 vars: 2 opposing points matrix(c(0, -0.1, 0, 0.1), ncol = 2, dimnames = list(colnames(rdf))) } else { diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index 9fc8b30..4bb34f5 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -17,6 +17,12 @@ test_that("Network plot works with 2 variables", { expect_s3_class(network_plot(d2, colors = c("indianred2", "white", "skyblue1")), "ggplot") }) +test_that("Network plot works with 1 variable", { + d1 <- correlate(datasets::anscombe["x1"]) + expect_s3_class(network_plot(d1), "ggplot") + expect_s3_class(network_plot(d1, colors = c("indianred2", "white", "skyblue1")), "ggplot") +}) + context("rplot") test_that("rplot works", { From 0a4b33034f15af14c486979dfdf4dd3cd52ba19d Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 19:38:10 +0000 Subject: [PATCH 5/7] Update NEWS and contributors --- DESCRIPTION | 4 +++- NEWS.md | 5 +++++ man/corrr-package.Rd | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d28923d..65727d4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,7 +18,9 @@ Authors@R: role = c("aut")), person(given = "Jorge", family = "Cimentada", - role = c("aut")) + role = c("aut")), + person(given = "Sachet", family = "Antoine", + email = "antoine.sac@gmail.com", role = "ctb") ) Depends: R (>= 3.3.0) diff --git a/NEWS.md b/NEWS.md index a3b0a2b..26446d5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,6 +8,11 @@ - Make `network_plot()` more robust, for example to highly correlated data (@thisisdaryn, #107) +- `correlate()` now works with single-column data.frames and numeric vectors (@antoine-sachet, #122) + +- `network_plot()` now works with `cor_df` objects with only 1 or 2 columns (@antoine-sachet, #122) + + # corrr 0.4.2 - Updates to work with tibble 3.0.0 and dplyr 1.0.0 diff --git a/man/corrr-package.Rd b/man/corrr-package.Rd index bfd5a5a..26204c3 100644 --- a/man/corrr-package.Rd +++ b/man/corrr-package.Rd @@ -33,5 +33,10 @@ Authors: \item Jorge Cimentada } +Other contributors: +\itemize{ + \item Sachet Antoine \email{antoine.sac@gmail.com} [contributor] +} + } \keyword{internal} From 1341e0ffc935c05b8f8800c699efda22ed962081 Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Wed, 28 Oct 2020 20:17:19 +0000 Subject: [PATCH 6/7] Make it clear in the NEWS that the diagonal is not set for 1x1 correlation matrices. --- NEWS.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 26446d5..2422c70 100644 --- a/NEWS.md +++ b/NEWS.md @@ -9,7 +9,8 @@ - Make `network_plot()` more robust, for example to highly correlated data (@thisisdaryn, #107) - `correlate()` now works with single-column data.frames and numeric vectors (@antoine-sachet, #122) - + Note the `diagonal` argument is ignored in these 2 cases. + - `network_plot()` now works with `cor_df` objects with only 1 or 2 columns (@antoine-sachet, #122) From 11a55640204aa1d4312da0b88a5c7d91d0a1b922 Mon Sep 17 00:00:00 2001 From: Antoine Sachet Date: Sun, 1 Nov 2020 19:40:08 +0000 Subject: [PATCH 7/7] Address PR feedback --- DESCRIPTION | 5 +---- NEWS.md | 3 +-- man/corrr-package.Rd | 5 ----- 3 files changed, 2 insertions(+), 11 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 65727d4..260988f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -18,10 +18,7 @@ Authors@R: role = c("aut")), person(given = "Jorge", family = "Cimentada", - role = c("aut")), - person(given = "Sachet", family = "Antoine", - email = "antoine.sac@gmail.com", role = "ctb") - ) + role = c("aut"))) Depends: R (>= 3.3.0) Imports: diff --git a/NEWS.md b/NEWS.md index 2422c70..a6a1c60 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,8 +8,7 @@ - Make `network_plot()` more robust, for example to highly correlated data (@thisisdaryn, #107) -- `correlate()` now works with single-column data.frames and numeric vectors (@antoine-sachet, #122) - Note the `diagonal` argument is ignored in these 2 cases. +- `correlate()` now works with single-column data.frames and numeric vectors (@antoine-sachet, #122). Note the `diagonal` argument is ignored in these 2 cases. - `network_plot()` now works with `cor_df` objects with only 1 or 2 columns (@antoine-sachet, #122) diff --git a/man/corrr-package.Rd b/man/corrr-package.Rd index 26204c3..bfd5a5a 100644 --- a/man/corrr-package.Rd +++ b/man/corrr-package.Rd @@ -33,10 +33,5 @@ Authors: \item Jorge Cimentada } -Other contributors: -\itemize{ - \item Sachet Antoine \email{antoine.sac@gmail.com} [contributor] -} - } \keyword{internal}