martin-borkovec / ggparty

147 stars 14 forks source link

Plot factors with many (long) levels #15

Open henningsway opened 5 years ago

henningsway commented 5 years ago

When using partykit, I often run into this problem: https://stackoverflow.com/questions/16581587/how-do-i-jitter-the-node-split-strings-in-plotting-ctree-output-from-partykit

Will you library tackle this problem? :)

Ps: the project looks really cool already!

martin-borkovec commented 5 years ago

Thanks for the suggestion! Probably wouldn't have anticipated this problem, although it seems obvious now. So in this paricular example it actually works out quite nicely with ggparty, since the default method is to plot the edge labels at the center of the edges. Whether that's generally the best option is open to debate, but at least in this case it's advantageous ;)

library(MASS)
library("partykit")
#> Loading required package: grid
#> Loading required package: libcoin
#> Loading required package: mvtnorm
SexTest <- ctree(sex ~ ., data=Aids2)

library(ggparty)
#> Loading required package: ggplot2
ggparty(SexTest) +
  geom_edge() + 
  geom_edge_label() +
  geom_node_splitvar() +
  geom_nodeplot(gglist = list(geom_bar(aes(x = "",
                                           fill = sex),
                                       position = position_fill()) 
  ))

Created on 2019-03-21 by the reprex package (v0.2.1)

But of course it would be nice to have an option to tackle this issue in other cases, so I've just added an argument to select only specified levels of the split to plot with one geom_edge_label. Playing around with the nudge and/or shift arguments one should hopefully be able to achieve a satisfying solution. Although it might get tricky considering the two separate white label-background boxes, so maybe we'll come up with a better solution.

ggparty(SexTest) +
  geom_edge() + 
  geom_edge_label(splitlevels = 1:2, y_nudge = 0.025) +
  geom_edge_label(splitlevels = 3:4, y_nudge = -0.025) +
  geom_node_splitvar() +
  geom_nodeplot(gglist = list(geom_bar(aes(x = "",
                                           fill = sex),
                                       position = position_fill()) 
  ))

Created on 2019-03-21 by the reprex package (v0.2.1)

henningsway commented 5 years ago

Awesome. This looks great.

I could also think about: