FreesearchR/R/plot_box.R

94 lines
2.6 KiB
R
Raw Normal View History

2025-03-19 13:10:56 +01:00
#' Beautiful box plot(s)
#'
2025-06-27 11:10:04 +02:00
#' @param data data frame
#' @param pri primary variable
#' @param sec secondary variable
#' @param ter tertiary variable
#' @param ... passed on to wrap_plot_list
#'
2025-03-19 13:10:56 +01:00
#' @returns ggplot2 object
#' @export
#'
#' @name data-plots
#'
#' @examples
2025-06-27 11:10:04 +02:00
#' mtcars |> plot_box(pri = "mpg", sec = "gear")
#' mtcars |> plot_box(pri = "mpg", sec="cyl")
2025-03-19 13:10:56 +01:00
#' mtcars |>
#' default_parsing() |>
#' plot_box(pri = "mpg", sec = "cyl", ter = "gear")
2025-06-27 11:10:04 +02:00
#' mtcars |>
#' default_parsing() |>
#' plot_box(pri = "mpg", sec = "cyl", ter = "gear",axis.font.family="mono")
plot_box <- function(data, pri, sec, ter = NULL,...) {
if (!is.null(ter)) {
ds <- split(data, data[ter])
2025-03-19 13:10:56 +01:00
} else {
ds <- list(data)
}
out <- lapply(ds, \(.ds){
plot_box_single(
data = .ds,
pri = pri,
sec = sec
2025-03-19 13:10:56 +01:00
)
})
2025-06-27 11:10:04 +02:00
wrap_plot_list(out,title=glue::glue("Grouped by {get_label(data,ter)}"),...)
2025-03-19 13:10:56 +01:00
}
#' Create nice box-plots
#'
#' @name data-plots
#'
2025-03-24 14:43:50 +01:00
#' @returns ggplot object
2025-03-19 13:10:56 +01:00
#' @export
#'
#' @examples
2025-04-22 13:57:59 +02:00
#' mtcars |> plot_box_single("mpg")
2025-03-19 13:10:56 +01:00
#' mtcars |> plot_box_single("mpg","cyl")
2025-06-27 11:10:04 +02:00
#' gtsummary::trial |> plot_box_single("age","trt")
plot_box_single <- function(data, pri, sec=NULL, seed = 2103) {
2025-03-19 13:10:56 +01:00
set.seed(seed)
if (is.null(sec)) {
sec <- "All"
2025-04-23 14:26:05 +02:00
data[[sec]] <- sec
2025-03-19 13:10:56 +01:00
}
discrete <- !data_type(data[[sec]]) %in% "continuous"
2025-03-19 13:10:56 +01:00
data |>
ggplot2::ggplot(ggplot2::aes(x = !!dplyr::sym(pri), y = !!dplyr::sym(sec), fill = !!dplyr::sym(sec), group = !!dplyr::sym(sec))) +
2025-03-19 13:10:56 +01:00
ggplot2::geom_boxplot(linewidth = 1.8, outliers = FALSE) +
## THis could be optional in future
2025-04-28 08:31:23 +02:00
ggplot2::geom_jitter(color = "black", size = 2, alpha = 0.9, width = 0.1, height = .2) +
2025-06-27 11:10:04 +02:00
ggplot2::xlab(get_label(data,pri))+
ggplot2::ylab(get_label(data,sec)) +
2025-03-19 13:10:56 +01:00
ggplot2::coord_flip() +
viridis::scale_fill_viridis(discrete = discrete, option = "D") +
# ggplot2::theme_void() +
ggplot2::theme_bw(base_size = 24) +
ggplot2::theme(
legend.position = "none",
# panel.grid.major = element_blank(),
# panel.grid.minor = element_blank(),
# axis.text.y = element_blank(),
# axis.title.y = element_blank(),
# text = ggplot2::element_text(size = 20),
# axis.text = ggplot2::element_blank(),
# plot.title = element_blank(),
panel.background = ggplot2::element_rect(fill = "white"),
plot.background = ggplot2::element_rect(fill = "white"),
panel.border = ggplot2::element_blank(),
panel.grid.major = ggplot2::element_blank(),
panel.grid.minor = ggplot2::element_blank(),
axis.line = ggplot2::element_line(colour = "black"),
axis.ticks = ggplot2::element_line(colour = "black")
)
}