I have a classification tree analyzed using ctree()
was wondering how can one rotate the terminal nodes so that the axes are vertical?
library(party)
data(iris)
attach(iris)
plot(ctree(Species ~ Sepal.Length + Sepel.Width
+ Petal.Length + Petal.Width, data = iris))
Here is how I would go about it. Not the shortest answer, but I wanted to be as thorough as possible.
Since we are plotting your tree, it's probably a good idea to look at the documentation for the appropriate plotting function:
Looking at
?'plot.BinaryTree'
we see the following description of theterminal_panel
argument:Further down in the documentation is a link to
?node_barplot
. This is what I guessed was being used as a default, and calling the following proved the guess right:(The output is the same as your original graph).
Unfortunately, there's no
horizontal
orhoriz
parameters fornode_barplot
. Looking at the code for this function, by simply typingnode_barplot
at the prompt, reveals that the graphs are drawn "by hand" using viewports. Unfortunately, the only way I could find to proceed was to edit this function. I tried to make my changes as obvious as possible:And now we can test it!
Here's the output:
Just a few points:
I admit that this may not THE solution to your problem. It is merely a way of going about solving this type of problem, when easier options do not exist.
Don't trust the function I gave above completely. As you can see, the
beside
parameter disables thehoriz
parameter automatically (my first edit) since I did not alter the sections of code that deal withbeside
being true. If you want it to work in this case, you'll have to make those edits yourself - take a look at?viewport
and?grid.rect
to get started. I'm pretty sure thereverse
function is also broken, but haven't tested anything. Apologies to the original authors of the function if I butchered it a bit, this was merely meant to be a demonstration.I hope this helped a bit. Good luck with any further edits you need to make!