File: geom_edge_span.R

package info (click to toggle)
r-cran-ggraph 2.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,832 kB
  • sloc: cpp: 1,630; makefile: 2
file content (178 lines) | stat: -rw-r--r-- 6,348 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
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
#' Draw edges as vertical spans
#'
#' This edge geom is mainly intended for use with [fabric][layout_tbl_graph_fabric]
#' layouts. It draws edges as vertical segments with an optional end shape
#' adornment. Due to the special nature of fabric layouts where nodes are not
#' a single point in space but a line, this geom doesn't derive the x position
#' from the location of the terminal nodes, but defaults to using the `edge_x`
#' variable calculated by the fabric layout. If this geom is used with other
#' layouts `x`and `xend` must be given explicitly.
#'
#' @inheritSection geom_edge_link Edge variants
#' @inheritSection geom_edge_link Edge aesthetic name expansion
#'
#' @section Aesthetics:
#' `geom_edge_span` and `geom_edge_span0` understand the following
#' aesthetics. Bold aesthetics are automatically set, but can be overwritten.
#'
#' - **x**
#' - **y**
#' - **xend**
#' - **yend**
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - filter
#'
#' `geom_edge_span2` understand the following aesthetics. Bold aesthetics are
#' automatically set, but can be overwritten.
#'
#' - **x**
#' - **y**
#' - **group**
#' - edge_colour
#' - edge_width
#' - edge_linetype
#' - edge_alpha
#' - filter
#'
#' `geom_edge_span` and `geom_edge_span2` 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
#'
#' @param end_shape The adornment to put at the ends of the span. The naming
#' follows the conventions of the shape aesthetic in [ggplot2::geom_point()]
#'
#' @author Thomas Lin Pedersen
#'
#' @family geom_edge_*
#'
#' @examples
#' require(tidygraph)
#' gr <- play_smallworld(n_dim = 3, dim_size = 3, order = 1, p_rewire = 0.6)
#'
#' # Standard use
#' ggraph(gr, 'fabric', sort.by = node_rank_fabric()) +
#'   geom_node_range(colour = 'grey80') +
#'   geom_edge_span()
#'
#' # Add end shapes
#' ggraph(gr, 'fabric', sort.by = node_rank_fabric()) +
#'   geom_node_range(colour = 'grey80') +
#'   geom_edge_span(end_shape = 'circle')
#'
#' # If the layout include shadow edges these can be styled differently
#' ggraph(gr, 'fabric', sort.by = node_rank_fabric(), shadow.edges = TRUE) +
#'   geom_node_range(colour = 'grey80') +
#'   geom_edge_span(aes(colour = shadow_edge), end_shape = 'square') +
#'   scale_edge_colour_manual(values = c('FALSE' = 'black', 'TRUE' = 'grey'))
#'
#' @rdname geom_edge_span
#' @name geom_edge_span
#'
NULL

#' @rdname geom_edge_span
#'
#' @importFrom ggforce StatLink
#' @export
geom_edge_span <- function(mapping = NULL, data = get_edges('short'),
                           position = 'identity', end_shape = NA, 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 = edge_x, y = y, xend = edge_x,
                                        yend = yend, group = edge.id))
  layer(
    data = data, mapping = mapping, stat = StatEdgeLink,
    geom = GeomEdgeSpanPath, 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, end_shape = end_shape,
        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 geom_edge_span
#'
#' @importFrom ggforce StatLink2
#' @export
geom_edge_span2 <- function(mapping = NULL, data = get_edges('long'),
                            position = 'identity', end_shape = NA, 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 = edge_x, y = y,
                                        group = edge.id))
  layer(
    data = data, mapping = mapping, stat = StatEdgeLink2,
    geom = GeomEdgeSpanPath, 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 = TRUE, end_shape = end_shape,
        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 geom_edge_span
#'
#' @importFrom ggforce StatLink2
#' @export
geom_edge_span0 <- function(mapping = NULL, data = get_edges(),
                            position = 'identity', end_shape = NA, arrow = NULL,
                            lineend = 'butt', show.legend = NA, ...) {
  mapping <- complete_edge_aes(mapping)
  mapping <- aes_intersect(mapping, aes(x = edge_x, y = y,
                                        xend = edge_x, yend = yend))
  layer(
    data = data, mapping = mapping, stat = StatFilter,
    geom = GeomEdgeSpanSegment, position = position,
    show.legend = show.legend, inherit.aes = FALSE,
    params = expand_edge_aes(
      list2(end_shape = end_shape, arrow = arrow, lineend = lineend, ...)
    )
  )
}