It is not completely straightforward to do this because one would have to tweak the panel functions to do different things in different panels. For example, you only need the meters in the two edges from node 1 and you only need the y-axis label on the left of node 2. Of course, you could also repeat the y-axis label in nodes 4 and 5 but it would be redundant. That was the main motivation for omitting it altogether to avoid visual "clutter".
Hence, my recommendation is to first create the plot as you did above and then manually add the bits and pieces you need. To do so, one can leverage the grid
system of so-called viewport
s. These are the plotting regions created for the inner nodes, the edges, and all terminal nodes. By default the viewpors are deleted after creation of the plot (called pop
ping viewports in grid
jargon). But you can simply keep them by setting pop = FALSE
. All viewports then have fairly simply names that you can use to navigate.
For a reproducible example I use the cars
data in R which also yield a tree with three terminal nodes:
library("partykit")
ct <- ctree(dist ~ speed, data = cars)
The plot can then be set up as in your example just with the additional pop = FALSE
at the end:
plot(ct,
main = expression('Suitable Brook Trout Habitat (m'^2*'/100m'^2*')'),
inner_panel = node_inner(ct, fill = c("white"), id = FALSE),
terminal_panel = node_boxplot(ct, col = "black", fill = "lightgray",
width = 0.5, yscale = NULL, ylines = 3, cex = 0.5, id = FALSE),
pop = FALSE
)
For the y-axis label in node 3 we can jump to the viewport with label "node_boxplot3plot"
. (In your case node 2
instead of 3
, of course.) Then we can use grid.text()
to add the y-axis label. The coordinates are -3 lines (of text) horizontally and the middle (0.5 normalized parent coordinates) vertically:
seekViewport("node_boxplot3plot")
grid.text("Hello World!",
x = unit(-3, "lines"), y = unit(0.5, "npc"), rot = 90)
Finally, we add the "m"
in the first and second edge labels from node 1. These are called "edge1-1"
and "edge1-2"
, respectively. Now the horizontal position is again the middle of that viewport (o.5 npc) plus the width of the string "< 17"
("< 16.4"
in your case):
seekViewport("edge1-1")
grid.text("m", x = unit(0.5, "npc") + unit(1, "strwidth", "< 17"))
seekViewport("edge1-2")
grid.text("m", x = unit(0.5, "npc")+ unit(1, "strwidth", "> 17"))
Together this yields:
