grantmcdermott / tinyplot

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

ribbon legends could be improved #57

Closed grantmcdermott closed 11 months ago

grantmcdermott commented 11 months ago

tl;dr the current ribbon legend is just a line without shading/fill. Contrary to https://github.com/grantmcdermott/plot2/pull/46#issuecomment-1649936102, I now realise that this could be addressed quite elegantly by specifying a large square with fill (basically, pch = 22 + pt.cex > 1) and some other tweaks.

Manual example of what I have in mind:

pred = predict(lm(mpg~wt+factor(cyl),mtcars), interval = "confidence")
m = cbind(mtcars, pred)

library(plot2)

with(
  m,
  plot2(
    wt, fit, ymin = lwr, ymax = upr, by = cyl, type = "ribbon",
    legend = FALSE # turn off default line type legend
    )
)

# manual example of a nicer legend
legend(
  "topright",
  legend = c(4,6,8),
  title = "cyl", bty = "n", border = NA, 
  col = palette()[1:3],
  pch = 22,
  pt.bg = adjustcolor(palette()[1:3], alpha = 0.2),
  pt.cex = 3.5,
  x.intersp = 0.75, y.intersp = 1.25,
  lty = 1,
  pt.lwd = 0,
  seg.len = 1.5
)

Created on 2023-08-11 with reprex v2.0.2

Update: seg.len above should probably be 1.25 but otherwise these settings seem to work well.