Skip to content

Commit f3db193

Browse files
authored
Handle xmin/xmax/ymin/ymax along with model.frame (#403)
* handle xmin/xmax/ymin/ymax along with model.frame * turn facet into a formula if it does not evaluate successfully * document min/max processing in NEWS and ?tinyplot * typo * use formula-based syntax for pointrange/errorbar * run devtools::document
1 parent 745fa82 commit f3db193

6 files changed

Lines changed: 45 additions & 38 deletions

File tree

NEWS.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ where the formatting is also better._
2929
`tinyplot(..., yaxl = "%")`, etc. More details are provided in the lower-level
3030
`tinylabel` function documentation (#363, #391 @grantmcdermott)
3131

32-
### Bugs fixes:
32+
33+
### Bug fixes:
3334

3435
- The `tinyplot(..., cex = <cex>)` argument should be respected when using
3536
`type = "b"`. Thanks to @rjknell for report #307 and @vincentarelbundock for
@@ -70,6 +71,12 @@ where the formatting is also better._
7071
- Improved website theme and navigation layout, especially on mobile.
7172
(#395 @zeileis)
7273

74+
### Misc:
75+
76+
- Simplify specification of `xmin`/`xmax`/`ymin`/`ymax` in formula method.
77+
The arguments are now processed along with the `model.frame()` so that
78+
`ymin = var` works if `var` is a variable in the `data`.
79+
7380
### Internals:
7481

7582
- The order of the nested loop for drawing interior plot elements has been

R/tinyplot.R

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#' or interval plot types. Only used when the `type` argument is one of
1818
#' `"rect"` or `"segments"` (where all four min-max coordinates are required),
1919
#' or `"pointrange"`, `"errorbar"`, or `"ribbon"` (where only `ymin` and
20-
#' `ymax` required alongside `x`).
20+
#' `ymax` required alongside `x`). In the formula method the arguments
21+
#' can be specified as `ymin = var` if `var` is a variable in `data`.
2122
#' @param by grouping variable(s). The default behaviour is for groups to be
2223
#' represented in the form of distinct colours, which will also trigger an
2324
#' automatic legend. (See `legend` below for customization options.) However,
@@ -1346,6 +1347,10 @@ tinyplot.formula = function(
13461347
facet = NULL,
13471348
facet.args = NULL,
13481349
type = NULL,
1350+
xmin = NULL,
1351+
xmax = NULL,
1352+
ymin = NULL,
1353+
ymax = NULL,
13491354
xlim = NULL,
13501355
ylim = NULL,
13511356
# log = "",
@@ -1384,12 +1389,18 @@ tinyplot.formula = function(
13841389
## placeholder for legend title
13851390
legend_args = list(x = NULL)
13861391

1392+
## turn facet into a formula if it does not evaluate successfully
1393+
if (inherits(try(facet, silent = TRUE), "try-error")) {
1394+
facet = as.formula(paste("~", deparse(substitute(facet))))
1395+
environment(facet) = environment(formula)
1396+
}
1397+
13871398
## process all formulas
13881399
tf = tinyformula(formula, facet)
13891400

13901401
## set up model frame
13911402
m = match.call(expand.dots = FALSE)
1392-
m = m[c(1L, match(c("formula", "data", "subset", "na.action", "drop.unused.levels"), names(m), 0L))]
1403+
m = m[c(1L, match(c("formula", "data", "subset", "na.action", "drop.unused.levels", "xmin", "xmax", "ymin", "ymax"), names(m), 0L))]
13931404
m$formula = tf$full
13941405
## need stats:: for non-standard evaluation
13951406
m[[1L]] = quote(stats::model.frame)
@@ -1459,6 +1470,10 @@ tinyplot.formula = function(
14591470
facet = facet, facet.args = facet.args,
14601471
data = data,
14611472
type = type,
1473+
xmin = mf[["(xmin)"]],
1474+
xmax = mf[["(xmax)"]],
1475+
ymin = mf[["(ymin)"]],
1476+
ymax = mf[["(ymax)"]],
14621477
xlim = xlim,
14631478
ylim = ylim,
14641479
# log = "",

R/type_errorbar.R

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,11 @@
1111
#' op = tpar(pch = 19)
1212
#'
1313
#' # "errorbar" and "pointrange" type convenience strings
14-
#' with(
15-
#' coefs,
16-
#' tinyplot(x = term, y = est, ymin = lwr, ymax = upr, type = "errorbar")
17-
#' )
18-
#' with(
19-
#' coefs,
20-
#' tinyplot(x = term, y = est, ymin = lwr, ymax = upr, type = "pointrange")
21-
#' )
14+
#' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "errorbar")
15+
#' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = "pointrange")
2216
#'
2317
#' # Use `type_errorbar()` to pass extra arguments for customization
24-
#' with(
25-
#' coefs,
26-
#' tinyplot(x = term, y = est, ymin = lwr, ymax = upr, type = type_errorbar(length = 0.2))
27-
#' )
18+
#' tinyplot(est ~ term, ymin = lwr, ymax = upr, data = coefs, type = type_errorbar(length = 0.2))
2819
#'
2920
#' tpar(op)
3021
#'

man/tinyplot.Rd

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/type_errorbar.Rd

Lines changed: 3 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/introduction.qmd

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,14 @@ monthcoefs = data.frame(
307307
subset(!grepl("Day", term))
308308
309309
# plot
310-
with(
311-
monthcoefs,
312-
tinyplot(
313-
x = term, y = estimate,
314-
ymin = ci_low, ymax = ci_high,
315-
type = "pointrange", # or: "errobar", "ribbon"
316-
pch = 19, col = "dodgerblue",
317-
grid = TRUE,
318-
main = "Average Monthly Effect on Temperature"
319-
)
310+
tinyplot(
311+
estimate ~ term,
312+
ymin = ci_low, ymax = ci_high,
313+
data = monthcoefs,
314+
type = "pointrange", # or: "errobar", "ribbon"
315+
pch = 19, col = "dodgerblue",
316+
grid = TRUE,
317+
main = "Average Monthly Effect on Temperature"
320318
)
321319
```
322320

0 commit comments

Comments
 (0)