Skip to content

Commit d1e5353

Browse files
committed
Adding 2 possible quantile dot plot functions
1 parent da5c707 commit d1e5353

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Imports:
3939
tibble (>= 2.0.0),
4040
tidyr,
4141
tidyselect,
42-
utils
42+
utils,
4343
Suggests:
4444
ggfortify,
4545
gridExtra (>= 2.2.1),

R/ppc-distributions.R

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,108 @@ ppc_boxplot <-
507507
xaxis_title(FALSE)
508508
}
509509

510+
#' @rdname PPC-distributions
511+
#' @export
512+
ppc_qdotplot <-
513+
function(y,
514+
yrep,
515+
...,
516+
binwidth = NULL,
517+
freq = TRUE) {
518+
check_ignored_arguments(...)
519+
520+
data <- ppc_data(y, yrep)
521+
522+
# Calculate adaptive binwidth if not provided
523+
if (is.null(binwidth)) {
524+
data_range <- diff(range(data$value, na.rm = TRUE))
525+
binwidth <- data_range / 30
526+
}
527+
528+
# Create a test plot to understand the data structure per facet
529+
test_plot <- ggplot(data, aes(x = .data$value)) +
530+
geom_dotplot(
531+
binwidth = binwidth,
532+
method = "histodot",
533+
) +
534+
facet_wrap_parsed("rep_label")
535+
536+
# Build the plot to extract scaling information
537+
built_plot <- ggplot_build(test_plot)
538+
539+
# Find the maximum count across all facets
540+
max_count_per_facet <- built_plot$data[[1]] %>%
541+
group_by(PANEL) %>%
542+
summarise(max_count = max(count, na.rm = TRUE), .groups = "drop")
543+
overall_max_count <- max(max_count_per_facet$max_count, na.rm = TRUE)
544+
545+
# More aggressive scaling for high counts
546+
if (overall_max_count <= 9) {
547+
optimal_dotsize <- 1.0
548+
} else {
549+
optimal_dotsize <- 3 / sqrt(overall_max_count)
550+
}
551+
552+
ggplot(data, mapping = set_hist_aes(
553+
freq = freq,
554+
fill = !!quote(is_y_label),
555+
color = !!quote(is_y_label),
556+
)) +
557+
geom_dotplot(
558+
binwidth = binwidth,
559+
method = "histodot",
560+
dotsize = optimal_dotsize,
561+
) +
562+
scale_fill_ppc() +
563+
scale_color_ppc() +
564+
facet_wrap_parsed("rep_label") +
565+
force_axes_in_facets() +
566+
bayesplot_theme_get() +
567+
space_legend_keys() +
568+
yaxis_text(FALSE) +
569+
yaxis_title(FALSE) +
570+
yaxis_ticks(FALSE) +
571+
xaxis_title(FALSE) +
572+
facet_text(FALSE) +
573+
facet_bg(FALSE)
574+
}
575+
576+
### GGDIST VERSION
577+
library(ggdist)
578+
ppc_qdotplot_ggdist <-
579+
function(y,
580+
yrep,
581+
...,
582+
binwidth = NA,
583+
quantiles = NA,
584+
freq = TRUE) {
585+
check_ignored_arguments(...)
586+
587+
data <- ppc_data(y, yrep)
588+
589+
ggplot(data, mapping = aes(
590+
x = .data$value,
591+
fill = .data$is_y_label,
592+
color = .data$is_y_label
593+
)) +
594+
stat_dots(
595+
binwidth = binwidth,
596+
quantiles = quantiles,
597+
overflow = "warn"
598+
) +
599+
scale_fill_ppc() +
600+
scale_color_ppc() +
601+
facet_wrap_parsed("rep_label") +
602+
force_axes_in_facets() +
603+
bayesplot_theme_get() +
604+
space_legend_keys() +
605+
yaxis_text(FALSE) +
606+
yaxis_title(FALSE) +
607+
yaxis_ticks(FALSE) +
608+
xaxis_title(FALSE) +
609+
facet_text(FALSE) +
610+
facet_bg(FALSE)
611+
}
510612

511613
#' @rdname PPC-distributions
512614
#' @export

0 commit comments

Comments
 (0)