这个想法是指定一个用于绘制内部节点的面板函数。
我生成一些数据,然后树
lls <- data.frame(N = gl(3, 50, labels = c("A", "B", "C")),
a = rnorm(150) + rep(c(1, 0,150)),
b = runif(150))
pond= sample(1:5,150,replace=TRUE)
tt <- ctree(formula=N~a+b, data=lls,weights = pond)
自定义内部绘图函数。我画一个圆圈,在其中写下一些权重。
innerWeights <- function(node){
grid.circle(gp = gpar(fill = "White", col = 1))
mainlab <- paste( node$psplit$variableName, "\n(n = ")
mainlab <- paste(mainlab, sum(node$weights),")" , sep = "")
grid.text(mainlab,gp = gpar(col='red'))
}
我绘制了树
plot(tt, type='simple', inner_panel = innerWeights)
PS:结果取决于随机生成的数据,因此您可能不会得到相同的图。