Skip to content

Commit

Permalink
comment code
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 15, 2023
1 parent d5c2b45 commit 87a94b4
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,19 @@ plot_panel <- function(x,
# fake init
.data <- NULL

if (.obj_has_name(x, "group") && is.character(x$group)) x$group <- factor(x$group, levels = unique(x$group))
if (.obj_has_name(x, "facet") && is.character(x$facet)) x$facet <- factor(x$facet, levels = unique(x$facet))
if (.obj_has_name(x, "response.level") && is.character(x$response.level)) x$response.level <- ordered(x$response.level, levels = unique(x$response.level))
# for plotting, we need to convert groups/facets into factors
if (.obj_has_name(x, "group") && is.character(x$group)) {
x$group <- factor(x$group, levels = unique(x$group))
}
if (.obj_has_name(x, "facet") && is.character(x$facet)) {
x$facet <- factor(x$facet, levels = unique(x$facet))
}
if (.obj_has_name(x, "response.level") && is.character(x$response.level)) {
x$response.level <- ordered(x$response.level, levels = unique(x$response.level))
}

# when group variable is numeric (like mean +/- SD), we need to preserve
# numeric values
if (rawdata && isTRUE(attr(x, "continuous.group"))) {
x$group_col <- as.numeric(as.character(x$group))
} else {
Expand All @@ -617,6 +626,9 @@ plot_panel <- function(x,
single_color <- FALSE

if (has_groups && !facets_grp && is_black_white && x_is_factor) {
# - we have more than one level/category for the x-axis
# - x-axis has a categorical predictor
# - black/white plot is requested, so we use different point shapes
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
Expand All @@ -628,6 +640,9 @@ plot_panel <- function(x,
)
)
} else if (has_groups && !facets_grp && is_black_white && !x_is_factor) {
# - we have more than one level/category (legend)
# - x-axis is a numeric / continuous predictor
# - black/white plot is requested, so we use different line types
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
Expand All @@ -639,6 +654,9 @@ plot_panel <- function(x,
)
)
} else if (has_groups && !facets_grp && !is.null(colors) && colors[1] == "gs" && x_is_factor) {
# - we have more than one level/category (legend)
# - x-axis is a numeric / continuous predictor
# - grey scale plot is requested, so we use different shapes
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
Expand All @@ -650,6 +668,9 @@ plot_panel <- function(x,
)
)
} else if (has_groups && (is.null(colors) || colors[1] != "bw")) {
# - we have more than one level/category (legend)
# - x-axis is either numeric or factor
# - default color palette is used, so we don't need to map line types or shapes
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(
Expand All @@ -660,6 +681,8 @@ plot_panel <- function(x,
)
)
} else {
# - no groups, so we have a single color plot w/o legend
# - colors are hardcoded inside geom
p <- ggplot2::ggplot(
plot_data,
ggplot2::aes(x = .data[["x"]], y = .data[["predicted"]])
Expand All @@ -671,6 +694,7 @@ plot_panel <- function(x,

# get color values -----

# we may have shortcuts are "colors", here we retrieve the actual color values
colors <- .get_colors(
colors,
length(unique(stats::na.omit(x$group))),
Expand Down Expand Up @@ -1204,14 +1228,14 @@ plot.ggalleffects <- function(x,
# check if we have a group-variable with at least two groups
if (.obj_has_name(rawdat, "group")) {

# we need to make sure that scale of raw data matches scale of predictions
if (isTRUE(attr(x, "continuous.group"))) {
rawdat$group_col <- as.numeric(as.character(rawdat$group))
} else {
rawdat$group_col <- rawdat$group
}

rawdat$group <- as.factor(rawdat$group)
# levels(rawdat$group) <- unique(x$group)
grps <- .n_distinct(rawdat$group) > 1
} else {
grps <- FALSE
Expand All @@ -1223,10 +1247,7 @@ plot.ggalleffects <- function(x,
rawdat <- rawdat[which(rawdat$group %in% x$group), , drop = FALSE]
}


# if we have groups, add colour aes, to map raw data to
# grouping variable

# if we have groups, add colour aes, to map raw data to grouping variable
if (grps) {
mp <- ggplot2::aes(
x = .data[["x"]],
Expand All @@ -1246,7 +1267,6 @@ plot.ggalleffects <- function(x,
}

# for binary response, no jittering by default

if ((attr(x, "logistic", exact = TRUE) == "1" && jitter.miss) || is.null(jitter)) {
p <- p + ggplot2::geom_point(
data = rawdat,
Expand Down

0 comments on commit 87a94b4

Please sign in to comment.