Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot of BinaryTree (ctree, party) ignores plot option of par()

I would like to plot the BinaryTree in the uppper part of the plot, and make a second one in the second part (bottom). Here is some example code to show, that the plot of the tree completely ignores the partitioning options set by par()

library("party")
### regression
airct <- ctree(Ozone ~ ., data = subset(airquality, !is.na(Ozone)))
### classification
irisct <- ctree(Species ~ .,data = iris)

par(mfrow = c(2, 1))
plot(airct)
plot(irisct)

This code does not plot the two trees in the same plot (page). How can I correct this?

Even when following the very detailed answer does not work in this case: plots generated by 'plot' and 'ggplot' side-by-side the plotting of a ctree ignores all established options.

like image 661
dmeu Avatar asked Nov 12 '22 17:11

dmeu


1 Answers

The plots in party and its successor package partykit are implemented in grid and hence the base graphics options from par() such as mfrow do not work. In addition to the remarks from the comments, you can use grid.layout() to achieve similar results.

Doing so in plain grid is a bit technical but the code should not be too hard to follow:

grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))

pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
plot(airct, newpage = FALSE)
popViewport()

pushViewport(viewport(layout.pos.row = 2, layout.pos.col = 1))
plot(irisct, newpage = FALSE)
popViewport()

party grid

The reason for the newpage = FALSE argument is that by default the plot is drawn on a new page, rather than adding to a potentially existing plot.

like image 129
Achim Zeileis Avatar answered Nov 15 '22 05:11

Achim Zeileis