Skip to content

Commit

Permalink
Add checks that transforms are functions
Browse files Browse the repository at this point in the history
If geom does not end in "repel" and label.fill == "" set label.fill to NA. This greatly improves performance of geom_label()
  • Loading branch information
aphalo committed Sep 27, 2024
1 parent 9d10403 commit 4766536
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
24 changes: 23 additions & 1 deletion R/stat-label-peaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ stat_label_peaks <-
na.rm = TRUE,
show.legend = FALSE,
inherit.aes = TRUE) {
if (!(is.function(x.label.transform) &&
is.function(y.label.transform) &&
is.function(colour.transform))) {
stop("'transform' arguments must be function defintions")
}

# for performance
if (!grepl("repel$", geom) && label.fill == "") {
label.fill <- NA_character_
}

ggplot2::layer(
stat = StatLabelPeaks, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
Expand Down Expand Up @@ -293,7 +304,18 @@ stat_label_valleys <- function(mapping = NULL,
na.rm = TRUE,
show.legend = FALSE,
inherit.aes = TRUE) {
ggplot2::layer(
if (!(is.function(x.label.transform) &&
is.function(y.label.transform) &&
is.function(colour.transform))) {
stop("'transform' arguments must be function defintions")
}

# for performance
if (!grepl("repel$", geom) && label.fill == "") {
label.fill <- NA_character_
}

ggplot2::layer(
stat = StatLabelValleys, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
params = list(span = span,
Expand Down
12 changes: 12 additions & 0 deletions R/stat-peaks.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ stat_peaks <- function(mapping = NULL,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {
if (!(is.function(x.label.transform) &&
is.function(y.label.transform) &&
is.function(colour.transform))) {
stop("'transform' arguments must be function defintions")
}

ggplot2::layer(
stat = StatPeaks, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
Expand Down Expand Up @@ -269,6 +275,12 @@ stat_valleys <- function(mapping = NULL,
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE) {
if (!(is.function(x.label.transform) &&
is.function(y.label.transform) &&
is.function(colour.transform))) {
stop("'transform' arguments must be function defintions")
}

ggplot2::layer(
stat = StatValleys, data = data, mapping = mapping, geom = geom,
position = position, show.legend = show.legend, inherit.aes = inherit.aes,
Expand Down

0 comments on commit 4766536

Please sign in to comment.