Skip to content

Commit

Permalink
spurious(?) viewport-too-small error with new ggplot2 version 3.5.0 (#…
Browse files Browse the repository at this point in the history
…328)

* spurious(?) viewport-too-small error with new ggplot2 version 3.5.0

* version

* fix
  • Loading branch information
strengejacke committed Mar 4, 2024
1 parent 73126f2 commit 4736d08
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions R/plot.check_heteroscedasticity.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) {
stats::rstandard(model)
},
error = function(e) {
# debugging
if (getOption("easystats_erros", FALSE)) {
insight::format_error(e$message)
}
NULL
}
)
Expand Down
32 changes: 15 additions & 17 deletions R/plot.estimate_density.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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))

Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -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)
}


Expand Down Expand Up @@ -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)) +
Expand All @@ -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) {
Expand Down
16 changes: 7 additions & 9 deletions R/plot.point_estimates.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))) {
Expand Down
10 changes: 7 additions & 3 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -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("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."
Expand Down

0 comments on commit 4736d08

Please sign in to comment.