-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43f5a6a
commit 063a13e
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) |