/
geom-rect.R
210 lines (179 loc) · 6.94 KB
/
geom-rect.R
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' An extension of \code{geom_rect} into the third dimension.
#'
#' @inheritSection geom_bar_z Z Offset Geom
#' @inheritSection geom_bar_z Geom Supports Extrusion
#' @family zoffset geoms
#' @family geoms supporting extrusion
#'
#' @inheritParams geom_polygon_z
#' @param mapping,data,stat,position,...,linejoin,na.rm,show.legend,inherit.aes
#' See documentation for \code{ggplot2::geom_segment}
#'
#' @import ggplot2
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
geom_rect_z <- function(mapping = NULL, data = NULL,
stat = "identity", position = "identity",
...,
linejoin = "mitre",
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE,
extrude = FALSE,
material = list(),
keep2d = FALSE) {
layer(
data = data,
mapping = mapping,
stat = stat,
geom = GeomRectZ,
position = position,
show.legend = show.legend,
inherit.aes = inherit.aes,
params = list(
linejoin = linejoin,
na.rm = na.rm,
extrude = extrude,
material = material,
keep2d = keep2d,
...
)
)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' GeomRectZ
#'
#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @import ggplot2
#' @import grid
#' @export
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GeomRectZ <- ggplot2::ggproto(
"GeomRectZ",
ggplot2::Geom,
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# include extrusion aesthetics as part of the default aes()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
default_aes = augment_aes(
extrusion_aesthetics,
aes(
colour = NA,
fill = "grey35",
size = 0.5,
linetype = 1,
alpha = NA
)
),
required_aes = c("xmin", "xmax", "ymin", "ymax", 'z'),
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
draw_panel = function(self, data, panel_params, coord, linejoin = "mitre",
extrude = FALSE, material = list(), keep2d = FALSE) {
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Keep original 2d Polygons?
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (isTRUE(keep2d)) {
grob2d <- GeomRect$draw_panel(data, panel_params, coord)
} else {
grob2d <- ggplot2::zeroGrob()
}
aesthetics <- setdiff(
names(data), c("x", "y", "xmin", "xmax", "ymin", "ymax", "z")
)
if (!coord$is_linear()) {
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 3d Polygons
# in ggplot, rects are promoted to polygons and then drawn using
# geom_polygon. However, polygon drawing in 3d can be slow (as we have
# to use triangular, so let's do something different in ggrgl).
# However, when the coord is not linear, then we need to do
# the slow expensive way e.g. for pie charts
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
polys3d <- lapply(split(data, seq_len(nrow(data))), function(row) {
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax)
aes <- new_data_frame(row[aesthetics])[rep(1,5), ]
aes$z <- rep(row$z, each = 5)
GeomPolygonZ$draw_panel(cbind(poly, aes), panel_params, coord,
extrude = extrude, material = material, keep2d = keep2d)
})
res <- ggname("rect", do.call(grid::grobTree, polys3d))
return(res)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Draw a quad3d for each rect
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
munched <- coord_munch(coord, data, panel_params)
alpha <- rep(munched$alpha, each = 4)
imunched <- data.frame(
x = with(munched, interleave(xmin, xmax, xmax, xmin)),
y = with(munched, interleave(ymin, ymin, ymax, ymax)),
z = rep(munched$z, each = 4),
id = rep(seq(nrow(munched)), each = 4),
extrude_face_fill = rep(munched$extrude_face_fill , each = 4),
extrude_face_alpha = rep(munched$extrude_face_alpha , each = 4),
extrude_edge_colour = rep(munched$extrude_edge_colour, each = 4),
extrude_edge_alpha = rep(munched$extrude_edge_alpha , each = 4),
extrude_edge_size = rep(munched$extrude_edge_size , each = 4),
extrude_z = rep(munched$extrude_z , each = 4)
)
rects_call <- cryogenic::capture_call(
quads3d(
x = imunched$x,
y = imunched$y,
z = imunched$z,
color = rep(munched$fill, each = 4),
alpha = ifelse(is.na(alpha), 1, alpha)
),
defaults = material
)
alpha <- ifelse(is.na(alpha), 1, alpha)
colour <- rep(munched$colour, each = 4)
alpha <- ifelse(is.na(colour), 0, alpha)
outlines_call <- cryogenic::capture_call(
quads3d(
x = imunched$x,
y = imunched$y,
z = imunched$z,
color = colour,
alpha = alpha,
front = 'lines',
back = 'lines'
),
defaults = material
)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create extrusion
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
extrusion <- list(faces_grob = ggplot2::zeroGrob(), edges_grob = ggplot2::zeroGrob())
if (isTRUE(extrude)) {
extrusion <- create_extrusion(imunched, closed = TRUE)
}
rects_grob <- snowcrash::encode_robj_to_rasterGrob(rects_call)
outlines_grob <- snowcrash::encode_robj_to_rasterGrob(outlines_call)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Put them all together
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ggname("rect", grid::grobTree(
grob2d,
extrusion$faces_grob, extrusion$edges_grob,
rects_grob, outlines_grob
))
},
draw_key = draw_key_polygon
)
# Convert rectangle to polygon
# Useful for non-Cartesian coordinate systems where it's easy to work purely in
# terms of locations, rather than locations and dimensions. Note that, though
# `polygonGrob()` expects an open form, closed form is needed for correct
# munching (c.f. https://github.com/tidyverse/ggplot2/issues/3037#issuecomment-458406857).
#
# @keyword internal
rect_to_poly <- function(xmin, xmax, ymin, ymax) {
new_data_frame(list(
y = c(ymax, ymax, ymin, ymin, ymax),
x = c(xmin, xmax, xmax, xmin, xmin)
))
}