Skip to content

Commit 58e998d

Browse files
committed
Consolidate plotting into plot(); deprecate plotPartial()
- plot.partial/plot.ice/plot.cice gain a lattice argument; lattice = TRUE reuses the existing lattice workhorses (3-predictor panels, wireframe) through the single plot() interface, printing the trellis and returning it invisibly - plotPartial() soft-deprecated with a pointer to plot(..., lattice = TRUE); internal callers (including partial(plot = TRUE)) bypass the deprecated generic so no warning is signaled - Vignette gains a lattice section with paper-style 3-D and three-predictor displays; tests updated and extended (test_plot.R) - pkgdown workflow now removes AGENTS.md before building the site (pkgdown renders every top-level .md file)
1 parent 0ba4bd3 commit 58e998d

16 files changed

Lines changed: 280 additions & 77 deletions

.github/workflows/pkgdown.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ jobs:
3636
extra-packages: any::pkgdown, local::.
3737
needs: website
3838

39+
# pkgdown renders every top-level .md file into a page; AGENTS.md is
40+
# agent/contributor tooling and should not be published on the site
41+
- name: Exclude agent instructions from the site
42+
run: rm -f AGENTS.md
43+
3944
- name: Build site
4045
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
4146
shell: Rscript {0}

NEWS.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# pdp (development version)
22

3+
## Deprecations
4+
5+
* `plotPartial()` is deprecated in favor of the consolidated `plot()`
6+
interface: the `plot()` methods gained a `lattice` argument, so
7+
`plot(..., lattice = TRUE)` now produces the same lattice-based displays
8+
(including 3-D wireframe surfaces and paneled three-predictor plots).
9+
`plotPartial()` continues to work (with a warning) but will be removed in a
10+
future release.
11+
312
## Bug fixes
413

514
* Plotting now works for non-syntactic predictor names (e.g., names containing

R/partial.R

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,22 +262,21 @@
262262
#' partial(boston.rf, pred.var = c("lstat", "rm"), grid.resolution = 40,
263263
#' plot = TRUE, chull = TRUE, progress = TRUE)
264264
#'
265-
#' # The plotPartial function offers more flexible plotting
265+
#' # The plot method produces lightweight base R graphics via the tinyplot
266+
#' # package by default; set `lattice = TRUE` for lattice graphics (e.g., for
267+
#' # 3-D surfaces or paneled three-predictor displays)
266268
#' pd <- partial(boston.rf, pred.var = c("lstat", "rm"), grid.resolution = 40)
267-
#' plotPartial(pd, levelplot = FALSE, zlab = "cmedv", drape = TRUE,
268-
#' colorkey = FALSE, screen = list(z = -20, x = -60))
269-
#'
270-
#' # The plot method can be used to produce lightweight base R graphics via
271-
#' # the tinyplot package
272269
#' plot(pd, contour = TRUE)
270+
#' plot(pd, lattice = TRUE, levelplot = FALSE, zlab = "cmedv", drape = TRUE,
271+
#' colorkey = FALSE, screen = list(z = -20, x = -60))
273272
#'
274273
#' #
275274
#' # Individual conditional expectation (ICE) curves
276275
#' #
277276
#'
278277
#' # Use partial to obtain ICE/c-ICE curves
279278
#' rm.ice <- partial(boston.rf, pred.var = "rm", ice = TRUE)
280-
#' plotPartial(rm.ice, rug = TRUE, train = boston, alpha = 0.2)
279+
#' plot(rm.ice, rug = TRUE, train = boston, alpha = 0.2)
281280
#' plot(rm.ice, center = TRUE, alpha = 0.2, rug = TRUE, train = boston)
282281
#'
283282
#' #
@@ -546,17 +545,19 @@ partial.default <- function(
546545
}
547546
return(invisible(pd.df))
548547
}
549-
# Return a graph (i.e., a "trellis" object)
548+
# Return a graph (i.e., a "trellis" object); the methods are called
549+
# directly (rather than via the deprecated plotPartial() generic) so no
550+
# deprecation warning is signaled
550551
res <- if (inherits(pd.df, what = c("ice", "cice"))) {
551-
plotPartial(
552+
plotPartial.ice(
552553
object = pd.df, plot.pdp = TRUE, rug = rug, train = train,
553554
alpha = alpha
554555
)
555556
} else {
556-
plotPartial(pd.df, smooth = smooth, rug = rug, train = train,
557-
levelplot = levelplot, contour = contour,
558-
contour.color = contour.color,
559-
screen = list(z = -30, x = -60)) # sensible default?
557+
plotPartial.partial(pd.df, smooth = smooth, rug = rug, train = train,
558+
levelplot = levelplot, contour = contour,
559+
contour.color = contour.color,
560+
screen = list(z = -30, x = -60)) # sensible default?
560561
}
561562
attr(res, "partial.data") <- pd.df # attach PDP data as an attribute
562563
} else { # return a data frame (i.e., a "data.frame" and "partial" object)

R/pdp-package.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
#' As of right now, \code{pdp} exports the following functions:
1212
#' \itemize{
1313
#' \item{\code{partial}} - construct partial dependence functions (i.e., objects of class \code{"partial"}) from various fitted model objects;
14-
#' \item{\code{plotPartial}} - plot partial dependence functions (i.e., objects of class \code{"partial"}) using \code{\link[lattice]{lattice}} graphics;
15-
#' \item{\code{plot}} - plot partial dependence functions (i.e., objects of class \code{"partial"}) using lightweight base R graphics via \code{\link[tinyplot]{tinyplot}};
14+
#' \item{\code{plot}} - plot partial dependence functions (i.e., objects of class \code{"partial"}) using lightweight base R graphics via \code{\link[tinyplot]{tinyplot}} (or \code{\link[lattice]{lattice}} graphics whenever \code{lattice = TRUE});
1615
#' \item{\code{exemplar}} - construct a single "exemplar" record from a data frame.
1716
#' }
1817
#' @keywords internal

R/plot.R

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#'
33
#' Plot partial dependence functions (i.e., marginal effects) and individual
44
#' conditional expectation (ICE) curves using lightweight base R graphics via
5-
#' the \href{https://grantmcdermott.com/tinyplot/}{tinyplot} package.
5+
#' the \href{https://grantmcdermott.com/tinyplot/}{tinyplot} package, or
6+
#' \code{\link[lattice]{lattice}} graphics whenever \code{lattice = TRUE}.
67
#'
78
#' @param x An object that inherits from class \code{"partial"}, \code{"ice"},
89
#' or \code{"cice"}; typically the result of a call to \code{\link{partial}}.
@@ -62,11 +63,43 @@
6263
#' title of the false color level plot used for two continuous predictors.
6364
#' Default is \code{"yhat"}.
6465
#'
66+
#' @param lattice Logical indicating whether or not to draw the display using
67+
#' \code{\link[lattice]{lattice}} graphics instead of tinyplot/base graphics.
68+
#' The lattice engine additionally supports three-predictor (paneled) displays
69+
#' and 3-D surfaces; see Details. Default is \code{FALSE}.
70+
#'
6571
#' @param ... Additional optional arguments to be passed on to
6672
#' \code{\link[tinyplot]{tinyplot}} (e.g., \code{palette}, \code{main}, or
67-
#' \code{theme}).
73+
#' \code{theme}) or, whenever \code{lattice = TRUE}, to the underlying lattice
74+
#' display (see Details).
75+
#'
76+
#' @details
77+
#' When \code{lattice = TRUE}, the display is constructed with
78+
#' \code{\link[lattice]{lattice}} graphics (this subsumes the now-deprecated
79+
#' \code{plotPartial()} interface). In that case, additional lattice-specific
80+
#' options can be supplied via \code{...}:
81+
#' \itemize{
82+
#' \item \code{levelplot} - use a false color level plot (\code{TRUE};
83+
#' default) or a 3-D \code{\link[lattice]{wireframe}} surface (\code{FALSE})
84+
#' for two continuous predictors;
85+
#' \item \code{chull} - overlay the convex hull of the first two predictors
86+
#' (requires \code{train});
87+
#' \item \code{col.regions} - color palette for level/wireframe plots;
88+
#' \item \code{number}/\code{overlap} - number of conditioning intervals
89+
#' (and their fraction of overlap) used to panel a third (continuous)
90+
#' predictor;
91+
#' \item any other argument accepted by \code{\link[lattice]{xyplot}},
92+
#' \code{\link[lattice]{levelplot}}, \code{\link[lattice]{wireframe}}, or
93+
#' \code{\link[lattice]{dotplot}} (e.g., \code{screen} or \code{drape}).
94+
#' }
95+
#' The tinyplot-specific arguments \code{color.by}, \code{bars}, and
96+
#' \code{legend.title} are ignored when \code{lattice = TRUE}.
6897
#'
69-
#' @return Draws a plot as a side effect and (invisibly) returns \code{x}.
98+
#' @return Draws a plot as a side effect. The tinyplot engine (invisibly)
99+
#' returns \code{x}; the lattice engine (\code{lattice = TRUE}) (invisibly)
100+
#' returns the \code{"trellis"} object, which can be captured for further
101+
#' manipulation (e.g., arranging multiple displays with
102+
#' \code{gridExtra::grid.arrange()}).
70103
#'
71104
#' @rdname plot.partial
72105
#'
@@ -101,7 +134,20 @@ plot.partial <- function(x, center = FALSE, plot.pdp = TRUE, pdp.col = "red2",
101134
pdp.lwd = 2, pdp.lty = 1, smooth = FALSE, rug = FALSE,
102135
contour = FALSE, contour.color = "white", train = NULL,
103136
alpha = 1, color.by = NULL, bars = FALSE,
104-
legend.title = "yhat", ...) {
137+
legend.title = "yhat", lattice = FALSE, ...) {
138+
139+
# Use lattice graphics instead? Calls the method directly (rather than the
140+
# deprecated plotPartial() generic) so no deprecation warning is signaled
141+
if (isTRUE(lattice)) {
142+
res <- plotPartial.partial(
143+
x, center = center, plot.pdp = plot.pdp, pdp.col = pdp.col,
144+
pdp.lwd = pdp.lwd, pdp.lty = pdp.lty, smooth = smooth, rug = rug,
145+
contour = contour, contour.color = contour.color, train = train,
146+
alpha = alpha, ...
147+
)
148+
print(res) # draw as a side effect, like the tinyplot engine
149+
return(invisible(res))
150+
}
105151

106152
# Determine if object contains multiple curves
107153
multi <- "yhat.id" %in% names(x)
@@ -154,7 +200,16 @@ plot.partial <- function(x, center = FALSE, plot.pdp = TRUE, pdp.col = "red2",
154200
#' @export
155201
plot.ice <- function(x, center = FALSE, plot.pdp = TRUE, pdp.col = "red2",
156202
pdp.lwd = 2, pdp.lty = 1, rug = FALSE, train = NULL,
157-
alpha = 1, color.by = NULL, ...) {
203+
alpha = 1, color.by = NULL, lattice = FALSE, ...) {
204+
if (isTRUE(lattice)) {
205+
res <- plotPartial.ice(
206+
x, center = center, plot.pdp = plot.pdp, pdp.col = pdp.col,
207+
pdp.lwd = pdp.lwd, pdp.lty = pdp.lty, rug = rug, train = train,
208+
alpha = alpha, ...
209+
)
210+
print(res)
211+
return(invisible(res))
212+
}
158213
tinyplot_ice_curves(
159214
object = x, center = center, plot.pdp = plot.pdp, pdp.col = pdp.col,
160215
pdp.lwd = pdp.lwd, pdp.lty = pdp.lty, rug = rug, train = train,
@@ -169,7 +224,15 @@ plot.ice <- function(x, center = FALSE, plot.pdp = TRUE, pdp.col = "red2",
169224
#' @export
170225
plot.cice <- function(x, plot.pdp = TRUE, pdp.col = "red2", pdp.lwd = 2,
171226
pdp.lty = 1, rug = FALSE, train = NULL, alpha = 1,
172-
color.by = NULL, ...) {
227+
color.by = NULL, lattice = FALSE, ...) {
228+
if (isTRUE(lattice)) {
229+
res <- plotPartial.cice(
230+
x, plot.pdp = plot.pdp, pdp.col = pdp.col, pdp.lwd = pdp.lwd,
231+
pdp.lty = pdp.lty, rug = rug, train = train, alpha = alpha, ...
232+
)
233+
print(res)
234+
return(invisible(res))
235+
}
173236
tinyplot_ice_curves(
174237
object = x, center = FALSE, plot.pdp = plot.pdp, pdp.col = pdp.col,
175238
pdp.lwd = pdp.lwd, pdp.lty = pdp.lty, rug = rug, train = train,

R/plotPartial.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
#' Plotting Partial Dependence Functions
1+
#' Plotting Partial Dependence Functions (deprecated)
22
#'
33
#' Plots partial dependence functions (i.e., marginal effects) using
44
#' \code{\link[lattice]{lattice}} graphics.
55
#'
6+
#' \strong{Deprecated:} \code{plotPartial()} is deprecated and will be removed
7+
#' in a future release; please use \code{plot(..., lattice = TRUE)} instead,
8+
#' which produces the same displays through a single interface (see
9+
#' \code{\link{plot.partial}} for details).
10+
#'
611
#' @param object An object that inherits from the \code{"partial"} class.
712
#'
813
#' @param center Logical indicating whether or not to produce centered ICE
@@ -104,6 +109,11 @@
104109
#' grid.arrange(p1, p2, ncol = 2)
105110
#' }
106111
plotPartial <- function(object, ...) {
112+
.Deprecated(
113+
msg = paste("pdp::plotPartial() is deprecated and will be removed in a",
114+
"future release. Please use plot(..., lattice = TRUE)",
115+
"instead; see ?pdp::plot.partial for details.")
116+
)
107117
UseMethod("plotPartial")
108118
}
109119

inst/tinytest/test_issues.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ pd113 <- partial(fit113, pred.var = "x-var", train = d113,
1616
grid.resolution = 5)
1717
expect_inherits(pd113, "partial")
1818
pdf(NULL)
19-
expect_inherits(plotPartial(pd113), "trellis")
19+
expect_inherits(plot(pd113, lattice = TRUE), "trellis")
2020
expect_inherits(plot(pd113), "partial") # tinyplot engine
2121
pd113.2 <- partial(fit113, pred.var = c("x-var", "x 2"), train = d113,
2222
grid.resolution = 5)
23-
expect_inherits(plotPartial(pd113.2), "trellis")
23+
expect_inherits(plot(pd113.2, lattice = TRUE), "trellis")
2424
expect_inherits(plot(pd113.2), "partial")
2525
ice113 <- partial(fit113, pred.var = "x-var", train = d113, ice = TRUE,
2626
grid.resolution = 5)
27-
expect_inherits(plotPartial(ice113), "trellis")
27+
expect_inherits(plot(ice113, lattice = TRUE), "trellis")
2828
expect_inherits(plot(ice113), "ice")
2929
invisible(dev.off())
3030

@@ -50,7 +50,8 @@ pd.multi <- partial(fit, pred.var = c("x1", "x2"), pred.fun = pfun, train = d,
5050
grid.resolution = 3)
5151
expect_inherits(pd.multi, "ice")
5252
pdf(NULL)
53-
expect_error(plotPartial(pd.multi), pattern = "multiple\\s+.?predictors")
53+
expect_error(plot(pd.multi, lattice = TRUE),
54+
pattern = "multiple\\s+.?predictors")
5455
expect_error(plot(pd.multi), pattern = "multiple\\s+.?predictors")
5556
invisible(dev.off())
5657

inst/tinytest/test_pkg_MASS.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ if (require(MASS, quietly = TRUE)) {
3939

4040
# Display plots in a grid
4141
grid.arrange(
42-
plotPartial(pd_lda),
43-
plotPartial(pd_lda_prob),
44-
plotPartial(ice_lda),
45-
plotPartial(ice_lda_prob),
42+
plot(pd_lda, lattice = TRUE),
43+
plot(pd_lda_prob, lattice = TRUE),
44+
plot(ice_lda, lattice = TRUE),
45+
plot(ice_lda_prob, lattice = TRUE),
4646
nrow = 2
4747
)
4848

@@ -68,10 +68,10 @@ if (require(MASS, quietly = TRUE)) {
6868

6969
# Display plots in a grid
7070
grid.arrange(
71-
plotPartial(pd_qda),
72-
plotPartial(pd_qda_prob),
73-
plotPartial(ice_qda),
74-
plotPartial(ice_qda_prob),
71+
plot(pd_qda, lattice = TRUE),
72+
plot(pd_qda_prob, lattice = TRUE),
73+
plot(ice_qda, lattice = TRUE),
74+
plot(ice_qda_prob, lattice = TRUE),
7575
nrow = 2
7676
)
7777

inst/tinytest/test_pkg_party.R

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ if (require(party, quietly = TRUE)) {
3737

3838
# Display plots in a grid
3939
grid.arrange(
40-
plotPartial(pd1),
41-
plotPartial(pd2),
42-
plotPartial(pd2_prob),
43-
plotPartial(ice1),
44-
plotPartial(ice2),
45-
plotPartial(ice2_prob),
40+
plot(pd1, lattice = TRUE),
41+
plot(pd2, lattice = TRUE),
42+
plot(pd2_prob, lattice = TRUE),
43+
plot(ice1, lattice = TRUE),
44+
plot(ice2, lattice = TRUE),
45+
plot(ice2_prob, lattice = TRUE),
4646
nrow = 2
4747
)
4848

@@ -73,12 +73,12 @@ if (require(party, quietly = TRUE)) {
7373

7474
# Display plots in a grid
7575
grid.arrange(
76-
plotPartial(pd3),
77-
plotPartial(pd4),
78-
plotPartial(pd4_prob),
79-
plotPartial(ice3),
80-
plotPartial(ice4),
81-
plotPartial(ice4_prob),
76+
plot(pd3, lattice = TRUE),
77+
plot(pd4, lattice = TRUE),
78+
plot(pd4_prob, lattice = TRUE),
79+
plot(ice3, lattice = TRUE),
80+
plot(ice4, lattice = TRUE),
81+
plot(ice4_prob, lattice = TRUE),
8282
nrow = 2
8383
)
8484

inst/tinytest/test_pkg_stats.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ if (require(party, quietly = TRUE)) {
3535

3636
# Display plots in a grid
3737
grid.arrange(
38-
plotPartial(pd1_lm),
39-
plotPartial(pd1_glm),
40-
plotPartial(ice1_lm),
41-
plotPartial(ice1_glm),
38+
plot(pd1_lm, lattice = TRUE),
39+
plot(pd1_glm, lattice = TRUE),
40+
plot(ice1_lm, lattice = TRUE),
41+
plot(ice1_glm, lattice = TRUE),
4242
nrow = 2
4343
)
4444

@@ -65,10 +65,10 @@ if (require(party, quietly = TRUE)) {
6565

6666
# Display plots in a grid
6767
grid.arrange(
68-
plotPartial(pd2_glm),
69-
plotPartial(pd2_glm_prob),
70-
plotPartial(ice2_glm),
71-
plotPartial(ice2_glm_prob),
68+
plot(pd2_glm, lattice = TRUE),
69+
plot(pd2_glm_prob, lattice = TRUE),
70+
plot(ice2_glm, lattice = TRUE),
71+
plot(ice2_glm_prob, lattice = TRUE),
7272
nrow = 2
7373
)
7474

0 commit comments

Comments
 (0)