grantmcdermott / parttree

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

flipaxes = TRUE doesn't seem to always work #5

Closed datalorax closed 3 years ago

datalorax commented 4 years ago

Hi Grant,

I'm trying out your parttree package for the first time and overall I'm really liking it. I did run into what seems to be a bug with flipaxes though. In the below, you can see that it has no effect. I haven't dug into the code at all but I'm wondering if it could be related to having a model that is missing a level altogether in its prediction?

I can, of course, manually flip the x/y axes. But I just wanted to make you aware.

library(ggplot2)
library(rpart)
library(parttree)
mpg$drv <- as.factor(mpg$drv)
drv_stump <- rpart(drv ~  displ + cty, mpg,
                   control = list(maxdepth = 1))

ggplot(mpg, aes(displ, cty)) +
  geom_parttree(aes(fill = drv), 
                data = drv_stump, 
                alpha = 0.4) +
  geom_point(aes(color = drv))


ggplot(mpg, aes(displ, cty)) +
  geom_parttree(aes(fill = drv), 
                data = drv_stump, 
                alpha = 0.4,
                flipaxes = TRUE) +
  geom_point(aes(color = drv))

Created on 2020-11-06 by the reprex package (v0.3.0)

grantmcdermott commented 3 years ago

Daniel! Sorry for the criminally slow response. Too many balls in the air ATM...

Ya, that looks like a bug. I've still got some pressing tasks to clear off of my plate, but will bookmark and get back to it once things calm down a bit.

datalorax commented 3 years ago

Not a worry! It's nothing pressing at all, from my end. Just thought you'd want to know. 🙂

grantmcdermott commented 3 years ago

Should add: I'm pretty sure you're correct about the single level. Should be easy enough to catch/fix if you feel up for a PR in the meantime ;-) https://github.com/grantmcdermott/parttree/blob/master/R/parttree.R#L101

datalorax commented 3 years ago

I am fairly swamped until the end of the term (as I'm sure you are) but I may be able to find a few moments to look into it more. Will submit a PR if I do.

grantmcdermott commented 3 years ago

Hey Daniel, should be sorted in the dev version. Thanks for the hint about single levels; made it an easy fix!

library(ggplot2)
library(rpart)
library(parttree)
mpg$drv <- as.factor(mpg$drv)
drv_stump <- rpart(drv ~  displ + cty, mpg,
                                     control = list(maxdepth = 1))

ggplot(mpg, aes(displ, cty, fill = drv)) +
    geom_parttree(data = drv_stump, 
                                alpha = 0.4,
                                flipaxes = TRUE) +
    geom_point(aes(color = drv))

Created on 2020-12-17 by the reprex package (v0.3.0)

P.S. Note that I'm setting aes(... fill = drv) in the initialising ggplot() call above, since this way it retains all the factor levels for the legend. An alternative would be to use drop = FALSE in the scales, but this seems easier...

datalorax commented 3 years ago

Looks great! Thanks Grant!