如何在R中编辑ctree标签 [英] How to edit ctree labels in R

查看:193
本文介绍了如何在R中编辑ctree标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个ctree,并且想要对其进行进一步的编辑.

I have a ctree produced and am wanting to make further edits on it.

我的代码是:

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

我想在框线图的y轴和诸如16.4的变量上添加标签(希望为16.4 m).

I would like to add labels to the y axis on the boxplots and to the variables such as 16.4 (would like it to be 16.4 m).

任何帮助将不胜感激.

推荐答案

执行此操作并不完全简单,因为必须调整面板功能才能在不同的面板中执行不同的操作.例如,您只需要节点1的两个边缘中的仪表,节点2的左侧只需要y轴标签.当然,您也可以在节点4和5中重复y轴标签,但是将是多余的.这是完全省略它以避免视觉混乱"的主要动机.

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".

因此,我的建议是首先像上面一样创建图,然后手动添加所需的点点滴滴.为此,可以利用所谓的viewportgrid系统.这些是为内部节点,边缘和所有终端节点创建的绘图区域.默认情况下,在创建图后将删除视口(在grid行话中称为pop ping视口).但是您只需设置pop = FALSE即可保留它们.这样,所有视口都有相当简单的名称,可用于导航.

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 viewports. 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 popping 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.

对于一个可重现的示例,我在R中使用了cars数据,该数据还生成了具有三个终端节点的树:

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)

然后可以像您的示例中一样设置图,只需要在末尾添加其他pop = FALSE:

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
)

对于节点3中的y轴标签,我们可以跳转到带有标签"node_boxplot3plot"的视口. (当然,在您的情况下为节点2而不是3.)然后,我们可以使用grid.text()添加y轴标签.坐标水平为-3行(文本),垂直为中间(0.5归一化父坐标):

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)

最后,我们在节点1的第一和第二边缘标签中添加"m".它们分别称为"edge1-1""edge1-2".现在,水平位置再次位于该视口的中间(o.5 npc)加上字符串"< 17"(在您的情况下为"< 16.4")的宽度:

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

总共产生:

这篇关于如何在R中编辑ctree标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆