You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中生成类似书籍中样式的详细决策树?

Getting a Nicer Decision Tree Visualization in R (vs. Base post())

Hey there! I totally get the frustration with the basic post() function in R—it’s pretty limited when it comes to making detailed, polished decision trees like the ones you’ve seen in that textbook. Let me walk you through some better options to replicate that style.

Ditch post(): Use the rpart.plot Package

This is hands down the easiest way to get professional-looking decision trees with all the details you want. It works seamlessly with rpart (the standard R package for decision trees) and lets you customize almost every part of the tree:

  • First, install and load the package:
    install.packages("rpart.plot")
    library(rpart.plot)
    
  • Build your decision tree model with rpart (example using the iris dataset):
    tree_model <- rpart(Species ~ ., data = iris)
    
  • Plot it with rpart.plot()—here are some parameters to get you close to that textbook style:
    rpart.plot(tree_model, 
               type = 4,          # Displays split labels below the nodes
               extra = 101,       # Shows class counts, percentages, and node numbers
               fallen.leaves = TRUE,  # Places leaf nodes at the bottom for cleaner layout
               box.palette = "Blues", # Custom color palette for nodes
               branch.lty = 1,    # Solid branch lines
               shadow.col = "gray") # Adds subtle shadow for depth
    

This will show way more than just those circles with XXXX/XXXX—you’ll get class distributions, split criteria, and a clean, readable layout that matches what you’ve seen in the book.

If You Really Want to Stick with Base R

The base post() function is pretty bare-bones, but you can try combining plot() and text() to add more details manually (though it’s clunky):

plot(tree_model, uniform = TRUE, branch = 0.6, margin = 0.1)
text(tree_model, use.n = TRUE, all = TRUE, cex = 0.8)

This will add sample counts to nodes, but it still won’t look as polished as the rpart.plot output. The ... in the post() documentation refers to parameters passed to the postscript() function (like file size, resolution), not tree visualization settings—so there’s no hidden magic there to make the tree nicer.

What About SAS?

SAS’s PROC TREE or PROC CLUSTER can generate very polished decision trees, with built-in options for displaying class percentages, split rules, and custom styling. But since you’re already working in R, the rpart.plot package will get you the same (if not better) results without switching tools.

Quick Note on the Textbook Tree

Chances are that textbook’s tree was either generated in SAS (since it’s common in academic stats texts) or using a tool like Graphviz (which R can also interface with via the DiagrammeR package, though rpart.plot is simpler for decision trees).

Don’t feel bad about struggling with that textbook—lots of us rely on external resources to fill in gaps where textbooks fall short. This visualization fix is a quick win once you use the right package!

内容的提问来源于stack exchange,提问作者Chelsea

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:43:41