问题
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.
回答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()
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.
来源:https://stackoverflow.com/questions/20249155/plot-of-binarytree-ctree-party-ignores-plot-option-of-par