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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#' Draw edges as diagonals
#'
#' This geom draws edge loops (edges starting and ending at the same node).
#' Loops are drawn as bezier curves starting and ending at the position of the
#' node and with control points protruding at an angle and in a direction
#' specified in the call. As the start and end node is always the same no *2
#' method is provided. Loops can severely clutter up your visualization which is
#' why they are decoupled from the other edge drawings. Only plot them if they
#' are of importance. If the graph doesn't contain any loops the geom adds
#' nothing silently.
#'
#' @inheritSection geom_edge_link Edge variants
#' @inheritSection geom_edge_link Edge aesthetic name expansion
#'
#' @section Aesthetics:
#' `geom_edge_loop` and `geom_edge_loop0` understand the following
#' aesthetics. Bold aesthetics are automatically set, but can be overwritten.
#'
#' - **x**
#' - **y**
#' - **from**
#' - **to**
#' - **span** *90*
#' - **direction** *45*
#' - **strength** *1*
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - filter
#'
#' `geom_edge_loop` furthermore takes the following aesthetics.
#'
#' - start_cap
#' - end_cap
#' - label
#' - label_pos
#' - label_size
#' - angle
#' - hjust
#' - vjust
#' - family
#' - fontface
#' - lineheight
#'
#' @section Computed variables:
#'
#' \describe{
#' \item{index}{The position along the path (not computed for the *0 version)}
#' }
#'
#' @inheritParams geom_edge_link
#' @inheritParams ggplot2::geom_path
#'
#' @author Thomas Lin Pedersen
#'
#' @family geom_edge_*
#'
#' @examples
#' require(tidygraph)
#' gr <- as_tbl_graph(
#' data.frame(from = c(1, 1, 2, 2, 3, 3, 3), to = c(1, 2, 2, 3, 3, 1, 2))
#' )
#'
#' ggraph(gr, 'stress') +
#' geom_edge_loop(aes(alpha = after_stat(index))) +
#' geom_edge_fan(aes(alpha = after_stat(index)))
#'
#' ggraph(gr, 'stress') +
#' geom_edge_loop0() +
#' geom_edge_fan0()
#' @rdname geom_edge_loop
#' @name geom_edge_loop
#'
NULL
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom ggforce StatBezier
#' @export
StatEdgeLoop <- ggproto('StatEdgeLoop', StatBezier,
setup_data = function(data, params) {
data <- StatFilter$setup_data(data, params)
if (nrow(data) == 0) return(data)
data <- data[data$from == data$to, ]
data$group <- make_unique(data$group)
if (nrow(data) != 0) {
create_loops(data, params)
} else {
data
}
},
required_aes = c('x', 'y', 'from', 'to', 'span', 'direction', 'strength'),
default_aes = aes(filter = TRUE),
extra_params = c('na.rm', 'n')
)
#' @rdname geom_edge_loop
#'
#' @export
geom_edge_loop <- function(mapping = NULL, data = get_edges(),
position = 'identity', arrow = NULL, n = 100,
lineend = 'butt', linejoin = 'round', linemitre = 1,
label_colour = 'black', label_alpha = 1,
label_parse = FALSE, check_overlap = FALSE,
angle_calc = 'rot', force_flip = TRUE,
label_dodge = NULL, label_push = NULL,
show.legend = NA, ...) {
mapping <- complete_edge_aes(mapping)
mapping <- aes_intersect(mapping, aes(
x = x, y = y, from = from, to = to, group = edge.id,
span = 90, direction = 45, strength = 1
))
layer(
data = data, mapping = mapping, stat = StatEdgeLoop,
geom = GeomEdgePath, position = position, show.legend = show.legend,
inherit.aes = FALSE,
params = expand_edge_aes(
list2(
arrow = arrow, lineend = lineend, linejoin = linejoin,
linemitre = linemitre, n = n, interpolate = FALSE,
label_colour = label_colour, label_alpha = label_alpha,
label_parse = label_parse, check_overlap = check_overlap,
angle_calc = angle_calc, force_flip = force_flip,
label_dodge = label_dodge, label_push = label_push, ...
)
)
)
}
#' @rdname ggraph-extensions
#' @format NULL
#' @usage NULL
#' @importFrom ggforce StatBezier0
#' @export
StatEdgeLoop0 <- ggproto('StatEdgeLoop0', StatBezier0,
setup_data = function(data, params) {
StatEdgeLoop$setup_data(data, params)
},
required_aes = c('x', 'y', 'from', 'to', 'span', 'direction', 'strength'),
default_aes = aes(filter = TRUE),
extra_params = c('na.rm')
)
#' @rdname geom_edge_loop
#'
#' @export
geom_edge_loop0 <- function(mapping = NULL, data = get_edges(),
position = 'identity', arrow = NULL,
lineend = 'butt', show.legend = NA, ...) {
mapping <- complete_edge_aes(mapping)
mapping <- aes_intersect(mapping, aes(
x = x, y = y, from = from, to = to,
span = 90, direction = 45, strength = 1
))
layer(
data = data, mapping = mapping, stat = StatEdgeLoop0,
geom = GeomEdgeBezier, position = position, show.legend = show.legend,
inherit.aes = FALSE,
params = expand_edge_aes(
list2(arrow = arrow, lineend = lineend, ...)
)
)
}
create_loops <- function(loops, params) {
control_angle1 <- loops$direction - loops$span / 2
control_angle2 <- loops$direction + loops$span / 2
controls1 <- find_loop_controls(loops, control_angle1)
controls2 <- find_loop_controls(loops, control_angle2)
end <- loops
bezier_start <- seq(1, by = 4, length.out = nrow(loops))
loops$index <- bezier_start
controls1$index <- bezier_start + 1
controls2$index <- bezier_start + 2
end$index <- bezier_start + 3
loops <- vec_rbind(loops, controls1, controls2, end)
loops[order(loops$index), names(loops) != 'index']
}
find_loop_controls <- function(loops, angle) {
angle <- angle / 360 * 2 * pi
loops$x <- loops$x + cos(angle) * loops$strength
loops$y <- loops$y + sin(angle) * loops$strength
loops
}
|