Skip to content

Commit

Permalink
Automatic ribbon order (#54)
Browse files Browse the repository at this point in the history
* Automatically order ribbon plots by xvar

* Allow interval plots (ribbons, pointrange, etc) to have a missing yvar

* NEWS and version bump

* Add ribbon tests
  • Loading branch information
grantmcdermott authored Aug 8, 2023
1 parent 2a46cb6 commit 7baaae2
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: plot2
Type: Package
Title: Lightweight extension of base R plot
Version: 0.0.3.9002
Version: 0.0.3.9003
Authors@R:
c(
person(
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# plot2 0.0.3.9002 (development version)
# plot2 0.0.3.9003 (development version)

New features:

Expand All @@ -11,6 +11,10 @@ Bug fixes:

- Y-label correctly prints if a function was used for the atomic plot method,
e.g. `plot2(rnorm(100)`. (#52 etiennebacher)
- Ribbon plot types are now automatically ordered by the x variable (#54
@grantmcdermott).
- Interval plots like ribbons, errorbars, and pointranges are now correctly
plotted even if a y variable isn't specified (#54 @grantmcdermott).

# plot2 0.0.3

Expand Down
23 changes: 20 additions & 3 deletions R/plot2.R
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,18 @@ plot2.default = function(
}

if (is.null(y)) {
y = x
x = seq_along(x)
xlab = "Index"
## Special catch for interval plots without a specified y-var
if (type %in% c("pointrange", "errorbar", "ribbon")) {
ymin_dep = deparse(substitute(ymin))
ymax_dep = deparse(substitute(ymax))
y_dep = paste0("[", ymin_dep, ", ", ymax_dep, "]")
y = rep(NA, length(x))
if (is.null(ylim)) ylim = range(c(ymin, ymax))
} else {
y = x
x = seq_along(x)
xlab = "Index"
}
}

if (is.null(xlab)) xlab = x_dep
Expand All @@ -310,6 +319,14 @@ plot2.default = function(
names(xlabs) = xlvls
x = as.integer(x)
}
if (type == "ribbon") {
xord = order(x)
x = x[xord]
y = y[xord]
ymin = ymin[xord]
ymax = ymax[xord]
rm(xord)
}
}

if (is.null(xlim)) xlim = range(x, na.rm = TRUE)
Expand Down
61 changes: 61 additions & 0 deletions inst/tinytest/_tinysnapshot/ribbon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions inst/tinytest/_tinysnapshot/ribbon_no_y.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions inst/tinytest/test-ribbon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
source("helpers.R")
using("tinysnapshot")

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

mtcars2 = cbind(mtcars, pred)


f = function() {
with(
mtcars2,
plot2(
x = wt, y = fit,
ymin = lwr, ymax = upr,
type = "ribbon"
)
)
}
expect_snapshot_plot(f, label = "ribbon")

f = function() {
with(
mtcars2,
plot2(
x = wt,
ymin = lwr, ymax = upr,
type = "ribbon"
)
)
}
expect_snapshot_plot(f, label = "ribbon_no_y")

0 comments on commit 7baaae2

Please sign in to comment.