grantmcdermott / parttree

R package for plotting simple decision tree partitions
http://grantmcdermott.com/parttree
Other
92 stars 23 forks source link

plot.parttree method #16

Open grantmcdermott opened 1 year ago

grantmcdermott commented 1 year ago

Would probably require adding extra attributes to the parttree object (name for x and y variables, fml call for evaluating the data object, etc.) A dedicatedbase plot method would also suggest that ggplot2 should move from Depends to Imports, but that's fine.

grantmcdermott commented 7 months ago

Once tinyplot gets on CRAN (and gets support for type = "rect"), this should be fairly straightforward.

library(rpart)
library(parttree)
#> Loading required package: ggplot2
library(tinyplot)

palette("classic")

fit = rpart(Kyphosis ~ Start + Age, data = kyphosis)
pt = parttree(fit)

plt(
    Age ~ Start | Kyphosis, kyphosis,
    pch = 19, grid = TRUE,
     xlab = "No. of topmost vertebra operated on",
     ylab = "Patient age (months)"
)
corners = par("usr")
xmin = pt$xmin; xmin[xmin==-Inf] = corners[1]
ymin = pt$ymin; ymin[ymin==-Inf] = corners[3]
xmax = pt$xmax; xmax[xmax==Inf]  = corners[2]
ymax = pt$ymax; ymax[ymax==Inf]  = corners[4]
rect(
    xmin, ymin, xmax, ymax,
    col = adjustcolor(palette(), alpha.f = 0.1)[pt$Kyphosis]
)

Created on 2024-02-09 with reprex v2.1.0