4

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!

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
JGM
  • 43
  • 3

1 Answers1

3

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] "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
)

ctree with gray bg

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.

Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • The new version 1.0-5 of the `partykit` package is now readily built on R-Forge (https://R-Forge.R-project.org/R/?group_id=26) and can be easily installed via `install.packages(“partykit“, repos = “http://R-Forge.R-project.org“)`. – Achim Zeileis Dec 02 '15 at 08:28
  • Thank you! I'm still having a bit of a problem. When I try to install the partykit package I get the following warning message: Warning message: package ‘partykit’ is not available (for R version 3.2.1) When I look up the partykit documentation, it says that it is supported on R (>= 3.1.0), so I don't know why it won't let me download / use it. – JGM Dec 02 '15 at 13:21
  • I also tried it on another computer running R 3.2.2 and that didn't work either. – JGM Dec 02 '15 at 13:51
  • Interesting, this appears to be a problem with the R-Forge repository. At the moment `available.packages(contrib.url("http://R-Forge.R-project.org", "source"))` produces an empty list of packages. Hence, `install.packages()` claims that no packages are available at all. But the packages are actually there. You can manually download the `.tar.gz` or the `.zip` package from https://R-Forge.R-project.org/R/?group_id=261 and then install it on your machine. – Achim Zeileis Dec 02 '15 at 17:06