Skip to content

Commit

Permalink
type_text() (#281)
Browse files Browse the repository at this point in the history
* type_text()

* type_text() respects `cex`

* text legend color

* type_text() snapshot tests + allow single label

* legend character for type_text()

* type_text() doc
  • Loading branch information
vincentarelbundock authored Dec 27, 2024
1 parent 4764cb0 commit 0b57bd8
Show file tree
Hide file tree
Showing 14 changed files with 437 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(type_segments)
export(type_spineplot)
export(type_spline)
export(type_summary)
export(type_text)
export(type_vline)
importFrom(grDevices,adjustcolor)
importFrom(grDevices,as.raster)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ New plot types:
(#252 @vincentarelbundock, @zeileis, and @grantmcdermott)
- `type_rug()` (shortcut: `"rug"`) adds a rug to an existing plot. (#276
@grantmcdermott)
- `type_text()` (shortcut: `"text"`) adds text annotations. (@vincentarelbundock)
- Models:
- `type_glm()` (shortcut: `"glm"`) (@vincentarelbundock)
- `type_lm()` (shortcut: `"lm"`) (@vincentarelbundock)
Expand Down
4 changes: 3 additions & 1 deletion R/by_aesthetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ by_col = function(ngrps = 1L, col = NULL, palette = NULL, gradient = NULL, order

by_pch = function(ngrps, type, pch = NULL) {
no_pch = FALSE
if (!type %in% c("p", "b", "o", "pointrange", "errorbar", "boxplot", "qq")) {
if (identical(type, "text")) {
pch <- rep(15, ngrps)
} else if (!type %in% c("p", "b", "o", "pointrange", "errorbar", "boxplot", "qq")) {
no_pch = TRUE
pch = NULL

Expand Down
3 changes: 2 additions & 1 deletion R/sanitize.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sanitize_type = function(type, x, y, dots) {
"abline", "area", "boxplot", "errorbar", "function", "glm", "hist",
"histogram", "hline", "j", "jitter", "lines", "lm", "loess", "pointrange",
"points", "polygon", "polypath", "qq", "rect", "ribbon", "ridge", "rug",
"segments", "spineplot", "spline", "vline"
"segments", "spineplot", "spline", "text", "vline"
)
assert_choice(type, types, null.ok = TRUE)

Expand Down Expand Up @@ -60,6 +60,7 @@ sanitize_type = function(type, x, y, dots) {
"segments" = type_segments,
"spineplot" = type_spineplot,
"spline" = type_spline,
"text" = type_text,
"vline" = type_vline,
type # default case
)
Expand Down
10 changes: 7 additions & 3 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
#' - `"rect"` / [`type_rect()`]: Draws rectangles; requires `xmin`, `xmax`, `ymin`, and `ymax`.
#' - `"ribbon"` / [`type_ribbon()`]: Creates a filled area between `ymin` and `ymax`.
#' - `"segments"` / [`type_segments()`]: Draws line segments between pairs of points.
#' - `"text"` / [`type_text()`]: Add text annotations.
#' - Visualizations:
#' - `"boxplot"` / [`type_boxplot()`]: Creates a box-and-whisker plot.
#' - `"density"`: Plots the density estimate of a variable.
Expand All @@ -118,7 +119,7 @@
#' - [`type_abline()`]: line(s) with intercept and slope.
#' - [`type_hline()`]: horizontal line(s).
#' - [`type_vline()`]: vertical line(s).
#' - [`type_function()`]: arbitrary function.
#' - [`type_function()`]: arbitrary function.
#' - [`type_summary()`]: summarize `y` by unique values of `x`.
#' @param xmin,xmax,ymin,ymax minimum and maximum coordinates of relevant area
#' or interval plot types. Only used when the `type` argument is one of
Expand Down Expand Up @@ -500,11 +501,12 @@
#'
#' # It's possible to customize the look of your plots by setting graphical
#' # parameters (e.g., via `(t)par`)... But a more convenient way is to just use
#' # built-in themes (see `?tinytheme`).
#' # built-in themes (see `?tinytheme`).
#'
#' tinytheme("clean2")
#' tinyplot(
#' Temp ~ Day | Month, data = aq,
#' Temp ~ Day | Month,
#' data = aq,
#' type = "b",
#' alpha = 0.5,
#' main = "Daily temperatures by month",
Expand Down Expand Up @@ -1170,6 +1172,7 @@ tinyplot.default = function(
ixmax = idata[[ii]]$xmax
iymin = idata[[ii]]$ymin
iymax = idata[[ii]]$ymax
ilabels = idata[[ii]][["labels"]]

if (isTRUE(by_continuous)) {
icol = idata[[ii]]$col
Expand Down Expand Up @@ -1230,6 +1233,7 @@ tinyplot.default = function(
iy = iy,
iymax = iymax,
iymin = iymin,
ilabels = ilabels,
iz = iz,
cex = cex,
dots = dots,
Expand Down
49 changes: 49 additions & 0 deletions R/type_text.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' Text annotations plot type
#'
#' @description Type function for adding text annotations to a plot. This function allows
#' you to draw text at specified (x,y) coordinates. If no label is provided, the y-values
#' will be used as the text labels.
#'
#' @param labels Character vector of the same length as the number of x,y coordinates.
#' @param font Font to be used, following [graphics::par()]
#' @inheritParams graphics::text
#' @examples
#'
#' tinyplot(mpg ~ hp | factor(cyl),
#' data = mtcars,
#' type = type_text(
#' labels = row.names(mtcars),
#' font = 2,
#' adj = 0))
#'
#' @export
type_text = function(labels, adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, font = NULL) {
out = list(
draw = draw_text(adj = adj, pos = pos, offset = offset, vfont = vfont, font = font),
data = data_text(labels = labels),
name = "text"
)
class(out) = "tinyplot_type"
return(out)
}

data_text = function(labels) {
fun = function(datapoints, ...) {
assert_character(labels, name = "labels")
if (length(labels) != 1 && length(labels) != nrow(datapoints)) {
msg <- sprintf("`labels` must be of length 1 or %s.", nrow(datapoints))
stop(msg, call. = FALSE)
}
datapoints$labels = labels
out = list(datapoints = datapoints)
return(out)
}
return(fun)
}

draw_text = function(adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, font = NULL) {
fun = function(ix, iy, ilabels, icol, cex, ...) {
text(x = ix, y = iy, labels = ilabels, col = icol, adj = adj, pos = pos, offset = offset, vfont = vfont, font = font, cex = cex)
}
}

2 changes: 2 additions & 0 deletions altdoc/quarto_website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ website:
file: man/type_rect.qmd
- text: type_ribbon
file: man/type_ribbon.qmd
- text: type_text
file: man/type_text.qmd
- text: type_segments
file: man/type_segments.qmd
- section: Visualizations
Expand Down
Loading

0 comments on commit 0b57bd8

Please sign in to comment.