How to change plot background of a ctree object in R?
I'm making a tree using the party package for a poster, and the background of the poster is grey. I've been able to change the background of all of my other plots (box plots, scatter plots) to grey by using the command par(bg = "grey") but this doesn't work for ctree.
For example, this makes a scatter plot on a grey background:
airq <- subset(airquality, !is.na(Ozone)) par(bg="grey") plot(Temp ~ Wind, data = airq)
But this does not make a tree on a grey background:
library("party") air.ct <- ctree(Ozone ~ ., data = airq) par(bg = "grey") plot(air.ct, inner_panel=node_inner(air.ct, pval = TRUE, id = FALSE), terminal_panel = node_boxplot(air.ct, id = FALSE))
Please help, my poster is due on Thursday!
Both the party package and its successor partykit are based on the grid package for visualization. Therefore, the par() function for base graphics is ignored when creating grid graphics. For the latter, there is a gpar() function but it does not directly support setting a bg background.
Therefore, in the current version of party or partykit setting the background color is not possible via simple arguments - only by supplying adapted panel functions.
However, as this feature was already partially supported in some panel functions, I've adapted the partykit package on R-Forge to enable setting backgrounds. The most recent version of the package is required for this:
library("partykit") packageDescription("partykit")$Version ##  "1.0-5"
The tree can be grown as in your example:
airq <- subset(airquality, !is.na(Ozone)) air.ct <- ctree(Ozone ~ ., data = airq)
Then we first add an empty page with a gray background:
grid.newpage() grid.rect(gp = gpar(col = "gray", fill = "gray"))
Then the tree can be added:
plot(air.ct, ip_args = list(id = FALSE, fill = "gray"), ep_args = list(fill = "gray"), tp_args = list(id = FALSE, bg = "gray", fill = "slategray"), newpage = FALSE )
To obtain this development version of partykit, please go to the R-Forge page of the package. There you can either check out the source package (see "SCM") and install it by hand - or you can wait until a new package has been built (see "R Packages"). The latter should hopefully be completed in a few hours.