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

plot.n_factors: Add % of variance to plot #314

Merged
merged 18 commits into from
Feb 2, 2024
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,4 @@ Config/Needs/website:
rstudio/bslib,
r-lib/pkgdown,
easystats/easystatstemplate
Remotes: easystats/bayestestR
Remotes: easystats/parameters, easystats/bayestestR
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion R/data_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/geom_binomdensity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 31 additions & 21 deletions R/geom_from_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,60 +112,70 @@
#' geom_from_list(list(geom = "ggside::scale_xsidey_continuous", breaks = NULL))
#'
#' @export
geom_from_list <- function(x, ...) {

Check warning on line 115 in R/geom_from_list.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/geom_from_list.R,line=115,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 54 to at most 40.

Check warning on line 115 in R/geom_from_list.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/geom_from_list.R,line=115,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 54 to at most 40.
# 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
Expand All @@ -179,7 +189,7 @@
}

# Default for violin
if (x$geom == "violin") {
if (x$geom == "violin") { # nolint
stat <- "ydensity"
position <- "dodge"
} else if (x$geom == "boxplot") {
Expand Down Expand Up @@ -212,7 +222,7 @@

# 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
}
Expand All @@ -231,7 +241,7 @@
geom = x$geom,
mapping = aes_list,
data = x$data,
params = args,
params = arguments,
show.legend = show.legend,
...
)
Expand Down
60 changes: 30 additions & 30 deletions R/plot.check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions R/plot.check_heteroscedasticity.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).

Check warning on line 10 in R/plot.check_heteroscedasticity.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_heteroscedasticity.R,line=10,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 10 in R/plot.check_heteroscedasticity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_heteroscedasticity.R,line=10,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance")
#' m <- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
Expand All @@ -24,21 +24,19 @@

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
Expand Down
4 changes: 2 additions & 2 deletions R/plot.check_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).

Check warning on line 12 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_model.R,line=12,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 12 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=12,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance") && require("patchwork")
#' model <- lm(qsec ~ drat + wt, data = mtcars)
#' plot(check_model(model))
#'
#' @export
plot.see_check_model <- function(x,

Check warning on line 19 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_model.R,line=19,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 78 to at most 40.

Check warning on line 19 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=19,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 78 to at most 40.
style = theme_lucid,
colors = NULL,
type = c("density", "discrete_dots", "discrete_interval", "discrete_both"),
Expand All @@ -40,7 +40,7 @@
overdisp_type <- attr(x, "overdisp_type")
plot_type <- attr(x, "type")

if (missing(type) && !is.null(plot_type) && plot_type %in% c("density", "discrete_dots", "discrete_interval", "discrete_both")) {

Check warning on line 43 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_model.R,line=43,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.

Check warning on line 43 in R/plot.check_model.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_model.R,line=43,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 131 characters.
type <- plot_type
} else {
type <- match.arg(type)
Expand Down Expand Up @@ -209,9 +209,9 @@
pw <- plots(p, n_columns = n_columns)
.safe_print_plots(pw, ...)
invisible(pw)
} else {
return(p)
}

p
}


Expand Down
2 changes: 1 addition & 1 deletion R/plot.check_normality.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#'
#' @return A ggplot2-object.
#'
#' @seealso See also the vignette about [`check_model()`](https://easystats.github.io/performance/articles/check_model.html).

Check warning on line 27 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_normality.R,line=27,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.

Check warning on line 27 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_normality.R,line=27,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
#'
#' @examplesIf require("performance")
#' m <<- lm(mpg ~ wt + cyl + gear + disp, data = mtcars)
Expand Down Expand Up @@ -68,7 +68,7 @@
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))
Expand Down Expand Up @@ -235,7 +235,7 @@
)
},
ggplot2::geom_qq(
mapping = if (detrend) ggplot2::aes(y = ggplot2::after_stat(.data$sample) - ggplot2::after_stat(.data$theoretical)),

Check warning on line 238 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/plot.check_normality.R,line=238,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.

Check warning on line 238 in R/plot.check_normality.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_normality.R,line=238,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 124 characters.
shape = 16,
na.rm = TRUE,
stroke = 0,
Expand Down
12 changes: 5 additions & 7 deletions R/plot.check_outliers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand Down
Loading
Loading