Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated stretch to factor x and y columns by default #99

Merged
merged 7 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Handle correlation of exactly zero or 1 in `network_plot()` (@s-scherrer, #89)

- Add `.order` argument to `stretch()` with options "default" and "alphabet" (@mattwarkentin, #99)

# corrr 0.4.2

- Updates to work with tibble 3.0.0 and dplyr 1.0.0
Expand Down
5 changes: 3 additions & 2 deletions R/cor_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ rplot.cor_df <- function(rdf,
shape = 16,
colours = c("indianred2", "white", "skyblue1"),
print_cor = FALSE,
colors) {
colors,
.order = "default") {

if (!missing(colors))
colours <- colors
Expand All @@ -123,7 +124,7 @@ rplot.cor_df <- function(rdf,
row_order <- rdf$rowname

# Convert data to relevant format for plotting
pd <- stretch(rdf, na.rm = TRUE)
pd <- stretch(rdf, na.rm = TRUE, .order = .order)
juliasilge marked this conversation as resolved.
Show resolved Hide resolved
pd$size = abs(pd$r)
pd$label = fashion(pd$r)

Expand Down
13 changes: 10 additions & 3 deletions R/output.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}

Expand Down
18 changes: 14 additions & 4 deletions R/reshape.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,6 +106,9 @@ focus_if.default <- function(x, .predicate, ..., mirror = FALSE) {
#' matrix diagonal) should be dropped? Will automatically be set to TRUE if
#' mirror is FALSE.
#' @param remove.dups Removes duplicate entries, without removing all NAs
#' @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 tbl with three columns (x and y variables, and their correlation)
#' @export
#' @examples
Expand All @@ -116,16 +119,23 @@ focus_if.default <- function(x, .predicate, ..., mirror = FALSE) {
#' x <- shave(x) # use shave to set upper triangle to NA and then...
#' stretch(x, na.rm = FALSE) # omit all NAs, therefore keeping each
#' # correlation only once.
stretch <- function(x, na.rm = FALSE, remove.dups = FALSE) {
stretch <- function(x, na.rm = FALSE, remove.dups = FALSE,
.order = c("default", "alphabet")) {
.order <- match.arg(.order)
UseMethod("stretch")
}

#' @export
stretch.cor_df <- function(x, na.rm = FALSE, remove.dups = FALSE) {
stretch.cor_df <- function(x, na.rm = FALSE, remove.dups = FALSE,
.order = c("default", "alphabet")) {
.order <- match.arg(.order)
if(remove.dups) x <- shave(x)
row_name <- x$rowname
x <- x[, colnames(x) != "rowname"]
tb <- imap_dfr(x, ~tibble(x = .y, y = row_name, r = .x))
if(.order == "default") {
juliasilge marked this conversation as resolved.
Show resolved Hide resolved
tb[,c("x", "y")] <- map_dfc(tb[,c("x", "y")], factor, levels = row_name)
}
if(na.rm) tb <- tb[!is.na(tb$r), ]
if(remove.dups) {
stretch_unique(tb)
Expand Down
4 changes: 2 additions & 2 deletions man/focus.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/network_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions man/rplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion man/stretch.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/stretch-keep.order_alphabet.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> str(stretch(d, .order = FALSE))
Error in match.arg(.order): 'arg' must be NULL or a character vector

6 changes: 6 additions & 0 deletions tests/testthat/stretch-keep.order_default.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
> str(stretch(d))
tibble [16 x 3] (S3: tbl_df/tbl/data.frame)
$ x: Factor w/ 4 levels "Sepal.Length",..: 1 1 1 1 2 2 2 2 3 3 ...
$ y: Factor w/ 4 levels "Sepal.Length",..: 1 2 3 4 1 2 3 4 1 2 ...
$ r: num [1:16] NA -0.112 0.871 0.817 -0.112 ...

9 changes: 8 additions & 1 deletion tests/testthat/test-stretch.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ test_that("Converts to proper structure", {
colnames(stretch(d)),
c("x", "y", "r")
)
verify_output("stretch-keep.order_default.txt",
str(stretch(d))
)
verify_output("stretch-keep.order_alphabet.txt",
str(stretch(d, .order = FALSE))
)

exp_res <-
tibble::tribble(
Expand All @@ -34,7 +40,8 @@ test_that("Converts to proper structure", {
"Petal.Width", "Petal.Length", 0.962865431402796,
"Petal.Width", "Petal.Width", NA
)
expect_equivalent(as.data.frame(stretch(d)), as.data.frame(exp_res))
expect_equivalent(as.data.frame(stretch(d, .order = "alphabet")),
as.data.frame(exp_res))
})

test_that("na.rm", {
Expand Down