1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#' @importFrom ggeffects ggpredict ggeffect
plot_type_eff <- function(type,
model,
terms,
ci.lvl,
pred.type,
facets,
show.data,
jitter,
geom.colors,
axis.title,
title,
legend.title,
axis.lim,
case,
show.legend,
dot.size,
line.size,
...) {
if (missing(facets) || is.null(facets)) facets <- FALSE
pred.type <- switch(pred.type,
fe = "fixed",
re = "random",
pred.type
)
if (type == "pred") {
dat <- ggeffects::ggpredict(
model = model,
terms = terms,
ci_level = ci.lvl,
type = pred.type,
...
)
} else if (type == "emm") {
dat <- ggeffects::ggemmeans(
model = model,
terms = terms,
ci_level = ci.lvl,
type = pred.type,
...
)
} else {
dat <- ggeffects::ggeffect(
model = model,
terms = terms,
ci_level = ci.lvl,
...
)
}
if (is.null(dat)) return(NULL)
# evaluate dots-arguments
alpha <- .15
dodge <- .1
dot.alpha <- .5
log.y <- FALSE
# save number of terms, needed later
n.terms <- length(insight::find_predictors(model, component = "conditional", flatten = TRUE))
add.args <- lapply(match.call(expand.dots = F)$`...`, function(x) x)
if ("alpha" %in% names(add.args)) alpha <- eval(add.args[["alpha"]])
if ("dodge" %in% names(add.args)) dodge <- eval(add.args[["dodge"]])
if ("dot.alpha" %in% names(add.args)) dot.alpha <- eval(add.args[["dot.alpha"]])
if ("log.y" %in% names(add.args)) log.y <- eval(add.args[["log.y"]])
# select color palette
if (geom.colors[1] != "bw") {
if (is.null(terms)) {
if (facets) {
geom.colors <- "bw"
.ngrp <- n.terms
} else {
.ngrp <- 1
}
} else {
.ngrp <- dplyr::n_distinct(dat$group)
}
geom.colors <- col_check2(geom.colors, .ngrp)
}
p <- graphics::plot(
dat,
show_ci = !is.na(ci.lvl),
facets = facets,
show_data = show.data,
colors = geom.colors,
use_theme = FALSE,
jitter = jitter,
case = case,
show_legend = show.legend,
dot_alpha = dot.alpha,
alpha = alpha,
dodge = dodge,
log_y = log.y,
dot_size = dot.size,
line_size = line.size
)
# set axis and plot titles
if (!is.null(axis.title) && !is.null(terms)) {
if (length(axis.title) > 1) {
p <- p + labs(x = axis.title[1], y = axis.title[2])
} else {
p <- p + labs(y = axis.title)
}
} else if (!is.null(axis.title) && is.null(terms)) {
if (length(axis.title) > 1) {
p <- purrr::map(p, ~ .x + labs(x = axis.title[1], y = axis.title[2]))
} else {
p <- purrr::map(p, ~ .x + labs(y = axis.title))
}
}
# set axis and plot titles
if (!is.null(title) && !is.null(terms))
p <- p + ggtitle(title)
else if (!is.null(title) && is.null(terms))
p <- purrr::map(p, ~ .x + ggtitle(title))
# set axis and plot titles
if (!is.null(legend.title)) {
if (geom.colors[1] == "bw") {
p <- p +
labs(linetype = legend.title) +
guides(colour = "none")
} else {
p <- p + labs(colour = legend.title)
}
}
# set axis limits
if (!is.null(axis.lim)) {
if (is.list(axis.lim))
p <- p + xlim(axis.lim[[1]]) + ylim(axis.lim[[2]])
else
p <- p + ylim(axis.lim)
}
p
}
|