Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dedicated plot.parttree method #18

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^SCRATCH$
6 changes: 3 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ jobs:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
# - {os: macos-latest, r: 'release'}
# - {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
# - {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.RData
docs
inst/doc
SCRATCH/
25 changes: 15 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: parttree
Title: Visualise simple decision tree partitions
Version: 0.0.1.9004
Version: 0.0.1.9005
Authors@R: c(
person(given = "Grant",
family = "McDermott",
Expand All @@ -12,8 +12,7 @@ Authors@R: c(
role = "ctb",
email = "Achim.Zeileis@R-project.org",
comment = c(ORCID = "0000-0003-0918-3766")),
person(given = "Brian",
middle = "Heseung",
person(given = "Brian Heseung",
family = "Kim",
role = "ctb",
email = "brhkim@gmail.com",
Expand All @@ -27,28 +26,34 @@ Description: Simple functions for plotting 2D decision tree partition plots.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
LazyData: true
URL: https://github.com/grantmcdermott/parttree,
http://grantmcdermott.com/parttree
BugReports: https://github.com/grantmcdermott/parttree/issues
Depends:
ggplot2 (>= 3.4.0)
Imports:
rpart,
Imports:
graphics,
stats,
data.table,
partykit,
rlang
rlang,
rpart,
ggplot2 (>= 3.4.0),
tinyplot (> 0.1.0)
Suggests:
tinytest,
tinysnapshot (>= 0.0.3),
fontquiver,
rsvg,
svglite,
palmerpenguins,
titanic,
mlr3,
parsnip,
workflows,
magick,
imager,
patchwork,
knitr,
rmarkdown
Remotes: grantmcdermott/tinyplot
VignetteBuilder: knitr
5 changes: 4 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ S3method(parttree,LearnerRegrRpart)
S3method(parttree,constparty)
S3method(parttree,rpart)
S3method(parttree,workflow)
S3method(plot,parttree)
export(geom_parttree)
export(parttree)
import(ggplot2)
importFrom(data.table,":=")
importFrom(data.table,.SD)
importFrom(data.table,fifelse)
importFrom(graphics,par)
importFrom(stats,reformulate)
importFrom(tinyplot,tinyplot)
27 changes: 21 additions & 6 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
# parttree 0.0.1.9004
# parttree 0.0.1.9005

To be released as 0.1
To be released as 0.1.0

#### Breaking changes

* Move ggplot2 to Suggests, following the addition of native (base R)
`plot.parttree` method. The `geom_parttree()` function now checks whether
ggplot2 is available on the user's system before executing any code. (#18)
* The `flipaxes` argument has been renamed to `flip`, e.g.
`parttree(..., flip = TRUE)`. (#18)

#### Improvements

* Major speed-up for extracting parttree nodes and coordinates on complicated trees (#15).
* Add method for tidymodels workflows objects fitted with `"rpart"` engine (#7 by @juliasilge).
* Parttree objects now have their own class with a dedicated `plot.parttree`
method, powered by tinyplot. (#18)
* Major speed-up for extracting parttree nodes and coordinates on complicated
trees. (#15)
* Add method for tidymodels workflows objects fitted with `"rpart"` engine. (#7
by @juliasilge).

#### Bug fixes

* Support for negative values (#6 by @pjgeens).
* Better handling of single-level factors and `flipaxes` (#5).
* Support for negative values. (#6 by @pjgeens)
* Better handling of single-level factors and `flip(axes)`. (#5)

#### Internals

* Several dependency adjustments, e.g. tinyplot to Imports and ggplot2 to
Suggests. (#18)
* Added SVG snapshots for image-based tests. (#18)
* Bump ggplot2 version dependency to match deprecated functions from 3.4.0.
* Switched to "main" as primary GitHub branch for development.
* Added two dedicated vignettes.
Expand Down
40 changes: 26 additions & 14 deletions R/geom_parttree.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' @title Visualise tree partitions
#' @title Visualise tree partitions with ggplot2
#'
#' @description `geom_parttree()` is a simple extension of
#' [ggplot2::geom_rect()]that first calls
#' [parttree()] to convert the inputted tree object into an
#' amenable data frame.
#' amenable data frame. Please note that `ggplot2` is not a hard dependency
#' of `parttree` and should thus be installed separately on the user's system.
#' @param data An [rpart::rpart.object] or an object of compatible
#' type (e.g. a decision tree constructed via the `partykit`, `tidymodels`, or
#' `mlr3` front-ends).
#' @param flipaxes Logical. By default, the "x" and "y" axes variables for
#' @param flip Logical. By default, the "x" and "y" axes variables for
#' plotting are determined by the first split in the tree. This can cause
#' plot orientation mismatches depending on how users specify the other layers
#' of their plot. Setting to `TRUE` will flip the "x" and "y" variables for
#' the `geom_parttree` layer.
#' @import ggplot2
#' @inheritParams ggplot2::layer
#' @inheritParams ggplot2::geom_point
#' @inheritParams ggplot2::geom_segment
Expand All @@ -39,7 +39,9 @@
#' @seealso [parttree()], [ggplot2::geom_rect()].
#' @export
#' @examples
#' library(rpart)
#' library(parttree) # this package
#' library(rpart) # decision trees
#' library(ggplot2) # ggplot2 must be loaded separately
#'
#' ### Simple decision tree (max of two predictor variables)
#'
Expand Down Expand Up @@ -67,8 +69,7 @@
#' ## Oops
#' p2 + geom_parttree(data = iris_tree, aes(fill=Species), alpha = 0.1)
#'
#' ## Fix with 'flipaxes = TRUE'
#' p2 + geom_parttree(data = iris_tree, aes(fill=Species), alpha = 0.1, flipaxes = TRUE)
#' ## Fix with 'flip = TRUE'
#'
#'
#' ### Various front-end frameworks are also supported, e.g.:
Expand Down Expand Up @@ -106,19 +107,28 @@ geom_parttree =
function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
linejoin = "mitre", na.rm = FALSE, show.legend = NA,
inherit.aes = TRUE, flipaxes = FALSE, ...) {
pdata = parttree(data, flipaxes = flipaxes)
inherit.aes = TRUE, flip = FALSE, ...) {

ggplot2_installed = requireNamespace("ggplot2", quietly = TRUE)
if (isFALSE(ggplot2_installed)) {
stop("Please install the ggplot2 package.", .call = FALSE)
} else if (utils::packageVersion("ggplot2") < "3.4.0") {
stop("Please install a newer version of ggplot2 (>= 3.4.0).")
}


pdata = parttree(data, flip = flip)
mapping_null = is.null(mapping)
mapping$xmin = quote(xmin)
mapping$xmax = quote(xmax)
mapping$ymin = quote(ymin)
mapping$ymax = quote(ymax)
if (mapping_null) {
mapping = aes_all(mapping)
mapping = ggplot2::aes_all(mapping)
}
mapping$x = rlang::quo(NULL)
mapping$y = rlang::quo(NULL)
layer(
ggplot2::layer(
stat = stat, geom = GeomParttree,
data = pdata,
mapping = mapping,
Expand All @@ -129,11 +139,13 @@ geom_parttree =

## Underlying ggproto object
GeomParttree =
ggproto(
"GeomParttree", GeomRect,
default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1,
ggplot2::ggproto(
"GeomParttree", ggplot2::GeomRect,
default_aes = ggplot2::aes(colour = "black", linewidth = 0.5, linetype = 1,
x=NULL, y = NULL,
fill = NA, alpha = NA
),
non_missing_aes = c("x", "y", "xmin", "xmax", "ymin", "ymax")
)


Loading
Loading