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
@@ -1,7 +1,7 @@
Type: Package
Package: see
Title: Model Visualisation Toolbox for 'easystats' and 'ggplot2'
Version: 0.8.1
Version: 0.8.1.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
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
3 changes: 3 additions & 0 deletions 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
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
50 changes: 30 additions & 20 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 / lint

file=R/geom_from_list.R,line=115,col=1,[cyclocomp_linter] Functions should have cyclomatic complexity of less than 40, this has 54.
# 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") {

Check warning on line 192 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=192,col=3,[if_switch_linter] Prefer switch() statements over repeated if/else equality tests, e.g., switch(x, a = 1, b = 2) over if (x == "a") 1 else if (x == "b") 2.

Check warning on line 192 in R/geom_from_list.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/geom_from_list.R,line=192,col=3,[if_switch_linter] Prefer switch() statements over repeated if/else equality tests, e.g., switch(x, a = 1, b = 2) over if (x == "a") 1 else if (x == "b") 2.
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
50 changes: 24 additions & 26 deletions R/plot.check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
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 @@ -119,33 +119,31 @@
color = NA,
alpha = 0.15
) +
{
if (!is.null(ci_data)) {
list(
ggplot2::geom_linerange(
linewidth = size_line,
na.rm = TRUE
if (!is.null(ci_data)) {

Check warning on line 122 in R/plot.check_collinearity.R

View workflow job for this annotation

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

file=R/plot.check_collinearity.R,line=122,col=28,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 122 in R/plot.check_collinearity.R

View workflow job for this annotation

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

file=R/plot.check_collinearity.R,line=122,col=28,[unnecessary_nesting_linter] Reduce the nesting of this statement by removing the braces {}.

Check warning on line 122 in R/plot.check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_collinearity.R,line=122,col=28,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.

Check warning on line 122 in R/plot.check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_collinearity.R,line=122,col=28,[unnecessary_nesting_linter] Reduce the nesting of this statement by removing the braces {}.
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")
),
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
)
show.legend = FALSE
)
}
)
} +

Check warning on line 145 in R/plot.check_collinearity.R

View workflow job for this annotation

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

file=R/plot.check_collinearity.R,line=145,col=5,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.

Check warning on line 145 in R/plot.check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_collinearity.R,line=145,col=5,[brace_linter] Closing curly-braces should always be on their own line, unless they are followed by an else.
geom_point2(

Check warning on line 146 in R/plot.check_collinearity.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/plot.check_collinearity.R,line=146,col=4,[indentation_linter] Indentation should be 6 spaces but is 4 spaces.
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
Loading
Loading