grantmcdermott / tinyplot

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

Should ribbons be (x-axis) ordered automatically? #53

Closed grantmcdermott closed 1 year ago

grantmcdermott commented 1 year ago

Ribbons only make sense in the context of ordered data. So something like the following should work automatically.

library(plot2)

mod = lm(mpg ~ wt, mtcars)
pred = predict(mod, interval = "confidence")

mtcars2 = cbind(mtcars, pred)

with(
  mtcars2,
  plot2(
    x = wt, y = mpg,
    ymin = lwr, ymax = upr,
    type = "ribbon"
  )
)

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

grantmcdermott commented 1 year ago

Reopening since grouped ribbons still aren't working unless manually ordered beforehand.

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")
  )


with(
  m[order(m$wt),],
  plot2(wt, fit, ymin = lwr, ymax = upr, by = cyl, type = "ribbon")
  )

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

grantmcdermott commented 1 year ago

Fixed with https://github.com/grantmcdermott/plot2/commit/b0bcb94fae07dc3ad50d4a3e24a5f96e46d65ad9

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")
  )

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