-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgeom_point2.R
More file actions
152 lines (126 loc) · 3.64 KB
/
Copy pathgeom_point2.R
File metadata and controls
152 lines (126 loc) · 3.64 KB
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
#' @title Better looking points
#' @name geom_point2
#'
#' @description
#' The `*_borderless` geoms (and their shortcuts ending in `2`, such as `geom_point2`
#' or `geom_point_borderless`) render points without an outline stroke by default.
#' This prevents harsh edges and yields a smoother, cleaner look, especially when
#' using transparency.
#'
#' In contrast, the `*_halo` variants feature a border that automatically matches
#' the plot's background color. This creates a subtle visual separation (a "halo"
#' effect) that keeps overlapping points distinct.
#'
#' @param size Size of points.
#' @param stroke Stroke thickness.
#' @param shape Shape of points.
#' @param ... Other arguments to be passed to [ggplot2::geom_point()],
#' [ggplot2::geom_jitter()], [ggplot2::geom_pointrange()], or
#' [ggplot2::geom_count()].
#'
#' @note The color aesthetics for the `*_halo()` functions is `"fill"`, not
#' `"color"`. See 'Examples'.
#'
#' @examplesIf requireNamespace("patchwork", quietly = TRUE)
#' library(ggplot2)
#' library(see)
#'
#' normal <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
#' geom_point(size = 8, alpha = 0.3) +
#' theme_modern()
#'
#' new <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
#' geom_point2(size = 8, alpha = 0.3) +
#' theme_modern()
#'
#' plots(normal, new, n_columns = 2)
#'
#' ggplot(iris, aes(x = Petal.Width, y = Sepal.Length, color = Species)) +
#' geom_point_borderless(size = 4) +
#' theme_modern()
#'
#' theme_set(theme_abyss())
#' ggplot(iris, aes(x = Petal.Width, y = Sepal.Length, color = Species)) +
#' geom_point_borderless(size = 4)
#'
#' # add "halo" effect - note that the aesthetics is "fill", not "color"
#' theme_set(theme_abyss())
#' ggplot(iris, aes(x = Petal.Width, y = Sepal.Length, fill = Species)) +
#' geom_point_halo(size = 12)
#' @export
geom_point2 <- function(..., stroke = 0, shape = 16) {
geom_point(stroke = stroke, shape = shape, ...)
}
#' @rdname geom_point2
#' @export
geom_jitter2 <- function(..., size = 2, stroke = 0, shape = 16) {
geom_jitter(size = size, stroke = stroke, shape = shape, ...)
}
#' @rdname geom_point2
#' @export
geom_pointrange2 <- function(..., stroke = 0) {
geom_pointrange(stroke = stroke, ...)
}
#' @rdname geom_point2
#' @export
geom_count2 <- function(..., stroke = 0) {
geom_count(stroke = stroke, ...)
}
#' @rdname geom_point2
#' @export
geom_count_borderless <- geom_count2
#' @rdname geom_point2
#' @export
geom_point_borderless <- geom_point2
#' @rdname geom_point2
#' @export
geom_jitter_borderless <- geom_jitter2
#' @rdname geom_point2
#' @export
geom_pointrange_borderless <- geom_pointrange2
#' @rdname geom_point2
#' @export
geom_point_halo <- function(...) {
fun <- ggplot2::geom_point
.geom_halo(fun, ...)
}
#' @rdname geom_point2
#' @export
geom_jitter_halo <- function(...) {
fun <- ggplot2::geom_jitter
.geom_halo(fun, ...)
}
#' @rdname geom_point2
#' @export
geom_count_halo <- function(...) {
fun <- ggplot2::geom_count
.geom_halo(fun, ...)
}
#' @rdname geom_point2
#' @export
geom_pointrange_halo <- function(...) {
fun <- ggplot2::geom_pointrange
.geom_halo(fun, ...)
}
.geom_halo <- function(fun, ...) {
dots <- list(...)
if (!is.null(dots$color)) {
dots$fill <- dots$color
}
if (!is.null(dots$colour)) {
dots$fill <- dots$colour
}
dots$colour <- dots$shape <- dots$pch <- NULL
dots$color <- .get_theme_bg_color()
fun_args <- c(list(pch = 21), dots)
do.call(fun, fun_args)
}
.get_theme_bg_color <- function() {
current_theme <- ggplot2::theme_get()
bg_color <- ifelse(
is.null(current_theme$panel.background$fill),
"white",
current_theme$panel.background$fill
)
bg_color
}