Skip to content

Commit

Permalink
Add table formatting options to g_lineplot() - table relative heigh…
Browse files Browse the repository at this point in the history
…t & font size (#1335)

# Pull Request

Fixes #1333 and #1334

---------

Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: Davide Garolini <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent 08adee9 commit 3709c31
Show file tree
Hide file tree
Showing 9 changed files with 611 additions and 251 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
* Added `"N_row"` as an optional input to `denom` in `s_count_occurrences()`.
* Refactored `a_count_occurrences_by_grade()` to no longer use `make_afun()`.

### Enhancements
* Added `rel_height_plot` parameter to `g_lineplot()` to control the line plot height relative to annotation table height.
* Updated the `table_font_size` parameter of `g_lineplot()` to control the size of all text in the annotation table, including labels.
* Added `as_list` parameter to `g_lineplot()` to allow users to return the line plot and annotation table elements as a list instead of stacked for more complex customization.

### Bug Fixes
* Fixed bug in `a_summary()` causing non-unique `row_name` values to occur when multiple statistics are selected for count variables.

Expand Down
38 changes: 32 additions & 6 deletions R/g_lineplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Line plot with the optional table.
#' Line plot with optional table.
#'
#' @inheritParams argument_convention
#' @param alt_counts_df (`data.frame` or `NULL`)\cr data set that will be used (only)
Expand Down Expand Up @@ -72,6 +72,11 @@
#' @param col (`character`)\cr color(s). See `?ggplot2::aes_colour_fill_alpha` for example values.
#' @param linetype (`character`)\cr line type(s). See `?ggplot2::aes_linetype_size_shape` for example values.
#' @param errorbar_width (`numeric(1)`)\cr width of the error bars.
#' @param rel_height_plot (`proportion`)\cr proportion of total figure height to allocate to the line plot.
#' Relative height of annotation table is then `1 - rel_height_plot`. If `table = NULL`, this parameter is ignored.
#' @param as_list (`flag`)\cr whether the two `ggplot` objects should be returned as a list when `table` is not `NULL`.
#' If `TRUE`, a named list with two elements, `plot` and `table`, will be returned. If `FALSE` (default) the
#' annotation table is printed below the plot via [cowplot::plot_grid()].
#'
#' @return A `ggplot` line plot (and statistics table if applicable).
#'
Expand Down Expand Up @@ -162,7 +167,9 @@ g_lineplot <- function(df,
errorbar_width = 0.45,
newpage = lifecycle::deprecated(),
col = NULL,
linetype = NULL) {
linetype = NULL,
rel_height_plot = 0.5,
as_list = FALSE) {
checkmate::assert_character(variables, any.missing = TRUE)
checkmate::assert_character(mid, null.ok = TRUE)
checkmate::assert_character(interval, null.ok = TRUE)
Expand All @@ -174,6 +181,8 @@ g_lineplot <- function(df,
checkmate::assert_number(errorbar_width, lower = 0)
checkmate::assert_string(title, null.ok = TRUE)
checkmate::assert_string(subtitle, null.ok = TRUE)
assert_proportion_value(rel_height_plot)
checkmate::assert_logical(as_list)

if (!is.null(table)) {
table_format <- get_formats_from_stats(table)
Expand Down Expand Up @@ -462,9 +471,15 @@ g_lineplot <- function(df,
axis.ticks = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.text.x = ggplot2::element_blank(),
axis.text.y = ggplot2::element_text(margin = ggplot2::margin(t = 0, r = 0, b = 0, l = 5)),
axis.text.y = ggplot2::element_text(
size = table_font_size * ggplot2::.pt,
margin = ggplot2::margin(t = 0, r = 0, b = 0, l = 5)
),
strip.text = ggplot2::element_text(hjust = 0),
strip.text.x = ggplot2::element_text(margin = ggplot2::margin(1.5, 0, 1.5, 0, "pt")),
strip.text.x = ggplot2::element_text(
size = table_font_size * ggplot2::.pt,
margin = ggplot2::margin(1.5, 0, 1.5, 0, "pt")
),
strip.background = ggplot2::element_rect(fill = "grey95", color = NA),
legend.position = "none"
)
Expand All @@ -473,8 +488,19 @@ g_lineplot <- function(df,
tbl <- tbl + ggplot2::facet_wrap(facets = group_var, ncol = 1)
}

# align plot and table
cowplot::plot_grid(p, tbl, ncol = 1, align = "v", axis = "tblr")
if (!as_list) {
# align plot and table
cowplot::plot_grid(
p,
tbl,
ncol = 1,
align = "v",
axis = "tblr",
rel_heights = c(rel_height_plot, 1 - rel_height_plot)
)
} else {
list(plot = p, table = tbl)
}
} else {
p
}
Expand Down
13 changes: 11 additions & 2 deletions man/g_lineplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions tests/testthat/_snaps/g_lineplot/g_lineplot_cohorts.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 12 additions & 12 deletions tests/testthat/_snaps/g_lineplot/g_lineplot_factor_levels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 3709c31

Please sign in to comment.