Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,31 @@ plot.outstandR <- function(x, ...,
plot_df$Model <- factor(plot_df$Model, levels = model_levels)
}

# Define custom colors so that any naive model is plotted in black
n_models <- length(model_levels)
is_naive <- grepl("Naive", model_levels, ignore.case = TRUE)
n_non_naive <- sum(!is_naive)

if (n_non_naive > 0) {
non_naive_colors <- grDevices::hcl(
h = seq(15, 375, length.out = n_non_naive + 1)[1:n_non_naive],
c = 100,
l = 65
)
} else {
non_naive_colors <- character(0)
}

model_colors <- stats::setNames(rep("black", n_models), model_levels)
model_colors[!is_naive] <- non_naive_colors

# combined forest plot
ggplot(
plot_df, aes(x = .data$Estimate, y = .data$Treatments, color = .data$Model)) +
geom_point(position = position_dodge(width = 0.5), size = 3) +
geom_errorbarh(aes(xmin = .data$lower.0.95, xmax = .data$upper.0.95),
position = position_dodge(width = 0.5), height = 0.2, na.rm = TRUE) +
scale_color_manual(values = model_colors) +
facet_wrap(~.data$Type, scales = "free") +
geom_vline(data = dplyr::filter(plot_df, .data$Type == "Relative Contrasts"),
aes(xintercept = 0), linetype = "dashed", color = "gray50") +
Expand Down
184 changes: 103 additions & 81 deletions scripts/exercises2.html

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions scripts/exercises2.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,20 @@ all_results_intro$Method <- factor(all_results_intro$Method, levels = c(
```{r}
#| echo: true

# Define colors: naive is black, others are default ggplot2 colors
methods_intro <- levels(all_results_intro$Method)
is_naive_intro <- grepl("Naive", methods_intro, ignore.case = TRUE)
n_non_naive_intro <- sum(!is_naive_intro)
colors_intro <- stats::setNames(rep("black", length(methods_intro)), methods_intro)
colors_intro[!is_naive_intro] <- grDevices::hcl(
h = seq(15, 375, length.out = n_non_naive_intro + 1)[1:n_non_naive_intro],
c = 100, l = 65
)

ggplot(all_results_intro, aes(x = Estimate, y = Method, color = Method)) +
geom_point(size = 4) +
geom_errorbarh(aes(xmin = lower.0.95, xmax = upper.0.95), height = 0.2, linewidth = 1, na.rm = TRUE) +
scale_color_manual(values = colors_intro) +
geom_vline(xintercept = 0, linetype = "dashed", color = "gray50", linewidth = 1) +
theme_minimal() +
labs(
Expand Down Expand Up @@ -696,9 +707,20 @@ all_results_final$Method <- factor(all_results_final$Method, levels = c(
"G-comp ML (Anchored, Adjusted)"
))

# Define colors dynamically: naive is black, others are default ggplot2 colors
methods_final <- levels(all_results_final$Method)
is_naive_final <- grepl("Naive", methods_final, ignore.case = TRUE)
n_non_naive_final <- sum(!is_naive_final)
colors_final <- stats::setNames(rep("black", length(methods_final)), methods_final)
colors_final[!is_naive_final] <- grDevices::hcl(
h = seq(15, 375, length.out = n_non_naive_final + 1)[1:n_non_naive_final],
c = 100, l = 65
)

ggplot(all_results_final, aes(x = Estimate, y = Method, color = Method)) +
geom_point(size = 4) +
geom_errorbarh(aes(xmin = lower.0.95, xmax = upper.0.95), height = 0.2, linewidth = 1, na.rm = TRUE) +
scale_color_manual(values = colors_final) +

# Highlight True Effect (0.0) with a distinct line
geom_vline(aes(xintercept = 0, linetype = "True Underlying Effect"), color = "red", linewidth = 1.2) +
Expand Down
Binary file modified scripts/exercises2_files/figure-html/unnamed-chunk-12-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified scripts/exercises2_files/figure-html/unnamed-chunk-14-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified scripts/exercises2_files/figure-html/unnamed-chunk-25-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified scripts/exercises2_files/figure-html/unnamed-chunk-9-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified scripts/practical_paic.pdf
Binary file not shown.
Loading