Skip to content

Commit

Permalink
Add plot nchar function
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksanderbl29 committed Jan 9, 2025
1 parent 43f5a6a commit 063a13e
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: aleksandeR
Title: Aleksander Bang-Larsen's personal convenience functions
Version: 0.0.0.9002
Version: 0.0.0.9003
Date: 2024-11-3
Authors@R:
person("Aleksander", "Bang-Larsen", , "contact@aleksanderbl.dk", role = c("aut", "cre", "cph"),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(format_num)
export(install_basic_packages)
export(pkg_bib)
export(plot_nchar)
export(prepare_classes)
export(project_init)
export(theme_map)
Expand Down
55 changes: 55 additions & 0 deletions R/plot_nchar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' Get character count from `ggplot2` object.
#'
#' @param plot Input a `ggplot2` object
#'
#' @returns Returns a integer with the amount of characters in the plot
#' @export
#'
#' @examples
#' plot <- data.frame(
#' x = seq(1, 10),
#' y = seq(10, 1),
#' label = rep(seq(1, 10, 2), 2)
#' ) |>
#' ggplot2::ggplot(ggplot2::aes(x = x, y = y, label = label)) +
#' ggplot2::geom_text(nudge_y = 3)
#'
#' plot_nchar(plot)
plot_nchar <- function(plot) {
# Extract built plot
built_plot <- ggplot2::ggplot_build(plot)

# Get text from geom_text/geom_label layers
text_layers <- lapply(built_plot$data, function(layer) {
if("label" %in% names(layer)) {
return(layer$label)
}
NULL
}) |> unique()

text_layers <- unlist(text_layers[!sapply(text_layers, is.null)])

# Get plot labels (title, axis labels, etc)
labels <- plot$labels

# Get axis text
x_text <- built_plot$layout$panel_params[[1]]$x$get_labels()
y_text <- built_plot$layout$panel_params[[1]]$y$get_labels()

# Combine all text elements
all_text <- list(
geom_text = text_layers,
title = labels$title,
subtitle = labels$subtitle,
x_label = labels$x,
y_label = labels$y,
caption = labels$caption,
x_axis_text = x_text,
y_axis_text = y_text
) |>
unlist() |>
nchar() |>
sum(na.rm = TRUE)

return(all_text)
}
28 changes: 28 additions & 0 deletions man/plot_nchar.Rd

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

21 changes: 21 additions & 0 deletions tests/testthat/test-plot_nchar.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("plot_nchar correctly extracts text", {
df_1 <- data.frame(
x = seq(20, 11),
y = seq(11, 20),
label = rep(seq(11, 15), 2)
)

plot <- data.frame(
x = seq(1, 10),
y = seq(10, 1),
label = rep(seq(1, 10, 2), 2)
) |>
ggplot2::ggplot(ggplot2::aes(x = x, y = y, label = label)) +
ggplot2::geom_point() +
ggplot2::geom_line() +
ggplot2::geom_text(nudge_y = 3) +
ggplot2::geom_text(data = df_1) +
ggplot2::annotate("text", x = c(3, 8), y = c(3, 8),
label = c("Hej", "Farvel til dig"))
expect_equal(63, plot_nchar(plot))
})

0 comments on commit 063a13e

Please sign in to comment.