View source: R/get_fig_title_suffix_from_ggplot.R
| get_fig_title_suffix_from_ggplot | R Documentation |
Creates a formatted suffix for figure titles that includes the sample size (N) range from a ggplot object. Optionally generates markdown download links for both the plot data and the plot image.
get_fig_title_suffix_from_ggplot(
plot,
save = FALSE,
n_equals_string = "N = ",
file_suffixes = c(".csv", ".png"),
link_prefixes = c("[CSV](", "[PNG]("),
save_fns = list(utils::write.csv, saros::ggsaver),
sep = ", "
)
plot |
A |
save |
Logical flag. If |
n_equals_string |
String. Prefix text for the sample size display
(default: |
file_suffixes |
Character vector. File extensions for the saved plot images
(default: |
link_prefixes |
Character vector. Markdown link text prefixes for the plot download links
(default: |
save_fns |
List of functions. Functions to save the plot data and images. |
sep |
String. Separator between N range text and download links
(default: |
This function is particularly useful for adding informative captions to plots in
reports. The N range is calculated using n_range2(), which extracts the sample
size from the plot data. When save = TRUE, the function creates downloadable
files using make_link():
Plot data as CSV (via utils::write.csv)
Plot image as PNG (via ggsaver())
The function returns an AsIs object to prevent automatic character escaping
in markdown/HTML contexts.
An AsIs object (using I()) containing a character string with:
Sample size range formatted as "{n_equals_string}{range}"
If save = TRUE: additional download links for plot data and image, separated by sep
Empty string if plot is not a valid ggplot object or has no data
n_range2() for extracting N range from ggplot objects
make_link() for creating download links
ggsaver() for saving ggplot objects
# Create a sample plot
plot <- makeme(data = ex_survey, dep = b_1:b_3)
# Get just the N range text
get_fig_title_suffix_from_ggplot(plot)
# Custom N prefix
get_fig_title_suffix_from_ggplot(plot, n_equals_string = "Sample size: ")
## Not run:
# Generate with download links (saves files to disk)
get_fig_title_suffix_from_ggplot(plot, save = TRUE)
# Custom separator and link prefix
get_fig_title_suffix_from_ggplot(
plot,
save = TRUE,
sep = " | ",
link_prefix = "[Download PNG]("
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.