2025-03-13 12:41:50 +01:00
|
|
|
#' Nice horizontal stacked bars (Grotta bars)
|
|
|
|
#'
|
|
|
|
#' @returns ggplot2 object
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @name data-plots
|
|
|
|
#'
|
|
|
|
#' @examples
|
2025-04-15 12:04:32 +02:00
|
|
|
#' mtcars |> plot_hbars(pri = "carb", sec = "cyl")
|
|
|
|
#' mtcars |> plot_hbars(pri = "carb", sec = NULL)
|
|
|
|
plot_hbars <- function(data, pri, sec, ter = NULL) {
|
|
|
|
out <- vertical_stacked_bars(data = data, score = pri, group = sec, strata = ter)
|
2025-03-13 12:41:50 +01:00
|
|
|
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#' Vertical stacked bar plot wrapper
|
|
|
|
#'
|
|
|
|
#' @param data data.frame
|
|
|
|
#' @param score outcome variable
|
|
|
|
#' @param group grouping variable
|
|
|
|
#' @param strata stratifying variable
|
|
|
|
#' @param t.size text size
|
|
|
|
#'
|
|
|
|
#' @return ggplot2 object
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
vertical_stacked_bars <- function(data,
|
|
|
|
score = "full_score",
|
|
|
|
group = "pase_0_q",
|
|
|
|
strata = NULL,
|
|
|
|
t.size = 10,
|
|
|
|
l.color = "black",
|
|
|
|
l.size = .5,
|
|
|
|
draw.lines = TRUE) {
|
|
|
|
if (is.null(group)) {
|
|
|
|
df.table <- data[c(score, group, strata)] |>
|
|
|
|
dplyr::mutate("All" = 1) |>
|
|
|
|
table()
|
|
|
|
group <- "All"
|
|
|
|
draw.lines <- FALSE
|
|
|
|
} else {
|
|
|
|
df.table <- data[c(score, group, strata)] |>
|
|
|
|
table()
|
|
|
|
}
|
|
|
|
|
|
|
|
p <- df.table |>
|
|
|
|
rankinPlot::grottaBar(
|
|
|
|
scoreName = score,
|
|
|
|
groupName = group,
|
|
|
|
textColor = c("black", "white"),
|
|
|
|
strataName = strata,
|
|
|
|
textCut = 6,
|
|
|
|
textSize = 20,
|
|
|
|
printNumbers = "none",
|
|
|
|
lineSize = l.size,
|
|
|
|
returnData = TRUE
|
|
|
|
)
|
|
|
|
|
|
|
|
colors <- viridisLite::viridis(nrow(df.table))
|
|
|
|
contrast_cut <-
|
|
|
|
sum(contrast_text(colors, threshold = .3) == "white")
|
|
|
|
|
2025-05-10 13:02:04 +02:00
|
|
|
score_label <- data |> get_label(var = score)
|
|
|
|
group_label <- data |> get_label(var = group)
|
2025-03-13 12:41:50 +01:00
|
|
|
|
|
|
|
p |>
|
|
|
|
(\(.x){
|
|
|
|
.x$plot +
|
|
|
|
ggplot2::geom_text(
|
|
|
|
data = .x$rectData[which(.x$rectData$n >
|
|
|
|
0), ],
|
|
|
|
size = t.size,
|
|
|
|
fontface = "plain",
|
|
|
|
ggplot2::aes(
|
|
|
|
x = group,
|
|
|
|
y = p_prev + 0.49 * p,
|
|
|
|
color = as.numeric(score) > contrast_cut,
|
|
|
|
# label = paste0(sprintf("%2.0f", 100 * p),"%"),
|
|
|
|
label = sprintf("%2.0f", 100 * p)
|
|
|
|
)
|
|
|
|
) +
|
|
|
|
ggplot2::labs(fill = score_label) +
|
|
|
|
ggplot2::scale_fill_manual(values = rev(colors)) +
|
|
|
|
ggplot2::theme(
|
|
|
|
legend.position = "bottom",
|
|
|
|
axis.title = ggplot2::element_text(),
|
|
|
|
) +
|
|
|
|
ggplot2::xlab(group_label) +
|
|
|
|
ggplot2::ylab(NULL)
|
|
|
|
# viridis::scale_fill_viridis(discrete = TRUE, direction = -1, option = "D")
|
|
|
|
})()
|
|
|
|
}
|