在 rpart.plot 或 fancyRpartPlot 中使用带下标的希腊字母作为标签?

问题描述 投票:0回答:1

我有以下 MWE:

library(rpart.plot)
library(rpart)

set.seed(1234)
df <- data.frame(nu = c(rnorm(mean = 1, n = 100), rnorm(mean = 2, n = 50)), rho = c(rnorm(n = 50),rnorm(sd=2,n=100)), cl = c(rep("TRUE",100), rep("FALSE",50)))
names(df)[-3] <- c(expression(nu),expression(rho))

上面的方法不起作用,所以我使用了unicode字体:

names(df)[-3] <- c("\u03BD","\u03C1")

所以我使用

rpart
包中的以下内容,然后使用
rpart
进行绘图。

tree <- rpart(cl~., data = df)
rpart.plot(tree, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn")

我得到:

但是,我的下一个目标是为

nu
rho
添加下标。

我尝试过:

names(df)[-3] <- c(expression("\u03BD"[1]) , expression("\u03C1"[2]))

tree <- rpart(cl~., data = df)
rpart.plot(tree, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn")

但是

rpart.plot(tree, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn")

给出:

rattle
的``fancyRplot`也出现同样的问题:

 fancyRpartPlot(tree, caption = NULL)

产生:

有没有办法在

rpart.plot
中包含下标 还是
fancyRpartPlot

r rpart rattle
1个回答
0
投票

rpart.plot()
似乎不支持表达式符号。但是,您可以使用 utf-8 下标 1下标 2 字符,
\u2081
\u2082

names(df)[1:2] <- c("\u03BD\u2081", "\u03C1\u2082")
rpart.plot(tree, type = 4, extra = 0, branch.lty = 3, box.palette = "RdYlGn")

© www.soinside.com 2019 - 2024. All rights reserved.