Skip to content

Commit 5acfa64

Browse files
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 4c12bbf. * remotes * lintr * lintr * lintr * wordlist * lintr * use GH to avoid warning * fix * lintr * lintr * styler --------- Co-authored-by: Daniel <[email protected]>
1 parent d4b4eda commit 5acfa64

15 files changed

+233
-172
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ Config/Needs/website:
117117
rstudio/bslib,
118118
r-lib/pkgdown,
119119
easystats/easystatstemplate
120-
Remotes: easystats/bayestestR
120+
Remotes: easystats/parameters, easystats/bayestestR

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# see 0.8.2
2+
3+
## Minor Changes
4+
5+
* `plot.n_factors()` now shows a dashed line over the bars, indicating the
6+
cumulate explained variance by the number of factors.
7+
18
# see 0.8.1
29

310
## Major Changes

R/data_plot.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ add_plot_attributes <- function(x) {
132132
if (!is.null(info$title)) {
133133
out[[length(out) + 1L]] <- ggplot2::labs(title = info$title)
134134
}
135+
if (!is.null(info$subtitle)) {
136+
out[[length(out) + 1L]] <- ggplot2::labs(subtitle = info$subtitle)
137+
}
135138

136139
out
137140
}
@@ -176,7 +179,7 @@ add_plot_attributes <- function(x) {
176179

177180
#' @keywords internal
178181
.dynGet <- function(x,
179-
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA),
182+
ifnotfound = stop(gettextf("%s not found", sQuote(x)), domain = NA, call. = FALSE),
180183
minframe = 1L,
181184
inherits = FALSE) {
182185
x <- insight::safe_deparse(x)

R/geom_binomdensity.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ geom_binomdensity <- function(data,
6262

6363
# Other parameters
6464
data$.side <- ifelse(data[[y]] == y_levels[1], "top", "bottom")
65-
data$.justification <- as.numeric(!(data[[y]] == y_levels[1]))
65+
data$.justification <- as.numeric(data[[y]] != y_levels[1])
6666
data$.scale <- .geom_binomdensity_scale(data, x, y, scale)
6767

6868
# ggdist geom

R/geom_from_list.R

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -114,58 +114,68 @@
114114
#' @export
115115
geom_from_list <- function(x, ...) {
116116
# Additional parameters ------------------------------------------------------
117-
args <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")]
117+
arguments <- x[!names(x) %in% c("geom", "aes", "data", "width", "height", "position", "show.legend")]
118118

119119
if (is.null(x$geom)) {
120120
return(NULL)
121121
}
122122

123123
if (inherits(x$geom, "function")) {
124-
return(do.call(x$geom, args))
124+
return(do.call(x$geom, args = arguments))
125125
}
126126

127127
if (x$geom %in% c("density_2d", "density_2d_filled", "density_2d_polygon")) {
128-
if (!"contour" %in% names(args)) args$contour <- TRUE
129-
if (!"contour_var" %in% names(args)) args$contour_var <- "density"
128+
if (!"contour" %in% names(arguments)) arguments$contour <- TRUE
129+
if (!"contour_var" %in% names(arguments)) arguments$contour_var <- "density"
130130
}
131131

132132
# If they are not geoms, return immediately
133133
if (x$geom == "labs") {
134-
return(do.call(ggplot2::labs, args))
134+
return(do.call(ggplot2::labs, args = arguments))
135135
}
136136
if (x$geom == "guides") {
137-
return(do.call(ggplot2::guides, args))
137+
return(do.call(ggplot2::guides, args = arguments))
138138
}
139139
if (x$geom == "coord_flip") {
140-
return(do.call(ggplot2::coord_flip, args))
140+
return(do.call(ggplot2::coord_flip, args = arguments))
141141
}
142142
if (x$geom == "facet_wrap") {
143-
return(do.call(ggplot2::facet_wrap, args))
143+
return(do.call(ggplot2::facet_wrap, args = arguments))
144144
}
145145
if (x$geom == "facet_grid") {
146-
return(do.call(ggplot2::facet_grid, args))
146+
return(do.call(ggplot2::facet_grid, args = arguments))
147147
}
148148
if (x$geom == "smooth") {
149-
if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym))
150-
if (!"method" %in% names(args)) args$method <- "loess"
151-
if (!"formula" %in% names(args)) args$formula <- "y ~ x"
152-
return(do.call(ggplot2::geom_smooth, args))
149+
if (!is.null(x$aes)) {
150+
arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym))
151+
}
152+
if (!"method" %in% names(arguments)) {
153+
arguments$method <- "loess"
154+
}
155+
if (!"formula" %in% names(arguments)) {
156+
arguments$formula <- "y ~ x"
157+
}
158+
return(do.call(ggplot2::geom_smooth, args = arguments))
153159
}
154160

155161
if (startsWith(x$geom, "scale_") || startsWith(x$geom, "theme") || startsWith(x$geom, "see_")) {
156-
return(do.call(x$geom, args))
162+
return(do.call(x$geom, args = arguments))
157163
}
158164

159165
if (startsWith(x$geom, "ggside::")) {
160166
insight::check_if_installed("ggside")
161-
if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym))
162-
return(do.call(eval(parse(text = x$geom)), args))
167+
if (!is.null(x$aes)) {
168+
arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym))
169+
}
170+
return(do.call(eval(parse(text = x$geom)), args = arguments))
163171
}
164172

165173
if (startsWith(x$geom, "ggraph::")) {
166174
insight::check_if_installed("ggraph")
167-
if (!is.null(x$aes)) args$mapping <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym))
168-
return(do.call(eval(parse(text = x$geom)), args))
175+
if (!is.null(x$aes)) {
176+
arguments$mapping <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym))
177+
}
178+
return(do.call(eval(parse(text = x$geom)), args = arguments))
169179
}
170180

171181
# Default parameters
@@ -179,7 +189,7 @@ geom_from_list <- function(x, ...) {
179189
}
180190

181191
# Default for violin
182-
if (x$geom == "violin") {
192+
if (x$geom == "violin") { # nolint
183193
stat <- "ydensity"
184194
position <- "dodge"
185195
} else if (x$geom == "boxplot") {
@@ -212,7 +222,7 @@ geom_from_list <- function(x, ...) {
212222

213223
# Aesthetics
214224
if ("aes" %in% names(x)) {
215-
aes_list <- do.call(ggplot2::aes, lapply(x$aes, .str_to_sym))
225+
aes_list <- do.call(ggplot2::aes, args = lapply(x$aes, .str_to_sym))
216226
} else {
217227
aes_list <- NULL
218228
}
@@ -231,7 +241,7 @@ geom_from_list <- function(x, ...) {
231241
geom = x$geom,
232242
mapping = aes_list,
233243
data = x$data,
234-
params = args,
244+
params = arguments,
235245
show.legend = show.legend,
236246
...
237247
)

R/plot.check_collinearity.R

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ plot.see_check_collinearity <- function(x,
6969
xlim <- nrow(x)
7070
if (ylim < 10) ylim <- 10
7171

72-
if (!is.null(ci_data)) {
73-
x <- cbind(x, ci_data)
74-
} else {
72+
if (is.null(ci_data)) {
7573
x$VIF_CI_low <- NA_real_
7674
x$VIF_CI_high <- NA_real_
75+
} else {
76+
x <- cbind(x, ci_data)
7777
}
7878

7979
# make sure legend is properly sorted
@@ -118,33 +118,33 @@ plot.see_check_collinearity <- function(x,
118118
fill = colors[3],
119119
color = NA,
120120
alpha = 0.15
121-
) +
122-
{
123-
if (!is.null(ci_data)) {
124-
list(
125-
ggplot2::geom_linerange(
126-
linewidth = size_line,
127-
na.rm = TRUE
128-
),
129-
ggplot2::geom_segment(
130-
data = x[x$VIF_CI_high > ylim * 1.15, ],
131-
mapping = aes(
132-
x = .data$x,
133-
xend = .data$x,
134-
y = .data$y,
135-
yend = .data$VIF_CI_high
136-
),
137-
lineend = "round",
138-
linejoin = "round",
139-
arrow = ggplot2::arrow(
140-
ends = "last", type = "closed",
141-
angle = 20, length = ggplot2::unit(0.03, "native")
142-
),
143-
show.legend = FALSE
144-
)
145-
)
146-
}
147-
} +
121+
)
122+
123+
if (!is.null(ci_data)) {
124+
p <- p +
125+
ggplot2::geom_linerange(
126+
linewidth = size_line,
127+
na.rm = TRUE
128+
) +
129+
ggplot2::geom_segment(
130+
data = x[x$VIF_CI_high > ylim * 1.15, ],
131+
mapping = aes(
132+
x = .data$x,
133+
xend = .data$x,
134+
y = .data$y,
135+
yend = .data$VIF_CI_high
136+
),
137+
lineend = "round",
138+
linejoin = "round",
139+
arrow = ggplot2::arrow(
140+
ends = "last", type = "closed",
141+
angle = 20, length = ggplot2::unit(0.03, "native")
142+
),
143+
show.legend = FALSE
144+
)
145+
}
146+
147+
p <- p +
148148
geom_point2(
149149
size = size_point,
150150
na.rm = TRUE

R/plot.check_heteroscedasticity.R

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ plot.see_check_heteroscedasticity <- function(x, data = NULL, ...) {
2424

2525
faminfo <- insight::model_info(model)
2626
r <- tryCatch(
27-
{
28-
if (inherits(model, "merMod")) {
29-
stats::residuals(model, scaled = TRUE)
30-
} else if (inherits(model, c("glmmTMB", "MixMod"))) {
31-
sigma <- if (faminfo$is_mixed) {
32-
sqrt(insight::get_variance_residual(model))
33-
} else {
34-
.sigma_glmmTMB_nonmixed(model, faminfo)
35-
}
36-
stats::residuals(model) / sigma
37-
} else if (inherits(model, "glm")) {
38-
stats::rstandard(model, type = "pearson")
27+
if (inherits(model, "merMod")) {
28+
stats::residuals(model, scaled = TRUE)
29+
} else if (inherits(model, c("glmmTMB", "MixMod"))) {
30+
sig <- if (faminfo$is_mixed) {
31+
sqrt(insight::get_variance_residual(model))
3932
} else {
40-
stats::rstandard(model)
33+
.sigma_glmmTMB_nonmixed(model, faminfo)
4134
}
35+
stats::residuals(model) / sig
36+
} else if (inherits(model, "glm")) {
37+
stats::rstandard(model, type = "pearson")
38+
} else {
39+
stats::rstandard(model)
4240
},
4341
error = function(e) {
4442
NULL

R/plot.check_model.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ plot.see_check_model <- function(x,
209209
pw <- plots(p, n_columns = n_columns)
210210
.safe_print_plots(pw, ...)
211211
invisible(pw)
212-
} else {
213-
return(p)
214212
}
213+
214+
p
215215
}
216216

217217

R/plot.check_normality.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ plot.see_check_normality <- function(x,
6868
alpha_level = alpha
6969
)
7070
} else {
71-
if (type == "qq") {
71+
if (type == "qq") { # nolint
7272
model_info <- attributes(x)$model_info
7373
if (inherits(model, c("lme", "lmerMod", "merMod", "glmmTMB", "afex_aov", "BFBayesFactor"))) {
7474
res_ <- suppressMessages(sort(stats::residuals(model), na.last = NA))

R/plot.check_outliers.R

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ plot.see_check_outliers <- function(x,
4141
...) {
4242
type <- match.arg(type)
4343
influential_obs <- attributes(x)$influential_obs
44-
methods <- attr(x, "methods", exact = TRUE)
44+
outlier_methods <- attr(x, "methods", exact = TRUE)
4545

46-
if (type == "dots" && !is.null(influential_obs) && (is.null(methods) || length(methods) == 1)) {
46+
if (type == "dots" && !is.null(influential_obs) && (is.null(outlier_methods) || length(outlier_methods) == 1)) {
4747
.plot_diag_outliers_new(
4848
influential_obs,
4949
show_labels = show_labels,
@@ -52,12 +52,10 @@ plot.see_check_outliers <- function(x,
5252
dot_alpha_level = dot_alpha,
5353
colors = colors
5454
)
55+
} else if (length(outlier_methods) == 1) {
56+
.plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance)
5557
} else {
56-
if (length(methods == 1)) {
57-
.plot_diag_outliers(x, show_labels = show_labels, size_text = size_text, rescale_distance = rescale_distance)
58-
} else {
59-
.plot_outliers_multimethod(x, rescale_distance)
60-
}
58+
.plot_outliers_multimethod(x, rescale_distance)
6159
}
6260
}
6361

0 commit comments

Comments
 (0)