From 5acfa64d7da729790a0e03117fd68d44eab351e2 Mon Sep 17 00:00:00 2001 From: Dominique Makowski Date: Fri, 2 Feb 2024 06:42:29 +0000 Subject: [PATCH] plot.n_factors: Add % of variance to plot (#314) * Add % of variance to n_factors plot * lintrs, news, desc * lintr * minor x-axis * Revert "minor x-axis" This reverts commit 4c12bbf81bc5e4b179f9904723674d53a9403941. * remotes * lintr * lintr * lintr * wordlist * lintr * use GH to avoid warning * fix * lintr * lintr * styler --------- Co-authored-by: Daniel --- DESCRIPTION | 2 +- NEWS.md | 7 +++ R/data_plot.R | 5 +- R/geom_binomdensity.R | 2 +- R/geom_from_list.R | 52 +++++++++------- R/plot.check_collinearity.R | 60 +++++++++--------- R/plot.check_heteroscedasticity.R | 24 ++++--- R/plot.check_model.R | 4 +- R/plot.check_normality.R | 2 +- R/plot.check_outliers.R | 12 ++-- R/plot.n_factors.R | 100 ++++++++++++++++++++++-------- R/plot.parameters_brms_meta.R | 64 +++++++++---------- R/utils.R | 8 +-- R/utils_add_prior_layer.R | 62 +++++++++--------- inst/WORDLIST | 1 + 15 files changed, 233 insertions(+), 172 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 6eceb404f..0fb89dc19 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -117,4 +117,4 @@ Config/Needs/website: rstudio/bslib, r-lib/pkgdown, easystats/easystatstemplate -Remotes: easystats/bayestestR +Remotes: easystats/parameters, easystats/bayestestR diff --git a/NEWS.md b/NEWS.md index 3971c6217..caaa42dc4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# see 0.8.2 + +## Minor Changes + +* `plot.n_factors()` now shows a dashed line over the bars, indicating the + cumulate explained variance by the number of factors. + # see 0.8.1 ## Major Changes diff --git a/R/data_plot.R b/R/data_plot.R index 8a54b9270..c88c21d5a 100644 --- a/R/data_plot.R +++ b/R/data_plot.R @@ -132,6 +132,9 @@ add_plot_attributes <- function(x) { if (!is.null(info$title)) { out[[length(out) + 1L]] <- ggplot2::labs(title = info$title) } + if (!is.null(info$subtitle)) { + out[[length(out) + 1L]] <- ggplot2::labs(subtitle = info$subtitle) + } out } @@ -176,7 +179,7 @@ add_plot_attributes <- function(x) { #' @keywords internal .dynGet <- function(x, - ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA), + ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE), minframe = 1L, inherits = FALSE) { x <- insight::safe_deparse(x) diff --git a/R/geom_binomdensity.R b/R/geom_binomdensity.R index e430baecb..ab951c513 100644 --- a/R/geom_binomdensity.R +++ b/R/geom_binomdensity.R @@ -62,7 +62,7 @@ geom_binomdensity <- function(data, # Other parameters data$.side <- ifelse(data[[y]] == y_levels[1], "top", "bottom") - data$.justification <- as.numeric(!(data[[y]] == y_levels[1])) + data$.justification <- as.numeric(data[[y]] != y_levels[1]) data$.scale <- .geom_binomdensity_scale(data, x, y, scale) # ggdist geom diff --git a/R/geom_from_list.R b/R/geom_from_list.R index 52a9186a4..9196e2c90 100644 --- a/R/geom_from_list.R +++ b/R/geom_from_list.R @@ -114,58 +114,68 @@ #' @export geom_from_list <- function(x, ...) { # Additional parameters ------------------------------------------------------ - args <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")] + arguments <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")] if (is.null(x$geom)) { return(NULL) } if (inherits(x$geom, "function")) { - return(do.call(x$geom, args)) + return(do.call(x$geom, args = arguments)) } if (x$geom %in% c("density_2d", "density_2d_filled", "density_2d_polygon")) { - if (!"contour" %in% names(args)) args$contour <- TRUE - if (!"contour_var" %in% names(args)) args$contour_var <- "density" + if (!"contour" %in% names(arguments)) arguments$contour <- TRUE + if (!"contour_var" %in% names(arguments)) arguments$contour_var <- "density" } # If they are not geoms, return immediately if (x$geom == "labs") { - return(do.call(ggplot2::labs, args)) + return(do.call(ggplot2::labs, args = arguments)) } if (x$geom == "guides") { - return(do.call(ggplot2::guides, args)) + return(do.call(ggplot2::guides, args = arguments)) } if (x$geom == "coord_flip") { - return(do.call(ggplot2::coord_flip, args)) + return(do.call(ggplot2::coord_flip, args = arguments)) } if (x$geom == "facet_wrap") { - return(do.call(ggplot2::facet_wrap, args)) + return(do.call(ggplot2::facet_wrap, args = arguments)) } if (x$geom == "facet_grid") { - return(do.call(ggplot2::facet_grid, args)) + return(do.call(ggplot2::facet_grid, args = arguments)) } if (x$geom == "smooth") { - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - if (!"method" %in% names(args)) args$method <- "loess" - if (!"formula" %in% names(args)) args$formula <- "y ~ x" - return(do.call(ggplot2::geom_smooth, args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + if (!"method" %in% names(arguments)) { + arguments$method <- "loess" + } + if (!"formula" %in% names(arguments)) { + arguments$formula <- "y ~ x" + } + return(do.call(ggplot2::geom_smooth, args = arguments)) } if (startsWith(x$geom, "scale_") || startsWith(x$geom, "theme") || startsWith(x$geom, "see_")) { - return(do.call(x$geom, args)) + return(do.call(x$geom, args = arguments)) } if (startsWith(x$geom, "ggside::")) { insight::check_if_installed("ggside") - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - return(do.call(eval(parse(text = x$geom)), args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + return(do.call(eval(parse(text = x$geom)), args = arguments)) } if (startsWith(x$geom, "ggraph::")) { insight::check_if_installed("ggraph") - if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) - return(do.call(eval(parse(text = x$geom)), args)) + if (!is.null(x$aes)) { + arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) + } + return(do.call(eval(parse(text = x$geom)), args = arguments)) } # Default parameters @@ -179,7 +189,7 @@ geom_from_list <- function(x, ...) { } # Default for violin - if (x$geom == "violin") { + if (x$geom == "violin") { # nolint stat <- "ydensity" position <- "dodge" } else if (x$geom == "boxplot") { @@ -212,7 +222,7 @@ geom_from_list <- function(x, ...) { # Aesthetics if ("aes" %in% names(x)) { - aes_list <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym)) + aes_list <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym)) } else { aes_list <- NULL } @@ -231,7 +241,7 @@ geom_from_list <- function(x, ...) { geom = x$geom, mapping = aes_list, data = x$data, - params = args, + params = arguments, show.legend = show.legend, ... ) diff --git a/R/plot.check_collinearity.R b/R/plot.check_collinearity.R index 594c556ec..5cf6f8d7c 100644 --- a/R/plot.check_collinearity.R +++ b/R/plot.check_collinearity.R @@ -69,11 +69,11 @@ plot.see_check_collinearity <- function(x, xlim <- nrow(x) if (ylim < 10) ylim <- 10 - if (!is.null(ci_data)) { - x <- cbind(x, ci_data) - } else { + if (is.null(ci_data)) { x$VIF_CI_low <- NA_real_ x$VIF_CI_high <- NA_real_ + } else { + x <- cbind(x, ci_data) } # make sure legend is properly sorted @@ -118,33 +118,33 @@ plot.see_check_collinearity <- function(x, fill = colors[3], color = NA, alpha = 0.15 - ) + - { - if (!is.null(ci_data)) { - list( - ggplot2::geom_linerange( - linewidth = size_line, - na.rm = TRUE - ), - ggplot2::geom_segment( - data = x[x$VIF_CI_high > ylim * 1.15, ], - mapping = aes( - x = .data$x, - xend = .data$x, - y = .data$y, - yend = .data$VIF_CI_high - ), - lineend = "round", - linejoin = "round", - arrow = ggplot2::arrow( - ends = "last", type = "closed", - angle = 20, length = ggplot2::unit(0.03, "native") - ), - show.legend = FALSE - ) - ) - } - } + + ) + + if (!is.null(ci_data)) { + p <- p + + ggplot2::geom_linerange( + linewidth = size_line, + na.rm = TRUE + ) + + ggplot2::geom_segment( + data = x[x$VIF_CI_high > ylim * 1.15, ], + mapping = aes( + x = .data$x, + xend = .data$x, + y = .data$y, + yend = .data$VIF_CI_high + ), + lineend = "round", + linejoin = "round", + arrow = ggplot2::arrow( + ends = "last", type = "closed", + angle = 20, length = ggplot2::unit(0.03, "native") + ), + show.legend = FALSE + ) + } + + p <- p + geom_point2( size = size_point, na.rm = TRUE diff --git a/R/plot.check_heteroscedasticity.R b/R/plot.check_heteroscedasticity.R index 53cd814c2..18c9b9260 100644 --- a/R/plot.check_heteroscedasticity.R +++ b/R/plot.check_heteroscedasticity.R @@ -24,21 +24,19 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) { faminfo <- insight::model_info(model) r <- tryCatch( - { - if (inherits(model, "merMod")) { - stats::residuals(model, scaled = TRUE) - } else if (inherits(model, c("glmmTMB", "MixMod"))) { - sigma <- if (faminfo$is_mixed) { - sqrt(insight::get_variance_residual(model)) - } else { - .sigma_glmmTMB_nonmixed(model, faminfo) - } - stats::residuals(model) / sigma - } else if (inherits(model, "glm")) { - stats::rstandard(model, type = "pearson") + if (inherits(model, "merMod")) { + stats::residuals(model, scaled = TRUE) + } else if (inherits(model, c("glmmTMB", "MixMod"))) { + sig <- if (faminfo$is_mixed) { + sqrt(insight::get_variance_residual(model)) } else { - stats::rstandard(model) + .sigma_glmmTMB_nonmixed(model, faminfo) } + stats::residuals(model) / sig + } else if (inherits(model, "glm")) { + stats::rstandard(model, type = "pearson") + } else { + stats::rstandard(model) }, error = function(e) { NULL diff --git a/R/plot.check_model.R b/R/plot.check_model.R index 29c13ff02..14e0a4930 100644 --- a/R/plot.check_model.R +++ b/R/plot.check_model.R @@ -209,9 +209,9 @@ plot.see_check_model <- function(x, pw <- plots(p, n_columns = n_columns) .safe_print_plots(pw, ...) invisible(pw) - } else { - return(p) } + + p } diff --git a/R/plot.check_normality.R b/R/plot.check_normality.R index 01c62233b..8aae47298 100644 --- a/R/plot.check_normality.R +++ b/R/plot.check_normality.R @@ -68,7 +68,7 @@ plot.see_check_normality <- function(x, alpha_level = alpha ) } else { - if (type == "qq") { + if (type == "qq") { # nolint model_info <- attributes(x)$model_info if (inherits(model, c("lme", "lmerMod", "merMod", "glmmTMB", "afex_aov", "BFBayesFactor"))) { res_ <- suppressMessages(sort(stats::residuals(model), na.last = NA)) diff --git a/R/plot.check_outliers.R b/R/plot.check_outliers.R index eae2b173f..8d8d0f334 100644 --- a/R/plot.check_outliers.R +++ b/R/plot.check_outliers.R @@ -41,9 +41,9 @@ plot.see_check_outliers <- function(x, ...) { type <- match.arg(type) influential_obs <- attributes(x)$influential_obs - methods <- attr(x, "methods", exact = TRUE) + outlier_methods <- attr(x, "methods", exact = TRUE) - if (type == "dots" && !is.null(influential_obs) && (is.null(methods) || length(methods) == 1)) { + if (type == "dots" && !is.null(influential_obs) && (is.null(outlier_methods) || length(outlier_methods) == 1)) { .plot_diag_outliers_new( influential_obs, show_labels = show_labels, @@ -52,12 +52,10 @@ plot.see_check_outliers <- function(x, dot_alpha_level = dot_alpha, colors = colors ) + } else if (length(outlier_methods) == 1) { + .plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance) } else { - if (length(methods == 1)) { - .plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance) - } else { - .plot_outliers_multimethod(x, rescale_distance) - } + .plot_outliers_multimethod(x, rescale_distance) } } diff --git a/R/plot.n_factors.R b/R/plot.n_factors.R index 71a967037..33ef1bff5 100644 --- a/R/plot.n_factors.R +++ b/R/plot.n_factors.R @@ -3,33 +3,47 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { s1 <- summary(x) if ("n_Factors" %in% names(x)) { - var <- "n_Factors" + variable <- "n_Factors" lab <- "factors" } else { - var <- "n_Clusters" + variable <- "n_Clusters" lab <- "clusters" } - s2 <- data.frame(n_Methods = rep(0, max(x[[var]]))) + s2 <- data.frame(n_Methods = rep(0, max(x[[variable]]))) if (type == "line") { - s1[[var]] <- as.factor(s1[[var]]) - s2[[var]] <- factor(1:max(x[[var]])) + s1[[variable]] <- as.factor(s1[[variable]]) + s2[[variable]] <- factor(1:max(x[[variable]])) } else { - s2[[var]] <- 1:max(x[[var]]) + s2[[variable]] <- 1:max(x[[variable]]) } - dataplot <- rbind(s1, s2[!s2[[var]] %in% s1[[var]], ]) + if ("Variance_Cumulative" %in% names(s1)) { + s2$Variance_Cumulative <- NA + } + + dataplot <- rbind(s1, s2[!s2[[variable]] %in% s1[[variable]], ]) + + # Add Variance explained + if ("Variance_Explained" %in% names(attributes(x))) { + dataplot$Variance_Cumulative <- NULL # Remove column and re add + dataplot <- merge( + dataplot, + attributes(x)$Variance_Explained[, c("n_Factors", "Variance_Cumulative")], + by = "n_Factors" + ) + } if (type == "line") { - dataplot$x <- factor(dataplot[[var]], levels = rev(sort(levels(dataplot[[var]])))) + dataplot$x <- factor(dataplot[[variable]], levels = rev(sort(levels(dataplot[[variable]])))) dataplot$group <- "0" dataplot$group[which.max(dataplot$n_Methods)] <- "1" } else if (type == "area") { - dataplot$x <- dataplot[[var]] + dataplot$x <- dataplot[[variable]] } else { - dataplot <- dataplot[order(dataplot[[var]]), ] - dataplot$x <- dataplot[[var]] + dataplot <- dataplot[order(dataplot[[variable]]), ] + dataplot$x <- dataplot[[variable]] dataplot$fill <- "Not-optimal" dataplot$fill[which.max(dataplot$n_Methods)] <- "Optimal" } @@ -37,17 +51,30 @@ data_plot.n_factors <- function(x, data = NULL, type = "bar", ...) { dataplot$y <- dataplot$n_Methods / sum(dataplot$n_Methods) rownames(dataplot) <- NULL + # Labels and titles ----------------------------------------------------- + n_max <- sum(dataplot$n_Methods) + axis_lab <- paste0("% of methods (out of ", n_max, ")") + + # Inverse xlab and ylab for line plot if (type == "line") { attr(dataplot, "info") <- list( - "ylab" = paste("Number of", lab), - "xlab" = "Consensus between methods", - "title" = paste("How many", lab, "to retain") + ylab = paste("Number of", lab), + xlab = axis_lab ) } else { attr(dataplot, "info") <- list( - "xlab" = paste("Number of", lab), - "ylab" = "Consensus between methods", - "title" = paste("How many", lab, "to retain") + xlab = paste("Number of", lab), + ylab = axis_lab + ) + } + # Title + + attr(dataplot, "info")$title <- paste("How many", lab, "to retain") + attr(dataplot, "info")$subtitle <- paste0("Number of ", lab, " considered optimal by various algorithm") + if ("Variance_Cumulative" %in% names(dataplot) && type != "line") { + attr(dataplot, "info")$subtitle <- paste0( + attr(dataplot, "info")$subtitle, + ". The dashed line represent the cumulative percentage of variance explained" ) } @@ -83,7 +110,10 @@ data_plot.n_clusters <- data_plot.n_factors #' data(mtcars) #' result <- n_factors(mtcars, type = "PCA") #' result +#' +#' plot(result) # type = "bar" by default #' plot(result, type = "line") +#' plot(result, type = "area") #' #' @export plot.see_n_factors <- function(x, @@ -99,14 +129,15 @@ plot.see_n_factors <- function(x, if (missing(size)) { size <- switch(type, - "bar" = 0.7, - "line" = 1, + bar = 0.7, + line = 1, 1 ) } + # Base plot if (type == "area") { - ggplot(x, aes(x = .data$x, y = .data$y)) + + p <- ggplot(x, aes(x = .data$x, y = .data$y)) + geom_area(fill = flat_colors("grey")) + geom_segment( aes( @@ -115,32 +146,49 @@ plot.see_n_factors <- function(x, y = 0, yend = max(.data$y) ), - color = flat_colors("red"), - linetype = "dashed" + color = flat_colors("red") ) + geom_point(aes(x = .data$x[which.max(.data$y)], y = max(.data$y)), color = flat_colors("red") ) + - scale_y_continuous(labels = .percents) + scale_x_continuous(breaks = 1:max(x$x)) + add_plot_attributes(x) } else if (type == "line") { - ggplot(x, aes(y = .data$x, x = .data$y, colour = .data$group)) + + p <- ggplot(x, aes(y = .data$x, x = .data$y, colour = .data$group)) + geom_segment(aes(x = 0, yend = .data$x, xend = .data$y), linewidth = size) + geom_point(size = 2 * size) + guides(colour = "none") + scale_x_continuous(labels = .percents) + scale_color_manual(values = unname(flat_colors(c("grey", "red")))) + add_plot_attributes(x) + # If line, return plot as variance explained cannot be added due to the horizontal orientation of the plot + return(p) } else { - ggplot(x, aes(x = .data$x, y = .data$y, fill = .data$fill)) + + p <- ggplot(x, aes(x = .data$x, y = .data$y, fill = .data$fill)) + geom_bar(stat = "identity", width = size) + guides(fill = "none") + - scale_y_continuous(labels = .percents) + add_plot_attributes(x) + scale_x_continuous(breaks = 1:max(x$x)) + scale_fill_manual(values = unname(flat_colors(c("grey", "red")))) } + + # Add variance explained + if ("Variance_Cumulative" %in% names(x)) { + x$Varex_scaled <- x$Variance_Cumulative * max(x$y) + p <- p + + geom_line( + data = x, + aes(x = .data$x, y = .data$Varex_scaled, group = 1), + linetype = "dashed" + ) + + scale_y_continuous( + labels = .percents, + sec.axis = sec_axis(~ . / max(x$y), name = "% of variance explained", labels = .percents) + ) + } else { + p <- p + scale_y_continuous(labels = .percents) + } + p } #' @export diff --git a/R/plot.parameters_brms_meta.R b/R/plot.parameters_brms_meta.R index 7558d83ad..34504d80e 100644 --- a/R/plot.parameters_brms_meta.R +++ b/R/plot.parameters_brms_meta.R @@ -19,51 +19,51 @@ data_plot.parameters_brms_meta <- function(x, data = NULL, normalize_height = TR } # summary - summary <- x[, 1:6] - summary$Parameter <- attributes(x)$cleaned_parameters - colnames(summary)[2] <- "Estimate" - summary$Estimate_CI <- sprintf( + datasummary <- x[, 1:6] + datasummary$Parameter <- attributes(x)$cleaned_parameters + colnames(datasummary)[2] <- "Estimate" + datasummary$Estimate_CI <- sprintf( "%.2f %s", - summary$Estimate, + datasummary$Estimate, insight::format_ci( - summary$CI_low, - summary$CI_high, + datasummary$CI_low, + datasummary$CI_high, ci = NULL, digits = 2, zap_small = TRUE ) ) - summary$Parameter <- factor(summary$Parameter, levels = rev(unique(summary$Parameter))) - colnames(summary)[match("Parameter", colnames(summary))] <- "Study" + datasummary$Parameter <- factor(datasummary$Parameter, levels = rev(unique(datasummary$Parameter))) + colnames(datasummary)[match("Parameter", colnames(datasummary))] <- "Study" - summary$x <- NA_real_ - summary$y <- NA_real_ - summary$Color <- "Study" - summary$Color[summary$Study == "Overall"] <- "Overall" + datasummary$x <- NA_real_ + datasummary$y <- NA_real_ + datasummary$Color <- "Study" + datasummary$Color[datasummary$Study == "Overall"] <- "Overall" if ("ROPE_low" %in% names(x) && "ROPE_high" %in% names(x)) { - attr(summary, "rope") <- c(x$ROPE_low[1], x$ROPE_high[1]) + attr(datasummary, "rope") <- c(x$ROPE_low[1], x$ROPE_high[1]) } dataplot <- dataplot[dataplot$Study != "tau", ] - summary <- summary[summary$Study != "tau", ] + datasummary <- datasummary[datasummary$Study != "tau", ] dataplot$Study <- droplevels(dataplot$Study) - summary$Study <- droplevels(summary$Study) + datasummary$Study <- droplevels(datasummary$Study) dataplot$Group <- "Study" dataplot$Group[dataplot$Study == "Overall"] <- "Overall" dataplot$Color <- "Study" dataplot$Color[dataplot$Study == "Overall"] <- "Overall" - attr(dataplot, "summary") <- summary + attr(dataplot, "summary") <- datasummary attr(dataplot, "info") <- list( - "xlab" = "Standardized Mean Difference", - "ylab" = NULL, - "legend_fill" = NULL, - "legend_color" = NULL, - "title" = "Bayesian Meta-Analysis" + xlab = "Standardized Mean Difference", + ylab = NULL, + legend_fill = NULL, + legend_color = NULL, + title = "Bayesian Meta-Analysis" ) class(dataplot) <- unique(c("data_plot", "see_parameters_brms_meta", class(dataplot))) @@ -150,9 +150,7 @@ plot.see_parameters_brms_meta <- function(x, ...) { # save model for later use model <- tryCatch( - { - .retrieve_data(x) - }, + .retrieve_data(x), error = function(e) { priors <- FALSE NULL @@ -164,7 +162,7 @@ plot.see_parameters_brms_meta <- function(x, x <- data_plot(x, data = model, normalize_height = normalize_height, ...) } - summary <- attributes(x)$summary + datasummary <- attributes(x)$summary rope <- attributes(summary)$rope p <- ggplot2::ggplot(x, mapping = ggplot2::aes(x = .data$x, y = .data$Study, height = .data$y)) @@ -192,7 +190,7 @@ plot.see_parameters_brms_meta <- function(x, alpha = posteriors_alpha ) + ggplot2::geom_errorbarh( - data = summary, + data = datasummary, mapping = ggplot2::aes( xmin = .data$CI_low, xmax = .data$CI_high, @@ -201,7 +199,7 @@ plot.see_parameters_brms_meta <- function(x, linewidth = size_line ) + ggplot2::geom_point( - data = summary, + data = datasummary, mapping = ggplot2::aes(x = .data$Estimate, color = .data$Color), size = size_point, fill = "white", @@ -212,12 +210,12 @@ plot.see_parameters_brms_meta <- function(x, theme_lucid() + ggplot2::scale_y_discrete() + ggplot2::scale_fill_manual(values = c( - "Study" = unname(metro_colors("light blue")), - "Overall" = unname(metro_colors("amber")) + Study = unname(metro_colors("light blue")), + Overall = unname(metro_colors("amber")) )) + ggplot2::scale_colour_manual(values = c( - "Study" = unname(metro_colors("light blue")), - "Overall" = unname(metro_colors("amber")) + Study = unname(metro_colors("light blue")), + Overall = unname(metro_colors("amber")) )) + ggplot2::guides(fill = "none", colour = "none") + add_plot_attributes(x) @@ -229,7 +227,7 @@ plot.see_parameters_brms_meta <- function(x, p <- p + ggplot2::geom_text( - data = summary, + data = datasummary, mapping = ggplot2::aes(label = .data$Estimate_CI, x = Inf), hjust = "inward", size = size_text diff --git a/R/utils.R b/R/utils.R index 788b05868..87cbf49ac 100644 --- a/R/utils.R +++ b/R/utils.R @@ -38,7 +38,7 @@ .clean_parameter_names <- function(params, grid = FALSE) { params <- unique(params) - labels <- params + parameter_labels <- params # clean parameters names params <- gsub("(b_|bs_|bsp_|bcs_)(.*)", "\\2", params, perl = TRUE) @@ -90,7 +90,7 @@ params <- gsub("(Zero-Inflated) (Random)", "(Random, Zero-Inflated)", params, fixed = TRUE) } - stats::setNames(params, labels) + stats::setNames(params, parameter_labels) } @@ -139,8 +139,8 @@ .remove_intercept <- function(x, column = "Parameter", show_intercept = FALSE) { if (!show_intercept) { - remove <- which(.is_intercept(x[[column]])) - if (length(remove)) x <- x[-remove, ] + to_remove <- which(.is_intercept(x[[column]])) + if (length(to_remove)) x <- x[-to_remove, ] } x } diff --git a/R/utils_add_prior_layer.R b/R/utils_add_prior_layer.R index 8c2dbab76..90b0dbbac 100644 --- a/R/utils_add_prior_layer.R +++ b/R/utils_add_prior_layer.R @@ -17,8 +17,8 @@ # limit xrange, to avoid overly wide plots x_range <- stats::median(dens$x) + 7 * stats::mad(dens$x) * c(-1, 1) - remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) - if (length(remove)) dens <- dens[-remove, ] + to_remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) + if (length(to_remove)) dens <- dens[-to_remove, ] # remove intercept from output, if requested .remove_intercept(dens, column = "Parameter", show_intercept) @@ -60,35 +60,33 @@ na.rm = TRUE ) } + } else if (isTRUE(show_ridge_line)) { + ggridges::geom_ridgeline( + data = dat, + mapping = aes( + x = .data$x, + y = as.factor(.data$Parameter), + height = .data$y, + group = as.factor(.data$Parameter), + fill = "Priors" + ), + alpha = priors_alpha, + na.rm = TRUE + ) } else { - if (isTRUE(show_ridge_line)) { - ggridges::geom_ridgeline( - data = dat, - mapping = aes( - x = .data$x, - y = as.factor(.data$Parameter), - height = .data$y, - group = as.factor(.data$Parameter), - fill = "Priors" - ), - alpha = priors_alpha, - na.rm = TRUE - ) - } else { - ggridges::geom_ridgeline( - data = dat, - mapping = aes( - x = .data$x, - y = as.factor(.data$Parameter), - height = .data$y, - group = as.factor(.data$Parameter), - fill = "Priors" - ), - alpha = priors_alpha, - color = NA, - na.rm = TRUE - ) - } + ggridges::geom_ridgeline( + data = dat, + mapping = aes( + x = .data$x, + y = as.factor(.data$Parameter), + height = .data$y, + group = as.factor(.data$Parameter), + fill = "Priors" + ), + alpha = priors_alpha, + color = NA, + na.rm = TRUE + ) } } } @@ -115,8 +113,8 @@ # limit xrange, to avoid overly wide plots x_range <- stats::median(dens$x) + 7 * stats::mad(dens$x) * c(-1, 1) - remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) - if (length(remove)) dens <- dens[-remove, ] + to_remove <- which(dens$x <= x_range[1] | dens$x >= x_range[2]) + if (length(to_remove)) dens <- dens[-to_remove, ] # remove intercept from output, if requested .remove_intercept(dens, column = "Parameter", show_intercept) diff --git a/inst/WORDLIST b/inst/WORDLIST index 76cfea105..7aaeeedee 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -43,6 +43,7 @@ brms characterisation codecov colour +cumulate datawizard de diavola