In a recent SO discussion I displayed a binary classification tree that needed some pruning of vertices 6 and 7:
Below is the code I used:
KaryTree[9, 2,
VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?",
4 -> "Sinus tachycardia ?", 8 -> "< 30 days"},
EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes",
1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes",
2 \[UndirectedEdge] 5 -> "no", 4 \[UndirectedEdge] 8 -> "yes",
4 \[UndirectedEdge] 9 -> "no"}, ImagePadding -> 20]
If leaves 6 and 7 are pruned by VertexDelete
, vertices 8 and 9 are also clipped:
VertexDelete[
KaryTree[7, 2,
VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?",
4 -> "Has sinus tachycardia ?"},
EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes",
1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes",
2 \[UndirectedEdge] 5 -> "no"}, ImagePadding -> 20], {6, 7}]
TreeGraph is willing to plot the graph, but it has its own mind about how the graph should be laid out:
TreeGraph[{1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3,
2 \[UndirectedEdge] 4, 2 \[UndirectedEdge] 5, 4 \[UndirectedEdge] 6,
4 \[UndirectedEdge] 7},
VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?",
4 -> "Has sinus tachycardia ?", 6 -> "< 30 days"},
EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes",
1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes",
2 \[UndirectedEdge] 5 -> "no", 4 \[UndirectedEdge] 6 -> "yes",
4 \[UndirectedEdge] 7 -> "no"}, ImagePadding -> 20]
I'd like vertex 1 to be displayed at the top as the root of the graph.
I've played around with various GraphLayout
settings but not found a solution. Any ideas?