grantmcdermott · GitHub

Here: https://grantmcdermott.com/tinyplot/vignettes/tips.html#direct-labels

Probably a regression that was missed from #549.

library(tinyplot)
tinytheme("clean2")
aq = airquality
aq$Month = factor(month.name[aq$Month], levels = month.name[5:9])
# base layer
plt(Temp ~ Day | Month, data = aq, type = "l", legend = FALSE)
# for labels: subset to final dates for each month 
aq2 = aq[aq$Day == ave(aq$Day, aq$Month, FUN = max), ]
# add the labels with a type_text() layer
plt_add(data = aq2, type = "text", labels = aq2$Month,
        pos = 4, offset = 0.2, xpd = NA)

# Fix: first grab the theme params and then adjust the RHS margin by
# the longest label in the dataset
longest_lab = max(strwidth(as.character(aq2$Month)))/2 # divide by 2 to get lines
parms = tinyplot:::theme_clean2
parms$mar[4] = parms$mar[4] + longest_lab
tinytheme("clean2", mar = parms$mar) # theme with adjusted margins
# Now plot both the base and direct label layers
plt(Temp ~ Day | Month, data = aq, type = "l", legend = FALSE)
plt_add(data = aq2, type = "text", labels = aq2$Month,
        pos = 4, offset = 0.2, xpd = NA)

Created on 2026-05-15 with reprex v2.1.1

Read the original on github.com ↗