按颜色匹配节点列表为 rpart.plot 中的节点着色
Colour nodes in rpart.plot by list of colours matching nodes
在函数rpart.plot(来自包rpart.plot,扩展到rpart 包)中有参数box.col
,它控制树中节点的颜色。我如何设置它以便为节点着色,以便相同响应的节点具有相同的颜色?
我尝试了多种使用 box.col
参数的不同变体,例如使用 cols
作为因素,这会忽略所选的颜色。到目前为止我最接近的如下所示
set.seed(1);x <- runif(100)
set.seed(2);y <- runif(100)
data <- matrix(c(x,y),ncol=2)
fact <- as.numeric(factor(--((x > 0.5 & y < 0.5))))
fact[x < 0.1] = 3
cols <- (c("grey80", "red", "blue"))
plot(data, col=fact)
t1 <- rpart(factor(fact) ~ data)
rpart.plot(t1, type=5, extra=2,
box.col=cols)
我 expect/want 每个匹配的响应节点都被着色相同。在给定的代码中,我希望“1”节点为 grey80,“2”为红色,“3”为蓝色。上面的图显示了实际发生的情况,这没有帮助。
如第一部分所述,我如何设置它以便为节点着色,以便相同响应的节点着色相同?
如何使用 box.pallete
属性
rpart.plot(t1, type=5, extra=2,
box.palette = list('grey80','red','blue'))
更详细的解释,如果你在rpart.plot
中将cols设置为factor
,cols将按顺序分配给每个节点。
例如,
>t1
1) root 100 29 1 (0.7100000 0.2200000 0.0700000) # --> 'grey80'
2) data1>=0.1037049 93 22 1 (0.7634409 0.2365591 0.0000000) # --> 'red'
4) data1< 0.5413779 47 0 1 (1.0000000 0.0000000 0.0000000) * # --> 'blue'
5) data1>=0.5413779 46 22 1 (0.5217391 0.4782609 0.0000000) # --> 'grey80'
10) data2>=0.4849942 24 0 1 (1.0000000 0.0000000 0.0000000) * # --> 'red'
11) data2< 0.4849942 22 0 2 (0.0000000 1.0000000 0.0000000) *# --> 'blue'
3) data1< 0.1037049 7 0 3 (0.0000000 0.0000000 1.0000000) * # --> 'grey 80'
在函数rpart.plot(来自包rpart.plot,扩展到rpart 包)中有参数box.col
,它控制树中节点的颜色。我如何设置它以便为节点着色,以便相同响应的节点具有相同的颜色?
我尝试了多种使用 box.col
参数的不同变体,例如使用 cols
作为因素,这会忽略所选的颜色。到目前为止我最接近的如下所示
set.seed(1);x <- runif(100)
set.seed(2);y <- runif(100)
data <- matrix(c(x,y),ncol=2)
fact <- as.numeric(factor(--((x > 0.5 & y < 0.5))))
fact[x < 0.1] = 3
cols <- (c("grey80", "red", "blue"))
plot(data, col=fact)
t1 <- rpart(factor(fact) ~ data)
rpart.plot(t1, type=5, extra=2,
box.col=cols)
如第一部分所述,我如何设置它以便为节点着色,以便相同响应的节点着色相同?
如何使用 box.pallete
属性
rpart.plot(t1, type=5, extra=2,
box.palette = list('grey80','red','blue'))
更详细的解释,如果你在rpart.plot
中将cols设置为factor
,cols将按顺序分配给每个节点。
例如,
>t1
1) root 100 29 1 (0.7100000 0.2200000 0.0700000) # --> 'grey80'
2) data1>=0.1037049 93 22 1 (0.7634409 0.2365591 0.0000000) # --> 'red'
4) data1< 0.5413779 47 0 1 (1.0000000 0.0000000 0.0000000) * # --> 'blue'
5) data1>=0.5413779 46 22 1 (0.5217391 0.4782609 0.0000000) # --> 'grey80'
10) data2>=0.4849942 24 0 1 (1.0000000 0.0000000 0.0000000) * # --> 'red'
11) data2< 0.4849942 22 0 2 (0.0000000 1.0000000 0.0000000) *# --> 'blue'
3) data1< 0.1037049 7 0 3 (0.0000000 0.0000000 1.0000000) * # --> 'grey 80'