首页 > 解决方案 > 使用 ggparty 仅在终端节点中绘制重要的预测线

问题描述

我正在使用ggparty 包来可视化基于线性模型的树的结果。一般来说,我认为这与包作者提供的示例代码很好地配合:

library(ggparty)

data("iris") #load iris dataset
data=iris

formula = as.formula(paste("Sepal.Length~Sepal.Width", paste(colnames(data)[-which(colnames(data) %in% c("Sepal.Length","Sepal.Width"))], collapse=" + "), sep=" | ")) # creating a formula

lmtree <- lmtree(formula, data = data) #tree based on linear model

#plot example code provided in the package description (https://cran.r-project.org/web/packages/ggparty/ggparty.pdf)
ggparty(lmtree,
        terminal_space = 0.4,
        add_vars = list(p.value = "$node$info$p.value")) +
  geom_edge(size = 1) +
  geom_edge_label(colour = "grey", size = 5) +
  geom_node_plot(gglist = list(geom_point(aes(x = Sepal.Width,
                                              y = Sepal.Length),
  alpha = 0.5),
  theme_bw(base_size = 12)),
  scales = "fixed",
  ids = "terminal",
  shared_axis_labels = T,
  shared_legend = T,
  legend_separator = T,
  predict = "Sepal.Width",
  predict_gpar = list(col = "blue",
                      size = 1.2)) +
  geom_node_label(aes(col = splitvar),
                  line_list = list(aes(label = paste("Node", id)),
                                   aes(label = splitvar),
                                   aes(label = paste("p =", formatC(p.value, format = "e", digits = 2)))),
                  line_gpar = list(list(size = 10, col = "black", fontface = "bold"),
                                   list(size = 12),
                                   list(size = 10)),
                  ids = "inner") +
  geom_node_label(aes(label = paste0("Node ", id, ", N = ", nodesize)),
                  fontface = "bold",
                  ids = "terminal",
                  size = 4, 
                  nudge_y = 0.01) +
  theme(legend.position = "none")

但是,现在我只想在找到的关系显着(p<0.05)时才在终端节点中绘制预测线。此外,最好在各个终端节点图中绘制线性关系的R2p 值。

如果我需要提供有关此问题的更多信息,请告诉我。预先感谢您的帮助!

标签: r

解决方案


推荐阅读