如何更改树对象 class 或绘制漂亮的树
How to change tree object class or plot nicely trees
我需要为项目绘制一棵漂亮的树。问题是我必须使用特定的函数来创建创建树数据类型的树,并且所有用于以很好的方式绘制树的函数(ggtree,rpart.plot)都需要其他类型的对象。你能帮我改变树的对象数据类型或建议我其他函数来绘制它们吗?
我必须绘制 prune.result
。我不能使用其他功能来创建和修剪树。
(我知道我可以使用简单的 plot
函数,但我在所有项目中都使用了 ggplot 包,所以我想要类似的东西)。
提前致谢
dataframe <- data_frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)), x0 = rnorm(1000), x1 = rnorm(1000, 3), x2 = rnorm(1000, 3,0.5), x3 = rnorm(1000, 1,0.5), x4 = rnorm(1000, 3.5), x5 = rnorm(1000, 2.3,0.5), x6 = rnorm(1000, 1,0.5), x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)), x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))
library(tree)
tree = tree::tree(y ~., data=dataframe, split='gini', control = tree.control(nobs=dim(dataframe)[1],mincut=1,minsize=2))
prune.result = prune.misclass(tree, best =12)
我尝试将 prune.result 转换为数据帧以将其用于 ggtree,我还尝试将其转换为 rpart 对象,但我没有成功。
如果你需要使用 tree
包生成的树对象,但你想要 ggplot 输出,你应该查看 ggdendro
这是基于您的示例的完整表示:
set.seed(1)
dataframe <- data.frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)),
x0 = rnorm(1000),
x1 = rnorm(1000, 3),
x2 = rnorm(1000, 3,0.5),
x3 = rnorm(1000, 1,0.5),
x4 = rnorm(1000, 3.5),
x5 = rnorm(1000, 2.3,0.5),
x6 = rnorm(1000, 1,0.5),
x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)),
x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))
library(tree)
library(ggdendro)
tree <- tree(y ~ ., data = dataframe, split = 'gini',
control = tree.control(nobs = nrow(dataframe),
mincut = 1, minsize = 2))
prune.result <- prune.misclass(tree, best = 12)
ggdendrogram(prune.result, theme_dendro = FALSE)
由 reprex package (v2.0.1)
于 2022-03-28 创建
我需要为项目绘制一棵漂亮的树。问题是我必须使用特定的函数来创建创建树数据类型的树,并且所有用于以很好的方式绘制树的函数(ggtree,rpart.plot)都需要其他类型的对象。你能帮我改变树的对象数据类型或建议我其他函数来绘制它们吗?
我必须绘制 prune.result
。我不能使用其他功能来创建和修剪树。
(我知道我可以使用简单的 plot
函数,但我在所有项目中都使用了 ggplot 包,所以我想要类似的东西)。
提前致谢
dataframe <- data_frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)), x0 = rnorm(1000), x1 = rnorm(1000, 3), x2 = rnorm(1000, 3,0.5), x3 = rnorm(1000, 1,0.5), x4 = rnorm(1000, 3.5), x5 = rnorm(1000, 2.3,0.5), x6 = rnorm(1000, 1,0.5), x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)), x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))
library(tree)
tree = tree::tree(y ~., data=dataframe, split='gini', control = tree.control(nobs=dim(dataframe)[1],mincut=1,minsize=2))
prune.result = prune.misclass(tree, best =12)
我尝试将 prune.result 转换为数据帧以将其用于 ggtree,我还尝试将其转换为 rpart 对象,但我没有成功。
如果你需要使用 tree
包生成的树对象,但你想要 ggplot 输出,你应该查看 ggdendro
这是基于您的示例的完整表示:
set.seed(1)
dataframe <- data.frame(y = as.factor(rbinom(1000, size = 1, prob = 0.3)),
x0 = rnorm(1000),
x1 = rnorm(1000, 3),
x2 = rnorm(1000, 3,0.5),
x3 = rnorm(1000, 1,0.5),
x4 = rnorm(1000, 3.5),
x5 = rnorm(1000, 2.3,0.5),
x6 = rnorm(1000, 1,0.5),
x7 = as.factor(rbinom(1000, size = 4, prob = 0.5)),
x8 = as.factor(rbinom(1000, size = 3, prob = 0.8)))
library(tree)
library(ggdendro)
tree <- tree(y ~ ., data = dataframe, split = 'gini',
control = tree.control(nobs = nrow(dataframe),
mincut = 1, minsize = 2))
prune.result <- prune.misclass(tree, best = 12)
ggdendrogram(prune.result, theme_dendro = FALSE)
由 reprex package (v2.0.1)
于 2022-03-28 创建