【发布时间】:2020-09-01 03:46:59
【问题描述】:
我在网上找到了一张我试图在 R 中复制的图形。我已经成功地创建了下图的第一个图形,现在我正在尝试构建决策树。它仅用于说明目的,因此不会构成某些模型的一部分。
生成第一个图的代码:
x1 <- sample(c(1:100), 100, replace = TRUE)
x2 <- sample(c(1:100), 100, replace = TRUE)
d <- data.frame(x1, x2)
d %>%
mutate(
colours = case_when(
x1 < 50 & x2 >= 60 ~ "green",
x1 >= 50 & x2 >= 60 ~ "red",
x1 < 70 & x2 < 60 ~ "red",
x1 >= 70 & x2 < 20 ~ "red",
x1 > 70 & x2 > 20 ~ "green"
)
) %>%
ggplot(aes(x = x1, y = x2)) +
geom_point(aes( color = colours)) +
geom_line(data = data.frame(x = c(1, 100), y = c(60, 60)), aes(x = x , y = y), linetype = "dashed") +
annotate("text", label = "Split 1", x = 105, y = 60) +
geom_line(data = data.frame(x = c(50, 50), y = c(60, 100)), aes(x = x, y = y), linetype = "dashed") +
annotate("text", label = "Split 2", x = 50, y = 101) +
geom_line(data = data.frame(x = c(70, 70), y = c(1, 60)), aes(x = x, y = y), linetype = "dashed") +
annotate("text", label = "Split 3", x = 70, y = 61) +
geom_line(data = data.frame(x = c(70, 100), y = c(20, 20)), aes(x = x, y = y), linetype = "dashed") +
annotate("text", label = "Split 4", x = 105, y = 20)
这给出了:
我现在如何创建决策树部分?我正在研究 igraph 包,但在生成决策树模型时不知道从哪里开始。
【问题讨论】:
-
请参阅 cran.r-project.org/web/packages/ggparty/vignettes/… 以获取
ggparty包的小插图,该包从装有partykit包的树中绘制 ggplot 图形。
标签: r igraph decision-tree