Skip to content

Commit

Permalink
Add option to abbreviate interval labels based on only those that are…
Browse files Browse the repository at this point in the history
… actually shown (fixes #64)
  • Loading branch information
willgearty committed Aug 7, 2024
1 parent 1ee71f4 commit ebfe9f4
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 250 deletions.
56 changes: 40 additions & 16 deletions R/coord_geo.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ utils::globalVariables(c("min_age", "max_age", "mid_age", "label",
#' `lab_color` column) and will be recycled if/as necessary.
#' @param rot The amount of counter-clockwise rotation to add to the labels
#' (in degrees).
#' @param abbrv If including labels, whether to use abbreviations instead of
#' full interval names.
#' @param abbrv If including labels, should the labels be abbreviated? If
#' `TRUE`, the `abbrev` column will be used for the labels. If `FALSE`, the
#' `name` column will be used for the labels. If `"auto"`, the [abbreviate()]
#' function will be used to abbreviate the values in the `name` column. Note
#' that the built-in data and data retrieved via [get_scale_data()] already
#' have built-in abbreviations. However, using the `"auto"` option here will
#' create new unique abbreviations based on only the intervals that are being
#' plotted. In many cases, this may result in abbreviations that are shorter
#' in length because there are fewer similar interval names to abbreviate.
#' @param skip A vector of interval names indicating which intervals should not
#' be labeled. If `abbrv` is `TRUE`, this can also include interval
#' abbreviations.
Expand All @@ -85,7 +92,7 @@ utils::globalVariables(c("min_age", "max_age", "mid_age", "label",
#' @param fontface The font face to use for the labels. The standard options are
#' "plain" (default), "bold", "italic", and "bold.italic".
#' @param lwd Line width.
#' @param neg Set this to true if your x-axis is using negative values.
#' @param neg Set this to `TRUE` if your x-axis is using negative values.
#' @param bord A vector specifying on which sides of the scale to add borders
#' (same options as `pos`).
#' @param center_end_labels Should labels be centered within the visible range
Expand Down Expand Up @@ -318,7 +325,9 @@ make_geo_scale <- function(self, dat, fill, color, alpha, pos,
check_number_decimal(alpha, min = 0, max = 1)
check_bool(lab)
check_number_decimal(rot)
check_bool(abbrv)
if (!is.logical(abbrv) && abbrv != "auto") {
cli::cli_abort('`abbrv` must be either a boolean or "auto".')
}
check_string(family)
check_string(fontface)
check_character(skip, allow_null = TRUE)
Expand Down Expand Up @@ -359,18 +368,6 @@ make_geo_scale <- function(self, dat, fill, color, alpha, pos,
} else if (!("color" %in% colnames(dat))) {
dat$color <- rep(c("grey60", "grey80"), length.out = nrow(dat))
}
if (!is.null(lab_color)) {
dat$lab_color <- rep(lab_color, length.out = nrow(dat))
} else if (!("lab_color" %in% colnames(dat))) {
dat$lab_color <- "black"
}
if (abbrv && "abbr" %in% colnames(dat)) {
dat$label <- dat$abbr
dat$label[dat$abbr %in% skip] <- ""
} else {
dat$label <- dat$name
}
dat$label[dat$name %in% skip] <- ""

# do this so ggsave gets the whole plot
old_plot <- last_plot()
Expand Down Expand Up @@ -420,8 +417,23 @@ make_geo_scale <- function(self, dat, fill, color, alpha, pos,
expand = FALSE, clip = self$clip)
}

# Filter data to only those that are within the plot limits
if (neg) {
dat <- subset(dat, min_age < max(lims) & min_age > min(lims) |
max_age < max(lims) & max_age > min(lims))
} else {
dat <- subset(dat, min_age > min(lims) & min_age < max(lims) |
max_age > min(lims) & max_age < max(lims))
}

# Add labels
if (lab) {
if (!is.null(lab_color)) {
dat$lab_color <- rep(lab_color, length.out = nrow(dat))
} else if (!("lab_color" %in% colnames(dat))) {
dat$lab_color <- "black"
}

if (center_end_labels) {
# center the labels for the time periods at the ends of the axis
# find which intervals overlap with the ends of the axis
Expand All @@ -437,6 +449,18 @@ make_geo_scale <- function(self, dat, fill, color, alpha, pos,
# recalculate the mid ages
dat$mid_age <- (dat$max_age + dat$min_age) / 2
}
if (abbrv == "auto") {
dat$label <- abbreviate(dat$name, minlength = 1,
use.classes = TRUE, named = FALSE)
} else if (abbrv && "abbr" %in% colnames(dat)) {
dat$label <- dat$abbr
dat$label[dat$abbr %in% skip] <- ""
} else {
dat$label <- dat$name
}

dat$label[dat$name %in% skip] <- ""

if (size == "auto") {
gg_scale <- gg_scale +
exec(geom_fit_text,
Expand Down
2 changes: 1 addition & 1 deletion R/get_scale_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ get_scale_data <- function(name, true_colors = TRUE) {
no_abbr <- (is.na(clean_dat$abbr) | clean_dat$abbr == "")
clean_dat$abbr[no_abbr] <-
abbreviate(clean_dat$name, minlength = 1,
use.classes = FALSE, named = FALSE)[no_abbr]
use.classes = TRUE, named = FALSE)[no_abbr]
dat <- clean_dat
}
dat
Expand Down
15 changes: 11 additions & 4 deletions man/coord_geo.Rd

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

11 changes: 0 additions & 11 deletions tests/testthat/_snaps/coord_geo/geom-fit-text-new.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit ebfe9f4

Please sign in to comment.