Skip to content

Commit

Permalink
Removed the need to suggest tidytext by substituting scale_x_reordered.
Browse files Browse the repository at this point in the history
  • Loading branch information
moralapablo committed Jan 17, 2024
1 parent 16fc312 commit 2461519
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Imports:
Suggests:
keras, tensorflow, reticulate,
luz, torch,
cowplot, ggplot2, patchwork, tidytext,
cowplot, ggplot2, patchwork,
testthat (>= 3.0.0), vdiffr,
knitr, rmarkdown
LinkingTo:
Expand Down
12 changes: 7 additions & 5 deletions R/nn2poly_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ plot.nn2poly <- function(x, ..., n=NULL) {
stop("package 'ggplot2' is required for this functionality", call. = FALSE)
}

if (!requireNamespace("tidytext", quietly = TRUE)) {
stop("package 'tidytext' is required for this functionality", call. = FALSE)
}

if (!requireNamespace("patchwork", quietly = TRUE)) {
stop("package 'patchwork' is required for this functionality", call. = FALSE)
}
Expand Down Expand Up @@ -307,13 +303,19 @@ plot.nn2poly <- function(x, ..., n=NULL) {
new_x <- do.call(paste, c(list(all_df$name, sep = "___"), list(all_df$type)))
reorder_aux <- stats::reorder(new_x, all_df$value, FUN = mean, decreasing = TRUE)

# inspired by tidytext::scale_x_reordered and tidtytext::reorder_func
reorder_func <- function(x, sep = "___") {
reg <- paste0(sep, ".+$")
gsub(reg, "", x)
}


plot_all <- ggplot2::ggplot(all_df,
ggplot2::aes(x = reorder_aux,
y = .data$value,
fill = .data$sign)) +
ggplot2::geom_bar(stat = "identity", colour = "black", alpha = 1) +
tidytext::scale_x_reordered() +
ggplot2::scale_x_discrete(labels = reorder_func) +
ggplot2::facet_wrap(~type, scales = "free_x") +
cowplot::theme_half_open() +
ggplot2::labs(y = "Coefficient (absolute) values", x = "Variables or interactions") +
Expand Down
56 changes: 56 additions & 0 deletions man/plot.nn2poly.Rd

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

0 comments on commit 2461519

Please sign in to comment.