Is there any way to manipulate the titles of a ctree plot?

假如想象 提交于 2019-12-01 11:42:28

There is good news and bad news.

So the plot() function that's actually doing all the work there is party:::plot.BinaryTree. The help is available from ?plot.BinaryTree but the bad news is it doesn't have any easily accessible parameters for font formatting. However, the good news is that the function uses grid graphics to draw to the screen and you can update properties after you've created the plot.

So after you run

library(party)  
urp<-ctree(a~., data=data.frame(a,p))
plot(urp, main = "Broken Title")

You can run

for(gg in grid.ls(print=F)[[1]]) {
   if (grepl("text", gg)) {
       print(paste(gg, grid.get(gg)$label,sep=": "))
   }
}

to see all the text boxes on the plot. For example, I see

[1] "GRID.text.673: Broken Title"
[1] "GRID.text.677: X1"
[1] "GRID.text.678: p = 0.03"
[1] "GRID.text.680: 1"
[1] "GRID.text.682: phantom(0) <= 1"
[1] "GRID.text.684: phantom(0) > 1"
[1] "GRID.text.686: Node 2 (n = 8)"
[1] "GRID.text.697: Node 3 (n = 109)"

Here i see the node names, and the text they contain. Note that the node names are not the same from plot to plot and change everything you draw the same plot. But you can use this list to find the ones you want to change and update them. So if I wanted to make the main text bigger, I would run

grid.edit("GRID.text.673", gp=gpar(fontsize=20))

or If i wanted to italize the node labels i would run

grid.edit("GRID.text.686", gp=gpar(fontface=3))
grid.edit("GRID.text.697", gp=gpar(fontface=3))

and that gives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!