Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table formatting options to g_lineplot() - table relative height & font size #1335

Merged
merged 7 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# tern 0.9.6.9007

### 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.
Melkiades marked this conversation as resolved.
Show resolved Hide resolved

### 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 @@ -163,7 +168,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 @@ -175,6 +182,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 @@ -463,9 +472,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 @@ -474,8 +489,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
Loading