|
8 | 8 | #' - `element_rect()`: borders and backgrounds.
|
9 | 9 | #' - `element_line()`: lines.
|
10 | 10 | #' - `element_text()`: text.
|
| 11 | +#' - `element_polygon()`: polygons. |
| 12 | +#' - `element_point()`: points. |
11 | 13 | #' - `element_geom()`: defaults for drawing layers.
|
12 | 14 | #'
|
13 | 15 | #' `rel()` is used to specify sizes relative to the parent,
|
|
18 | 20 | #' of the fill.
|
19 | 21 | #' @param colour,color Line/border colour. Color is an alias for colour.
|
20 | 22 | #' `alpha()` can be used to set the transparency of the colour.
|
21 |
| -#' @param linewidth,borderwidth Line/border size in mm. |
22 |
| -#' @param size,fontsize text size in pts. |
| 23 | +#' @param linewidth,borderwidth,stroke Line/border size in mm. |
| 24 | +#' @param size,fontsize,pointsize text size in pts, point size in mm. |
| 25 | +#' @param linetype,bordertype Line type for lines and borders respectively. An |
| 26 | +#' integer (0:8), a name (blank, solid, dashed, dotted, dotdash, longdash, |
| 27 | +#' twodash), or a string with an even number (up to eight) of hexadecimal |
| 28 | +#' digits which give the lengths in consecutive positions in the string. |
| 29 | +#' @param shape,pointshape Shape for points (1-25). |
23 | 30 | #' @param arrow.fill Fill colour for arrows.
|
24 | 31 | #' @param inherit.blank Should this element inherit the existence of an
|
25 | 32 | #' `element_blank` among its parents? If `TRUE` the existence of
|
26 | 33 | #' a blank element among its parents will cause this element to be blank as
|
27 | 34 | #' well. If `FALSE` any blank parent element will be ignored when
|
28 | 35 | #' calculating final element state.
|
29 | 36 | #' @return An S3 object of class `element`, `rel`, or `margin`.
|
| 37 | +#' @details |
| 38 | +#' The `element_polygon()` and `element_point()` functions are not rendered |
| 39 | +#' in standard plots and just serve as extension points. |
| 40 | +#' |
30 | 41 | #' @examples
|
31 | 42 | #' # A standard plot
|
32 | 43 | #' plot <- ggplot(mpg, aes(displ, hwy)) + geom_point()
|
@@ -97,10 +108,6 @@ element_rect <- function(fill = NULL, colour = NULL, linewidth = NULL,
|
97 | 108 |
|
98 | 109 | #' @export
|
99 | 110 | #' @rdname element
|
100 |
| -#' @param linetype,bordertype Line type for lines and borders respectively. An |
101 |
| -#' integer (0:8), a name (blank, solid, dashed, dotted, dotdash, longdash, |
102 |
| -#' twodash), or a string with an even number (up to eight) of hexadecimal |
103 |
| -#' digits which give the lengths in consecutive positions in the string. |
104 | 111 | #' @param lineend Line end Line end style (round, butt, square)
|
105 | 112 | #' @param arrow Arrow specification, as created by [grid::arrow()]
|
106 | 113 | element_line <- function(colour = NULL, linewidth = NULL, linetype = NULL,
|
@@ -164,11 +171,36 @@ element_text <- function(family = NULL, face = NULL, colour = NULL,
|
164 | 171 | )
|
165 | 172 | }
|
166 | 173 |
|
| 174 | +#' @export |
| 175 | +#' @rdname element |
| 176 | +element_polygon <- function(fill = NULL, colour = NULL, linewidth = NULL, |
| 177 | + linetype = NULL, color = NULL, |
| 178 | + inherit.blank = FALSE) { |
| 179 | + structure( |
| 180 | + list( |
| 181 | + fill = fill, colour = color %||% colour, linewidth = linewidth, |
| 182 | + linetype = linetype, inherit.blank = inherit.blank |
| 183 | + ), |
| 184 | + class = c("element_polygon", "element") |
| 185 | + ) |
| 186 | +} |
| 187 | + |
| 188 | +#' @export |
| 189 | +#' @rdname element |
| 190 | +element_point <- function(colour = NULL, shape = NULL, size = NULL, fill = NULL, |
| 191 | + stroke = NULL, color = NULL, inherit.blank = FALSE) { |
| 192 | + structure( |
| 193 | + list( |
| 194 | + colour = color %||% colour, fill = fill, shape = shape, size = size, |
| 195 | + stroke = stroke, inherit.blank = inherit.blank |
| 196 | + ), |
| 197 | + class = c("element_point", "element") |
| 198 | + ) |
| 199 | +} |
| 200 | + |
167 | 201 | #' @param ink Foreground colour.
|
168 | 202 | #' @param paper Background colour.
|
169 | 203 | #' @param accent Accent colour.
|
170 |
| -#' @param pointsize Size for points in mm. |
171 |
| -#' @param pointshape Shape for points (1-25). |
172 | 204 | #' @export
|
173 | 205 | #' @rdname element
|
174 | 206 | element_geom <- function(
|
@@ -357,6 +389,40 @@ element_grob.element_line <- function(element, x = 0:1, y = 0:1,
|
357 | 389 | )
|
358 | 390 | }
|
359 | 391 |
|
| 392 | +#' @export |
| 393 | +element_grob.element_polygon <- function(element, x = c(0, 0.5, 1, 0.5), |
| 394 | + y = c(0.5, 1, 0.5, 0), fill = NULL, |
| 395 | + colour = NULL, linewidth = NULL, |
| 396 | + linetype = NULL, ..., |
| 397 | + id = NULL, id.lengths = NULL, |
| 398 | + pathId = NULL, pathId.lengths = NULL) { |
| 399 | + |
| 400 | + gp <- gg_par(lwd = linewidth, col = colour, fill = fill, lty = linetype) |
| 401 | + element_gp <- gg_par(lwd = element$linewidth, col = element$colour, |
| 402 | + fill = element$fill, lty = element$linetype) |
| 403 | + pathGrob( |
| 404 | + x = x, y = y, gp = modify_list(element_gp, gp), ..., |
| 405 | + # We swap the id logic so that `id` is always the (super)group id |
| 406 | + # (consistent with `polygonGrob()`) and `pathId` always the subgroup id. |
| 407 | + pathId = id, pathId.lengths = id.lengths, |
| 408 | + id = pathId, id.lengths = pathId.lengths |
| 409 | + ) |
| 410 | +} |
| 411 | + |
| 412 | +#' @export |
| 413 | +element_grob.element_point <- function(element, x = 0.5, y = 0.5, colour = NULL, |
| 414 | + shape = NULL, fill = NULL, size = NULL, |
| 415 | + stroke = NULL, ..., |
| 416 | + default.units = "npc") { |
| 417 | + |
| 418 | + gp <- gg_par(col = colour, fill = fill, pointsize = size, stroke = stroke) |
| 419 | + element_gp <- gg_par(col = element$colour, fill = element$fill, |
| 420 | + pointsize = element$size, stroke = element$stroke) |
| 421 | + shape <- translate_shape_string(shape %||% element$shape %||% 19) |
| 422 | + pointsGrob(x = x, y = y, pch = shape, gp = modify_list(element_gp, gp), |
| 423 | + default.units = default.units, ...) |
| 424 | +} |
| 425 | + |
360 | 426 | #' Define and register new theme elements
|
361 | 427 | #'
|
362 | 428 | #' The underlying structure of a ggplot2 theme is defined via the element tree, which
|
@@ -532,6 +598,8 @@ el_def <- function(class = NULL, inherit = NULL, description = NULL) {
|
532 | 598 | line = el_def("element_line"),
|
533 | 599 | rect = el_def("element_rect"),
|
534 | 600 | text = el_def("element_text"),
|
| 601 | + point = el_def("element_point"), |
| 602 | + polygon = el_def("element_polygon"), |
535 | 603 | geom = el_def("element_geom"),
|
536 | 604 | title = el_def("element_text", "text"),
|
537 | 605 | spacing = el_def("unit"),
|
|
0 commit comments