# simplest case (no labels), will auto revert to y labels
tinyplot(1:12, type = "text")
# pass explicit `labels` arg if you want specific text
tinyplot(1:12, type = "text", labels = month.abb)
# you can also use a labeller function (passed to `tinylabel`) to
# customize
tinyplot(1:12, type = "text", labels = month.abb, labeller = toupper)
# tip: use `xpd = NA` to avoid clipping text at the plot region
tinyplot(1:12, type = "text", labels = month.name, xpd = NA)
# for advanced customization, it's safer to pass args through `type_text()`
tinyplot(
1:12,
type = type_text(
labels = month.abb,
family = "HersheyScript",
srt = -20)
)
# some formula + data.frame examples...
mt = transform(mtcars[1:20,], cyl = factor(cyl), model = rownames(mtcars[1:20,]))
# as a top-level argument, `labels` supports non-standard evaluation, so a
# bare column name is resolved against `data` (just like the plot variables)
# tinyplot(mpg ~ wt, data = mt, type = type_text(labels = mt$model))
tinyplot(mpg ~ wt, data = mt, type = "text", labels = model) # same
# tip: use `repel = TRUE` to automatically nudge overlapping labels apart
tinyplot(
mpg ~ wt, data = mt,
type = "text", labels = model,
repel = TRUE, xpd = NA
)
# limitation: `repel` logic currently works per group, so grouped text data
# may still overlap
tinyplot(
mpg ~ wt | cyl, data = mt,
type = "text", labels = model,
repel = TRUE, xpd = NA
)
# an advantage of the top-level `labels` approach is that data subsetting and
# sanitizing ops pass through automatically
tinyplot(hp ~ wt, data = mt)
tinyplot_add(subset = carb==2, col = "red", pch = 16)
tinyplot_add(subset = carb==2, col = "red", type = "text", labels = model, pos = 4)
# fancier plot that combines a theme and some other customizations
tinyplot(
mpg ~ hp | cyl,
data = mt,
legend = list(title = "Cylinders"),
theme = "web"
)
tinyplot_add(
labels = model,
type = type_text(
family = "HersheySans",
font = 2,
pos = 4,
repel = TRUE,
xpd = NA
)
)
Run the code above in your browser using DataLab