Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ Theme fixes:
and drawing them as standalone bars. This is useful for Likert plots, where
you want to show a neutral categories (e.g., "Unsure") apart from the
diverging stack. Thanks to @strengejacke for the suggestion.
- `type_text()` (and `type = "text"`) gains a `labeller` argument that is passed
to `tinylabel()` for formatting the text labels. This is useful for ensuring
that text annotations match a formatted axis, e.g. `labeller = "%"` to display
the labels as percentages. (#620 @grantmcdermott)

### Bug fixes

Expand Down
22 changes: 18 additions & 4 deletions R/type_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#' @param labels Character vector of length `1` or the same length as the
#' number of `x`,`y` coordinates. If left as `NULL`, then the labels will
#' automatically inherit the corresponding `y` values. See Examples.
#' @param labeller A formatting function (or convenience string) passed to
#' [`tinylabel`] for formatting the `labels`. Useful for ensuring that the
#' text labels match the formatting of an axis, e.g. `labeller = "%"` to
#' display the labels as percentages. Default is `NULL`, i.e. no formatting.
#' See Examples.
#' @param family The name of a font family. Default of `NULL` means that the
#' family will be the same as the main plot text, following
#' \code{\link[graphics]{par}}. Note that if a `family` argument is provided,
Expand Down Expand Up @@ -53,9 +58,15 @@
#' )
#' )
#'
#' # use `labeller` to format the labels, e.g. to match a formatted axis
#' d = data.frame(x = c("A", "B"), y = c(0.5, 0.8))
#' tinyplot(y ~ x, data = d, type = "bar", ylim = c(0, 1), yaxl = "%")
#' tinyplot_add(type = type_text(labeller = "%", adj = c(0.5, -0.5)))
#'
#' @export
type_text = function(
labels = NULL,
labeller = NULL,
adj = NULL,
pos = NULL,
offset = 0.5,
Expand All @@ -77,27 +88,30 @@ type_text = function(
xpd = xpd,
srt = srt
),
data = data_text(labels = labels, clim = clim),
data = data_text(labels = labels, labeller = labeller, clim = clim),
name = "text"
)
class(out) = "tinyplot_type"
return(out)
}

data_text = function(labels = NULL, clim = c(0.5, 2.5)) {
data_text = function(labels = NULL, labeller = NULL, clim = c(0.5, 2.5)) {
fun = function(settings, ...) {
env2env(settings, environment(), "datapoints")

# Store clim for bubble() function
settings$clim = clim

if (is.null(labels)) {
labels = datapoints$y
}
if (length(labels) != 1 && length(labels) != nrow(datapoints)) {
msg = sprintf("`labels` must be of length 1 or %s.", nrow(datapoints))
stop(msg, call. = FALSE)
}
if (!is.null(labeller)) {
labels = tinylabel(labels, labeller)
}
datapoints$labels = labels
if (is.factor(datapoints$x)) {
datapoints$x = as.numeric(datapoints$x)
Expand Down
59 changes: 59 additions & 0 deletions inst/tinytest/_tinysnapshot/text_labeller_percent.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions inst/tinytest/test-type_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,12 @@ f = function() {
tinyplot(x, y, labels = z, type = "text")
}
expect_snapshot_plot(f, label = "text_single_character")


# labeller arg formats text labels, e.g. to match a formatted axis (#617)
f = function() {
d = data.frame(x = c("A", "B"), y = c(0.5, 0.8))
tinyplot(y ~ x, data = d, type = "bar", ylim = c(0, 1), yaxl = "%")
tinyplot_add(type = type_text(labeller = "%", adj = c(0.5, -0.5)))
}
expect_snapshot_plot(f, label = "text_labeller_percent")
12 changes: 12 additions & 0 deletions man/type_text.Rd

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

Loading