Tips & Tricks

This page collects miscellaneous tips and tricks for tinyplot. By definition, these are workarounds—i.e., techniques that fall outside of standard use cases or features that aren’t (yet) natively supported by the package. Please feel free to suggest or add more tips via our GitHub repo.

Legend

Opacity (alpha)

By default, the legend inherits the opacity of the plotted elements. In some cases, this may be undesirable. For example, in the following plot, the legend is too light:

library(tinyplot)
n = 50000
x = rnorm(n)
y = x + rnorm(n)
z = sample(c("a", "b"), n, replace = TRUE)
dat = data.frame(x, y, z)
tinyplot(y ~ x | z, data = dat, alpha = .1, pch = 19)

One solution is to draw our plot in two steps. First, we draw an empty plot with the desired (zero transparency) legend. Second, we add the transparent points on top of the existing canvas.

tinyplot(y ~ x | z, data = dat, pch = 19, empty = TRUE)
tinyplot_add(empty = FALSE, alpha = .1)

Labels

Axis label rotation

When category labels are long or overlapping, users may want to rotate them for readability. One option is fully perpendicular (90°) axis labels, which tinyplot supports via themes, e.g. tinytheme("clean", las = 2). If a user wants finer control over the degree of rotation—say, 45°—this requires a bit more manual effort since we do not support custom rotation out of the box. The workaround involves three steps:

  1. Suppress the default x-axis with xaxt = "n".
  2. Use text() to manually add rotated labels.
  3. Optionally, clear the y-axis label by setting ylab = "".
library(tinyplot)
tinyplot(~cyl, data = mtcars, type = "barplot", xaxt = "n", ylab = "")
text(1:3, 0,
    labels = c("Four cylinders", "Six cylinders", "Eight cylinders"),
    srt = 45,          # rotate text 45 degrees
    adj = c(1.1, 1.5), # adjust text alignment
    xpd = TRUE)        # allow drawing outside plot region

Note that adj and xpd settings may require trial and error to position labels correctly. Also, removing the x-axis label by setting ylab = "" is unintuitive but currently necessary when using formulas like ~ cyl.

P.S. Another option for long axis labels is to wrap them at a designated character length. See the final example in the tinylabel documentation for an example.