grantmcdermott / tinyplot

Lightweight extension of the base R graphics system
https://grantmcdermott.com/tinyplot
Apache License 2.0
208 stars 7 forks source link

Customizable facet strip text. #98

Closed grantmcdermott closed 5 months ago

grantmcdermott commented 5 months ago

Things like background fill, font colour and face (italic, bold, etc.) that the user could control through facet.args or par2.

Would probably require that we role our own combination of rect and text, rather than simply going through mtext. But that shouldn't be too problematic. Proof of concept:

curve(dnorm(x), -3, 3, col = "dodgerblue")

corners = par("usr") # Get the four corners of plot area (x1, x2, y1, y2)

# top side
line_height = grconvertY(1.1, from="char", to="user") - grconvertY(0, from="char", to="user")
rect(
    corners[1], corners[4], corners[2], corners[4] + line_height,
    col = "gray95", border = NA, xpd = TRUE
)
# mtext("Topside", side = 3, line = 0.1)
text(
    x = mean(corners[1:2]),
    y = corners[4],
    "Topside",
    adj = c(0.5, -0.5),
    xpd = TRUE, 
)

# right side
line_height = grconvertX(1.1, from="char", to="user") - grconvertX(0, from="char", to="user")
rect(
    corners[2], corners[3], corners[2]+line_height, corners[4],
    col = "gray95", border = NA, xpd = TRUE
)
# mtext("rightside", side = 4, line = 0.1)
text(
    x = corners[2],
    y = mean(corners[3:4]),
    "Rightside", srt = 270,
    adj = c(0.5, -0.5),
    xpd = TRUE
)

Created on 2024-01-22 with reprex v2.1.0

grantmcdermott commented 5 months ago

Complementarity: There's no way to rotate the RHS facet mtext for facet_grid cases, so that it faces outwards. (Reason: it doesn't support a srt argument and las doesn't give us that either.) So we'd have to go through a bespoke text approach anyway (as above: using srt = 270 to rotate outwards).