File: dendro_rpart.R

package info (click to toggle)
r-cran-ggdendro 0.1.22%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 368 kB
  • sloc: sh: 13; makefile: 2
file content (153 lines) | stat: -rw-r--r-- 5,876 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#
#  ggdendro/R/dendro_rpart.R by Andrie de Vries  Copyright (C) 2011-2015
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 or 3 of the License
#  (at your option).
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  A copy of the GNU General Public License is available at
#  http://www.r-project.org/Licenses/
#
# Classification Tree with rpart


#' Extract data from classification tree object for plotting using ggplot.
#'
#' Extracts data to plot line segments and labels from a
#' [rpart::rpart()] classification tree object.  This data can then be
#' manipulated or plotted, e.g. using [ggplot2::ggplot()].
#'
#' This code is in essence a copy of [rpart::plot.rpart()], retaining
#' the plot data but without plotting to a plot device.
#'
#' @param model object of class "tree", e.g. the output of tree()
#'
#' @param uniform if TRUE, uniform vertical spacing of the nodes is used; this
#'   may be less cluttered when fitting a large plot onto a page. The default is
#'   to use a non-uniform spacing proportional to the error in the fit.
#'
#' @param branch controls the shape of the branches from parent to child node.
#'   Any number from 0 to 1 is allowed. A value of 1 gives square shouldered
#'   branches, a value of 0 give V shaped branches, with other values being
#'   intermediate.
#'
#' @param compress if FALSE, the leaf nodes will be at the horizontal plot
#'   coordinates of 1:nleaves. If TRUE, the routine attempts a more compact
#'   arrangement of the tree. The compaction algorithm assumes uniform=TRUE;
#'   surprisingly, the result is usually an improvement even when that is not
#'   the case.
#'
#' @param nspace the amount of extra space between a node with children and a
#'   leaf, as compared to the minimal space between leaves. Applies to
#'   compressed trees only. The default is the value of branch.
#'
#' @param minbranch	set the minimum length for a branch to minbranch times the
#'   average branch length. This parameter is ignored if uniform=TRUE. Sometimes
#'   a split will give very little improvement, or even (in the classification
#'   case) no improvement at all. A tree with branch lengths strictly
#'   proportional to improvement leaves no room to squeeze in node labels.
#'
#' @param ... ignored
#' @export
#' @return
#' A list of three data frames:
#' \item{segments}{a data frame containing the line segment data}
#' \item{labels}{a data frame containing the label text data}
#' \item{leaf_labels}{a data frame containing the leaf label text data}
#'
#' @seealso [ggdendrogram()]
#' @family dendro_data methods
#' @family rpart functions
#' @example inst/examples/example_dendro_rpart.R
#'
dendro_data.rpart <- function(model, uniform = FALSE, branch = 1, compress = FALSE,
                              nspace, minbranch = 0.3, ...) {
  x <- model
  if (!inherits(x, "rpart")) stop("Not a legitimate \"rpart\" object")
  if (nrow(x$frame) <= 1L) stop("fit is not a tree, just a root")

  if (compress & missing(nspace)) nspace <- branch
  if (!compress) nspace <- -1L # means no compression
  # if(!interactive()) if (dev.cur() == 1L) dev.new() # not needed in R

  parms <- list(
    uniform = uniform,
    branch = branch,
    nspace = nspace,
    minbranch = minbranch
  )

  ## define the plot region
  temp <- rpartco(x, parms = parms)
  xx <- temp$x
  yy <- temp$y
  # temp1 <- range(xx) + diff(range(xx)) * c(-margin, margin)
  # temp2 <- range(yy) + diff(range(yy)) * c(-margin, margin)
  #   plot(temp1, temp2, type = "n", axes = FALSE, xlab = "", ylab = "", ...)
  ## Save information per device, once a new device is opened.
  # assign(paste0("device", dev.cur()), parms, envir = rpart_ggdendro_env)

  # Draw a series of horseshoes or V's, left son, up, down to right son
  #   NA's in the vector cause lines() to "lift the pen"
  node <- as.numeric(row.names(x$frame))
  temp <- rpart.branch(xx, yy, node, branch)

  #   if (branch > 0) text(xx[1L], yy[1L], "|")
  #   lines(c(temp$x), c(temp$y))
  #   invisible(list(x = xx, y = yy))

  labels <- text.rpart(x, parms = parms)

  segments <- rpart_segments(temp)
  #   labels <- rpart_labels(xx, ...)
  as.dendro(
    segments = segments,
    labels = labels$labels,
    leaf_labels = labels$leaf_labels,
    class = "rpart"
  )
}

#' Extract data frame from rpart object for plotting using ggplot.
#' @param model object of class "tree", e.g. the output of tree()
#' @param ... ignored
#' @keywords internal
#' @seealso [ggdendrogram()]
#' @family rpart functions
rpart_segments <- function(x, ...) {
  dat <- data.frame(
    stack(as.data.frame(x$x)),
    stack(as.data.frame(x$y))
  )[, c("ind", "values", "values.1")]

  dat <- cbind(head(dat, -1), tail(dat, -1))
  dat <- dat[complete.cases(dat), -4]
  names(dat) <- c("n", "x", "y", "xend", "yend")
  dat
}


#' Extract labels data frame from rpart object for plotting using ggplot.
#'
#' This code is modified from the original plot.rpart in package rpart.
#' @param model object of class "rpart", e.g. the output of rpart()
#' @param ... ignored
#' @return a list with two elements: $labels and $leaf_labels
#' @author Original author Brian Ripley
#' @keywords internal
#' @seealso [ggdendrogram()]
#' @family dendro_data methods
#' @family rpart functions
rpart_labels <- function(x) {
  labelSplits <- labelLeaves <- NULL
  list(
    labels = labelSplits,
    leaf_labels = labelLeaves
  )
}