Skip to content
Draft
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Temporary Items
.claude
.claude/settings.local.json
.httr-oauth
.vscode
Rplots.pdf
/Meta/
/doc/
CRAN-SUBMISSION
Expand Down
8 changes: 5 additions & 3 deletions R/ggbarstats.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ ggbarstats <- function(
) +
exec(
geom_label,
mapping = aes(label = .label, group = {{ x }}),
position = position_fill(vjust = 0.5),
mapping = aes(label = .label, group = {{ x }}),
position = position_fill(vjust = 0.5),
show.legend = FALSE,
na.rm = TRUE,
!!!label.args
) +
ggtheme +
theme(panel.grid.major.x = element_blank()) +
guides(fill = guide_legend(title = legend.title %||% as_name(x))) +
paletteer::scale_fill_paletteer_d(paste0(package, "::", palette), name = "")
paletteer::scale_fill_paletteer_d(paste0(package, "::", palette), name = "", drop = FALSE)

# proportion test ------------------------------------------

Expand Down
23 changes: 20 additions & 3 deletions R/ggpiestats-ggbarstats-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,34 @@ descriptive_data <- function(
digits.perc = 1L,
...
) {
.cat_counter(data, {{ x }}, {{ y }}) %>%
# retain all original factor levels so grouped plots share a single legend;
# without this, patchwork::guides("collect") can't merge legends across

# grouped sub-plots when groups have different observed levels (#868)
all_lvls <- levels(dplyr::pull(data, {{ x }}))

result <- .cat_counter(data, {{ x }}, {{ y }}) %>%
mutate(
.label = if (grepl("perc|prop", label.content)) {
paste0(round(perc, digits.perc), "%")
} else if (grepl("count|n|N", label.content)) {
.prettyNum(counts)
} else {
paste0(.prettyNum(counts), "\n", "(", round(perc, digits.perc), "%)")
}, # reorder the category factor levels to order the legend
{{ x }} := factor({{ x }}, unique({{ x }}))
},
{{ x }} := factor({{ x }}, union(rev(all_lvls), unique({{ x }})))
)

# Inject zero-count rows for unobserved factor levels so that every layer
# (geom_bar / geom_col) can draw a proper legend key glyph for every level.
# Without this, patchwork sees structurally different guide grobs and
# refuses to collect them into a single shared legend (#868).
result <- tidyr::complete(
result, {{ x }}, {{ y }},
fill = list(counts = 0L, perc = 0, .label = NA_character_)
)

result
}


Expand Down
4 changes: 3 additions & 1 deletion R/ggpiestats.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ ggpiestats <- function(
min.segment.length = 0,
fill = "white",
alpha = 1.0,
show.legend = FALSE,
na.rm = TRUE,
!!!label.args
)
}))
Expand All @@ -192,7 +194,7 @@ ggpiestats <- function(
plotPie <- plotPie +
coord_polar(theta = "y") +
scale_y_continuous(breaks = NULL) +
paletteer::scale_fill_paletteer_d(paste0(package, "::", palette), name = "") +
paletteer::scale_fill_paletteer_d(paste0(package, "::", palette), name = "", drop = FALSE) +
ggtheme +
theme(panel.grid = element_blank(), axis.ticks = element_blank()) +
guides(fill = guide_legend(override.aes = list(color = NA)))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading