5

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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
dmeu
  • 3,842
  • 5
  • 27
  • 43
  • Looks like the plot methods for **party** use **grid** graphics, not base graphics, which means anything having to do with `par` is totally irrelevant. It's an entirely different system. – joran Nov 27 '13 at 17:39
  • ...you might try `grid.arrange` from the **gridExtra** package. – joran Nov 27 '13 at 17:40
  • Ok, I'll have a look and report pack. Did not know that there are different systems! Thanks – dmeu Nov 27 '13 at 17:51
  • @joran, it seems that gridExtra package and plot are not compatible – dmeu Nov 28 '13 at 09:35
  • That's not really true. It's not the function, its the plotting system being used. This particular plot function uses grid graphics, the same system used by gridExtra. The problem is not that they are "not compatible", but that the ctree plot method hasn't been written in a way that conveniently returns a single grid object that can be passed to grid.arrange. This is possible, but it will require a little more grid expertise than I have. – joran Nov 28 '13 at 15:35

1 Answers1

1

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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49