-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathREADME.Rmd
99 lines (78 loc) · 3.48 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# parttree <a href='https://grantmcdermott.com/parttree/'><img src='man/figures/hex.png' align="right" width="120" /></a>
<!-- badges: start -->
[![CRAN status](https://www.r-pkg.org/badges/version/parttree)](https://CRAN.R-project.org/package=parttree)
[![R-universe status badge](https://grantmcdermott.r-universe.dev/badges/parttree)](https://grantmcdermott.r-universe.dev)
[![R-CMD-check](https://github.com/grantmcdermott/parttree/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/grantmcdermott/parttree/actions/workflows/R-CMD-check.yaml)
[![Docs](https://img.shields.io/badge/docs-homepage-blue.svg)](https://grantmcdermott.com/parttree/index.html)
<!-- badges: end -->
Visualize simple 2-D decision tree partitions in R. The **parttree**
package provides visualization methods for both base R graphics (via
[**tinyplot**](https://grantmcdermott.com/tinyplot/)) and
[**ggplot2**](https://ggplot2.tidyverse.org/).
## Installation
The stable version of **parttree** is available on CRAN.
``` r
install.packages("parttree")
```
Or, you can grab the latest development version from
[R-universe](https://grantmcdermott.r-universe.dev/parttree).
``` r
install.packages("parttree", repos = "https://grantmcdermott.r-universe.dev")
```
## Quickstart
The **parttree** [homepage](https://grantmcdermott.com/parttree/index.html)
includes an introductory vignette and detailed documentation. But here's a
quickstart example using the
["kyphosis"](https://search.r-project.org/CRAN/refmans/rpart/html/kyphosis.html)
dataset that comes bundled with the **rpart** package. In this case, we are
interested in predicting kyphosis recovery after spinal surgery, as a function
of 1) the number of topmost vertebra that were operated, and 2) patient age.
The key function is `parttree()`, which comes with its own plotting method.
```{r quickstart}
library(rpart) # For the dataset and fitting decisions trees
library(parttree) # This package
fit = rpart(Kyphosis ~ Start + Age, data = kyphosis)
# Grab the partitions and plot
fit_pt = parttree(fit)
plot(fit_pt)
```
Customize your plots by passing additional arguments:
```{r quickstart2}
plot(
fit_pt,
border = NA, # no partition borders
pch = 19, # filled points
alpha = 0.6, # point transparency
grid = TRUE, # background grid
palette = "classic", # new colour palette
xlab = "Topmost vertebra operated on", # custom x title
ylab = "Patient age (months)", # custom y title
main = "Tree predictions: Kyphosis recurrence" # custom title
)
```
### ggplot2
For **ggplot2** users, we offer an equivalent workflow via the `geom_partree()`
visualization layer.
```{r quickstart_gg}
library(ggplot2) ## Should be loaded separately
ggplot(kyphosis, aes(x = Start, y = Age)) +
geom_parttree(data = fit, alpha = 0.1, aes(fill = Kyphosis)) + # <-- key layer
geom_point(aes(col = Kyphosis)) +
labs(
x = "No. of topmost vertebra operated on", y = "Patient age (months)",
caption = "Note: Points denote observations. Shading denotes model predictions."
) +
theme_minimal()
```