From 633ce170bf0e6b4e5ba7513dbae092d74b96f975 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Mar 2024 21:32:29 +0100 Subject: [PATCH 1/3] spurious(?) viewport-too-small error with new ggplot2 version 3.5.0 --- R/plot.check_heteroscedasticity.R | 4 ++++ R/plot.estimate_density.R | 32 +++++++++++++++---------------- R/plot.point_estimates.R | 16 +++++++--------- R/plots.R | 10 +++++++--- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/R/plot.check_heteroscedasticity.R b/R/plot.check_heteroscedasticity.R index 18c9b9260..52d415d5d 100644 --- a/R/plot.check_heteroscedasticity.R +++ b/R/plot.check_heteroscedasticity.R @@ -39,6 +39,10 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) { stats::rstandard(model) }, error = function(e) { + # debugging + if (getOption(getOption("easystats_erros", FALSE))) { + insight::format_error(e$message) + } NULL } ) diff --git a/R/plot.estimate_density.R b/R/plot.estimate_density.R index ad431a8c6..bc2883360 100644 --- a/R/plot.estimate_density.R +++ b/R/plot.estimate_density.R @@ -23,7 +23,7 @@ data_plot.estimate_density <- function(x, # summary split_columns <- intersect(c("Parameter", "Effects", "Component"), colnames(dataplot)) datasplit <- split(dataplot, dataplot[split_columns]) - summary <- do.call(rbind, insight::compact_list(lapply(datasplit, function(i) { + my_summary <- do.call(rbind, insight::compact_list(lapply(datasplit, function(i) { if (length(i$x) > 0L) { Estimate <- as.numeric(bayestestR::point_estimate(i$x, centrality = centrality)) CI <- as.numeric(bayestestR::ci(i$x, ci = ci)) @@ -46,10 +46,10 @@ data_plot.estimate_density <- function(x, out }))) - summary$Parameter <- factor(summary$Parameter) - summary$Parameter <- factor(summary$Parameter, levels = levels(dataplot$Parameter)) + my_summary$Parameter <- factor(my_summary$Parameter) + my_summary$Parameter <- factor(my_summary$Parameter, levels = levels(dataplot$Parameter)) - attr(dataplot, "summary") <- summary + attr(dataplot, "summary") <- my_summary attr(dataplot, "info") <- list( xlab = "Values", ylab = "Density", @@ -112,9 +112,7 @@ plot.see_estimate_density <- function(x, ...) { # save model for later use model <- tryCatch( - { - .retrieve_data(x) - }, + .retrieve_data(x), error = function(e) { priors <- FALSE NULL @@ -139,7 +137,7 @@ plot.see_estimate_density <- function(x, params <- unique(x$y) # get labels - labels <- .clean_parameter_names(x$Parameter, grid = !is.null(n_columns)) + parameter_labels <- .clean_parameter_names(x$Parameter, grid = !is.null(n_columns)) # remove intercept from output, if requested x <- .remove_intercept(x, show_intercept = show_intercept) @@ -148,7 +146,7 @@ plot.see_estimate_density <- function(x, p <- ggplot(x, aes(x = .data$x, y = .data$y, color = .data$Parameter)) + geom_line(linewidth = size_line) + add_plot_attributes(x) + - scale_color_flat(labels = labels) + scale_color_flat(labels = parameter_labels) } else { p <- ggplot(x, aes(x = .data$x, y = .data$Parameter, height = .data$y)) @@ -184,13 +182,13 @@ plot.see_estimate_density <- function(x, scale_color_manual(values = unname(social_colors("grey"))) } - summary <- attributes(x)$summary - summary <- .remove_intercept(summary, show_intercept = show_intercept) - summary$y <- NA + my_summary <- attributes(x)$summary + my_summary <- .remove_intercept(my_summary, show_intercept = show_intercept) + my_summary$y <- NA p <- p + geom_errorbarh( - data = summary, + data = my_summary, mapping = aes( xmin = .data$CI_low, xmax = .data$CI_high, @@ -199,7 +197,7 @@ plot.see_estimate_density <- function(x, linewidth = size_line ) + geom_point( - data = summary, + data = my_summary, mapping = aes(x = .data$x, color = "Posterior"), size = size_point, fill = "white", @@ -213,7 +211,7 @@ plot.see_estimate_density <- function(x, if (length(unique(x$Parameter)) == 1 || isTRUE(stack)) { p <- p + scale_y_continuous(breaks = NULL, labels = NULL) } else { - p <- p + scale_y_discrete(labels = labels) + p <- p + scale_y_discrete(labels = parameter_labels) } @@ -252,7 +250,7 @@ plot.see_estimate_density_df <- function(x, size_line = 0.9, ...) { x$Parameter <- factor(x$Parameter, levels = rev(unique(x$Parameter))) - labels <- stats::setNames(levels(x$Parameter), levels(x$Parameter)) + parameter_labels <- stats::setNames(levels(x$Parameter), levels(x$Parameter)) if (stack) { p <- ggplot(x, aes(x = .data$x, y = .data$y, color = .data$Parameter)) + @@ -268,7 +266,7 @@ plot.see_estimate_density_df <- function(x, if (length(unique(x$Parameter)) == 1 || isTRUE(stack)) { p <- p + scale_y_continuous(breaks = NULL, labels = NULL) } else { - p <- p + scale_y_discrete(labels = labels) + p <- p + scale_y_discrete(labels = parameter_labels) } if (length(unique(x$Parameter)) == 1L) { diff --git a/R/plot.point_estimates.R b/R/plot.point_estimates.R index 1ebdfdb75..466cf3356 100644 --- a/R/plot.point_estimates.R +++ b/R/plot.point_estimates.R @@ -20,12 +20,10 @@ data_plot.point_estimate <- function(x, data = NULL, ...) { } data <- tryCatch( - { - if (!is.null(x$Parameter)) { - data[, x$Parameter, drop = FALSE] - } else { - data - } + if (!is.null(x$Parameter)) { + data[, x$Parameter, drop = FALSE] + } else { + data }, error = function(e) { data @@ -36,10 +34,10 @@ data_plot.point_estimate <- function(x, data = NULL, ...) { if (is.null(centrality)) centrality <- "all" dataplot <- lapply(colnames(data), function(i) { - dist <- data[[i]] + my_dist <- data[[i]] - pe <- bayestestR::point_estimate(dist, centrality = "all") - dat <- as.data.frame(stats::density(dist, n = 256)) + pe <- bayestestR::point_estimate(my_dist, centrality = "all") + dat <- as.data.frame(stats::density(my_dist, n = 256)) dat$group <- i if (any(centrality %in% c("all", "mean"))) { diff --git a/R/plots.R b/R/plots.R index ea52794db..701b61abd 100644 --- a/R/plots.R +++ b/R/plots.R @@ -99,14 +99,18 @@ plots <- function(..., .safe_print_plots <- function(pw, ...) { pw_drawn <- tryCatch(print(pw), error = function(e) e) + # for debugging, return original error + if (getOption(getOption("easystats_erros", FALSE))) { + insight::format_error(pw_drawn$message) + } if (inherits(pw_drawn, "simpleError")) { msg_display1 <- "\n- To fix this issue, please make the window larger." - msg_display3 <- "\n- If this still doesn't resolve your problems, you may check whether your apps are rescaled. On Windows, this can be done in the display settings (Start > Settings > System > Display, \"Scale and layout\"). Reduce the scaling and try again." - msg_display4 <- "\n- Finally, you can try to decrease the base font-size of your theme before plotting. Load `library(ggplot2)` and run: `theme_set(theme_classic(base_size = 6))`" + msg_display3 <- "\n- If this still doesn't resolve your problems, you may check whether your apps are rescaled. On Windows, this can be done in the display settings (Start > Settings > System > Display, \"Scale and layout\"). Reduce the scaling and try again." # nolint + msg_display4 <- "\n- Finally, you can try to decrease the base font-size of your theme before plotting. Load `library(ggplot2)` and run: `theme_set(theme_classic(base_size = 6))`" # nolint if (Sys.getenv("RSTUDIO") == "1") { msg <- "The RStudio 'Plots' window is too small to show this set of plots." - msg_display2 <- "\n- If this doesn't help, try to reset your zoom settings. In RStudio, go to Menu \"View > Actual Size\" and then retry." + msg_display2 <- "\n- If this doesn't help, try to reset your zoom settings. In RStudio, go to Menu \"View > Actual Size\" and then retry." # nolint } else { msg <- "The viewport is too small to show this set of plots." msg_display2 <- "\n- If this doesn't help, try to reset the zoom settings of your IDE and then retry." From d6c14affae83ae92db17b9a75e16a1cd91b0b6f2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Mar 2024 21:33:02 +0100 Subject: [PATCH 2/3] version --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 98584a0f8..b2d51ad1c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: see Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2' -Version: 0.8.2.3 +Version: 0.8.2.4 Authors@R: c(person(given = "Daniel", family = "Lüdecke", From 88c12013ee6fcc52f1d6db136ed2fdb0d7e7ef51 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Mar 2024 22:03:46 +0100 Subject: [PATCH 3/3] fix --- R/plot.check_heteroscedasticity.R | 2 +- R/plots.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/plot.check_heteroscedasticity.R b/R/plot.check_heteroscedasticity.R index 52d415d5d..4810537b7 100644 --- a/R/plot.check_heteroscedasticity.R +++ b/R/plot.check_heteroscedasticity.R @@ -40,7 +40,7 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) { }, error = function(e) { # debugging - if (getOption(getOption("easystats_erros", FALSE))) { + if (getOption("easystats_erros", FALSE)) { insight::format_error(e$message) } NULL diff --git a/R/plots.R b/R/plots.R index 701b61abd..42db9555b 100644 --- a/R/plots.R +++ b/R/plots.R @@ -100,7 +100,7 @@ plots <- function(..., .safe_print_plots <- function(pw, ...) { pw_drawn <- tryCatch(print(pw), error = function(e) e) # for debugging, return original error - if (getOption(getOption("easystats_erros", FALSE))) { + if (getOption("easystats_erros", FALSE)) { insight::format_error(pw_drawn$message) } if (inherits(pw_drawn, "simpleError")) {