grantmcdermott / tinyplot

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

Filled densities and better ribbon legend #58

Closed grantmcdermott closed 11 months ago

grantmcdermott commented 11 months ago

Closes #56. Also closes #57.

Quick examples:

palette("Tableau 10")
library(plot2)

# Density plot with manual colour and fill (via the 'bg' arg) 

with(
  airquality,
  plot2(
    density(Temp),
    by = Month,
    col = "white",
    bg = "red",
    grid = TRUE,
    legend = FALSE
  )
)

# Density plot with automatic, grouped colour and fill (via bg = "by")

with(
  airquality,
  plot2(
    density(Temp),
    by = Month,
    bg = "by", # or: type = "ribbon"
    grid = TRUE
  )
)


# Note that the "filled" legend is adaptive for other types of ribbon plots.
# E.g. Here we don't get a border, but rather a line in the middle.

mod = lm(Temp ~ 0 + factor(Month) / Day, airquality)
pred = predict(mod, interval = "confidence")

with(
  cbind(airquality, pred),
  plot2(
    x = Day, y = fit, by = Month,
    ymin = lwr, ymax = upr,
    type = "ribbon",
    grid = TRUE
  )
)

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

grantmcdermott commented 11 months ago

@vincentarelbundock @zeileis Feel free to take a look at this if/when you get a sec. (You can ignore the README changes, since I plan to shift that to a website soon to reduce the size of the install tarball.) The main idea is that we can leverage the new "ribbon" type and "bg" support to get filled densities. Then it's mostly just tweaking the legend to look right.