Skip to content

Commit ae3c31e

Browse files
committed
Add position_dodgenudge_to()
Nothing broken, but dodging not yet tested.
1 parent d533c32 commit ae3c31e

File tree

158 files changed

+4956
-4860
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+4956
-4860
lines changed

DESCRIPTION

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Package: ggpp
22
Type: Package
33
Title: Grammar Extensions to 'ggplot2'
44
Version: 0.5.8-1.9000
5-
Date: 2024-11-13
5+
Date: 2025-02-20
66
Authors@R:
77
c(
88
person("Pedro J.", "Aphalo", email = "[email protected]", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-3385-972X")),
@@ -57,7 +57,7 @@ URL: https://docs.r4photobiology.info/ggpp/,
5757
https://github.com/aphalo/ggpp
5858
BugReports: https://github.com/aphalo/ggpp/issues
5959
Encoding: UTF-8
60-
RoxygenNote: 7.3.1
60+
RoxygenNote: 7.3.2
6161
VignetteBuilder: knitr
6262
Collate:
6363
'annotate.r'
@@ -82,13 +82,13 @@ Collate:
8282
'geom-text-npc.r'
8383
'geom-text-pairwise.R'
8484
'ggpp.R'
85+
'position-dodge-nudge-to.R'
86+
'position-dodge-nudge.R'
87+
'position-dodge2-nudge.R'
88+
'position-jitter-nudge.R'
8589
'position-nudge-center.R'
86-
'position-nudge-dodge.R'
87-
'position-nudge-dodge2.R'
88-
'position-nudge-jitter.R'
8990
'position-nudge-line.R'
90-
'position-nudge-stack.R'
91-
'position-nudge-to.R'
91+
'position-stack-nudge.R'
9292
'scale-continuous-npc.r'
9393
'stat-apply.R'
9494
'stat-dens1d-filter.r'

NAMESPACE

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export(GeomXMarginPoint)
2525
export(GeomYMarginArrow)
2626
export(GeomYMarginGrob)
2727
export(GeomYMarginPoint)
28+
export(PositionDodgeNudgeTo)
2829
export(PositionFillAndNudge)
2930
export(PositionNudgeCenter)
3031
export(PositionNudgeLine)
31-
export(PositionNudgeTo)
3232
export(StatApplyGroup)
3333
export(StatDens1dFilter)
3434
export(StatDens1dFilterG)
@@ -76,6 +76,7 @@ export(position_dodge2_keep)
7676
export(position_dodge2nudge)
7777
export(position_dodge_keep)
7878
export(position_dodgenudge)
79+
export(position_dodgenudge_to)
7980
export(position_fill_keep)
8081
export(position_fillnudge)
8182
export(position_jitter_keep)

NEWS.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ editor_options:
66
wrap: 72
77
---
88

9+
# ggpp 0.5.9
10+
11+
- Add `position_dodgenudge_to()` that allows the action of `position_nudge_to()`
12+
to preceded by dodging.
13+
914
# ggpp 0.5.8-1
1015

1116
- Fix wrong test for availability of 'gginnards' in examples (reported by
@@ -73,7 +78,7 @@ and `point.padding`.
7378
with `geom_label_s()` and `geom_label_pairwise()`.
7479
- Add `position_stack_minmax()` to correctly position error bars in stacked plots.
7580
- Add data sets `birch.df`and `birch_dw.df` with data for 350 birch seedlings.
76-
- Add data set `weather_18_june_2019.df` with data at 1 min intercal for 24 h.
81+
- Add data set `weather_18_june_2019.df` with data at 1 min interval for 24 h.
7782
- Move transcriptomics data sets `quadrant_example.df` and `volcano_example.df` from 'ggpmisc' to 'ggpp'.
7883

7984
# ggpp 0.5.4

R/position-nudge-to.R R/position-dodge-nudge-to.R

+104-21
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
#' Nudge labels to new positions
22
#'
3-
#' \code{position_nudge_to()} is generally useful for adjusting the position of
4-
#' labels or text, both on a discrete or continuous scale.
5-
#' \code{position_nudge_to()} differs from \code{\link[ggplot2]{position_nudge}}
6-
#' in that the coordinates of the new position are given directly, rather than
7-
#' as a displacement from the original location. It optionally sets an even
8-
#' distance among positions. As other position functions in this package, it
9-
#' preserves the original position to allow the text to be linked back to its
10-
#' original position with a segment or arrow.
3+
#' \code{position_dodgenudge_to()} is generally useful for adjusting the
4+
#' position of labels or text, both on a discrete or continuous scale.
5+
#' \code{position_dodgenudge_to()} and \code{position_nudge_to()} differ from
6+
#' \code{\link[ggplot2]{position_nudge}} in that the coordinates of the new
7+
#' position are given directly, rather than as a displacement from the original
8+
#' location. It optionally sets an even spacing among positions within a range.
9+
#' In \code{position_dodgenudge_to()} this nudging can be combined with dodging.
10+
#' As with other position functions in this package, the original positions are
11+
#' preserved to allow the text or labels to be linked back to their original
12+
#' position with a segment or arrow.
1113
#'
1214
#' @family position adjustments
1315
#'
16+
#' @param width Dodging width, when different to the width of the individual
17+
#' elements. This is useful when you want to align narrow geoms with wider
18+
#' geoms. See the examples.
19+
#' @param preserve Should dodging preserve the total width of all elements at a
20+
#' position, or the width of a single element?.
1421
#' @param x,y Coordinates of the destination position. A vector of mode
1522
#' \code{numeric}, that is extended if needed, to the same length as rows
1623
#' there are in \code{data}. The default, \code{NULL}, leaves the original
17-
#' coordinates unchanged.
24+
#' coordinates unchanged after dodging.
1825
#' @param x.action,y.action character string, one of \code{"none"}, or
1926
#' \code{"spread"}. With \code{"spread"} distributing the positions
2027
#' within the range of argument \code{x} or \code{y}, if non-null, or the
@@ -23,25 +30,50 @@
2330
#' implemented.
2431
#' @param x.expansion,y.expansion numeric vectors of length 1 or 2, as a
2532
#' fraction of width of the range.
26-
#' @param kept.origin One of \code{"original"} or \code{"none"}.
33+
#' @param kept.origin One of \code{"original"}, \code{"dodged"} or
34+
#' \code{"none"}.
2735
#'
2836
#' @details The nudged to \code{x} and/or \code{y} values replace the original ones in
29-
#' \code{data}, while the original coordinates are returned in \code{x_orig}
30-
#' and \code{y_orig}. Values supported are those of \emph{mode} numeric,
31-
#' thus including dates and times.
37+
#' \code{data}, while the original or the dodged coordinates are returned in \code{x_orig}
38+
#' and \code{y_orig}. Nudge values supported are those of \emph{mode} numeric,
39+
#' thus including dates and times when they match the mapped data.
3240
#'
3341
#' If the length of \code{x} and/or \code{y} is more than one but less than
3442
#' rows are present in the data, the vector is both recycled and reordered so
3543
#' that the nudges are applied sequentially based on the data values. If their
3644
#' length matches the number of rows in data, they are assumed to be already
3745
#' in data order.
3846
#'
47+
#' The applied dodge is identical to that by
48+
#' \code{\link[ggplot2]{position_dodge}} while nudging is different to that by
49+
#' \code{\link[ggplot2]{position_nudge}}.
50+
#'
51+
#' There are two possible uses for these functions. First, without using dodging
52+
#' they can be used to obtain aligned labels when the labelled objects are not
53+
#' aligned. This is the most common use.
54+
#'
55+
#' The second use is to label dodged bars, boxplots or points with labels
56+
#' aligned. In this case, it is mandatory to use
57+
#' the same argument to \code{width} when passing \code{position_dodge()} to
58+
#' \code{geom_col()} and \code{position_dodgenudge_to()} to \code{geom_text()} or
59+
#' \code{geom_label()} or their repulsive equivalents. Otherwise the arrows or
60+
#' segments will fail to connect to the labels. In other words dodging is
61+
#' computed twice. Dodge is identical to that obtained with the same arguments
62+
#' in \code{\link[ggplot2]{position_dodge}} as \code{position_dodgenudge_to()}
63+
#' simply calls the same code from package 'ggplot2' ahead of applying
64+
#' nudging.
65+
#'
66+
#' When applying dodging, the return of original positions instead of the dodged
67+
#' ones is achieved by passing \code{origin = "original"} instead of the default
68+
#' of \code{origin = "dodged"}.
69+
#'
3970
#' @note Irrespective of the action, the ordering of rows in \code{data} is
4071
#' preserved.
4172
#'
4273
#' @return A \code{"Position"} object.
4374
#'
4475
#' @seealso \code{\link[ggplot2]{position_nudge}},
76+
#' \code{\link[ggplot2]{position_dodge}},
4577
#' \code{\link[ggrepel]{position_nudge_repel}}.
4678
#'
4779
#' @export
@@ -58,6 +90,10 @@
5890
#' geom_point() +
5991
#' geom_text(position = position_nudge_to())
6092
#'
93+
#' ggplot(df, aes(x, y, label = label)) +
94+
#' geom_point() +
95+
#' geom_text(position = position_dodgenudge_to())
96+
#'
6197
#' # a single y (or x) value nudges all observations to this data value
6298
#' ggplot(df, aes(x, y, label = label)) +
6399
#' geom_point() +
@@ -98,7 +134,7 @@
98134
#' ggplot(df, aes(x, y, label = label)) +
99135
#' geom_point() +
100136
#' geom_text_s(position =
101-
#' position_nudge_to(y = 3, x.action = "spread", x.expansion = -0.1))
137+
#' position_dodgenudge_to(y = 3, x.action = "spread", x.expansion = -0.1))
102138
#'
103139
#' # spread the values at equal distance within the range given by x
104140
#' ggplot(df, aes(x, y, label = label)) +
@@ -113,8 +149,10 @@
113149
#' position_nudge_to(y = 3, x = c(0,6), x.action = "spread"),
114150
#' hjust = "center")
115151
#'
116-
position_nudge_to <-
117-
function(x = NULL,
152+
position_dodgenudge_to <-
153+
function(width = 1,
154+
preserve = c("total", "single"),
155+
x = NULL,
118156
y = NULL,
119157
x.action = c("none", "spread"),
120158
y.action = c("none", "spread"),
@@ -123,6 +161,7 @@ position_nudge_to <-
123161
x.expansion = 0,
124162
y.expansion = 0,
125163
kept.origin = c("original", "none")) {
164+
preserve <- rlang::arg_match(preserve)
126165
kept.origin <- rlang::arg_match(kept.origin)
127166
x.action <- rlang::arg_match(x.action)
128167
y.action <- rlang::arg_match(y.action)
@@ -141,7 +180,7 @@ position_nudge_to <-
141180
y <- as.numeric(y)
142181
}
143182

144-
ggplot2::ggproto(NULL, PositionNudgeTo,
183+
ggplot2::ggproto(NULL, PositionDodgeNudgeTo,
145184
x = x,
146185
y = y,
147186
x.action = x.action,
@@ -150,17 +189,19 @@ position_nudge_to <-
150189
y.distance = y.distance,
151190
x.expansion = rep_len(x.expansion, 2),
152191
y.expansion = rep_len(y.expansion, 2),
153-
kept.origin = kept.origin
192+
kept.origin = kept.origin,
193+
width = width,
194+
preserve = rlang::arg_match(preserve)
154195
)
155196
}
156197

157198
#' @rdname ggpp-ggproto
158199
#' @format NULL
159200
#' @usage NULL
160201
#' @export
161-
PositionNudgeTo <-
202+
PositionDodgeNudgeTo <-
162203
ggplot2::ggproto(
163-
"PositionNudgeTo",
204+
"PositionDodgeNudgeTo",
164205
Position,
165206
x = NULL,
166207
y = NULL,
@@ -181,6 +222,11 @@ PositionNudgeTo <-
181222
},
182223

183224
compute_layer = function(self, data, params, layout) {
225+
# operate on the dodged positions
226+
data = ggplot2::ggproto_parent(ggplot2::PositionDodge, self)$compute_layer(data, params, layout)
227+
228+
x_dodged <- data$x
229+
y_dodged <- data$y
184230
x_orig <- data$x
185231
y_orig <- data$y
186232

@@ -273,11 +319,48 @@ PositionNudgeTo <-
273319
data <- transform_position(data, NULL, function(y) y + params$y)
274320
}
275321
# add original position
276-
if (params$kept.origin == "original") {
322+
if (params$kept.origin == "dodged") {
323+
data$x_orig <- x_dodged
324+
data$y_orig <- y_dodged
325+
} else if (params$kept.origin == "original") {
277326
data$x_orig <- x_orig
278327
data$y_orig <- y_orig
279328
}
280329

281330
data
331+
},
332+
333+
compute_panel = function(self, data, params, scales) {
334+
ggplot2::ggproto_parent(PositionDodge, self)$compute_panel(data, params, scales)
282335
}
283336
)
337+
338+
#' @rdname position_dodgenudge_to
339+
#'
340+
#' @export
341+
#'
342+
# for backwards compatibility with arguments passed by position
343+
position_nudge_to <-
344+
function(x = NULL,
345+
y = NULL,
346+
x.action = c("none", "spread"),
347+
y.action = c("none", "spread"),
348+
x.distance = "equal",
349+
y.distance = "equal",
350+
x.expansion = 0,
351+
y.expansion = 0,
352+
kept.origin = c("original", "none")) {
353+
354+
position_dodgenudge_to(width = 1,
355+
preserve = "total",
356+
x = x,
357+
y = y,
358+
x.action = x.action,
359+
y.action = y.action,
360+
x.distance = x.distance,
361+
y.distance = y.distance,
362+
x.expansion = x.expansion,
363+
y.expansion = y.expansion,
364+
kept.origin = kept.origin
365+
)
366+
}

R/position-nudge-dodge.R R/position-dodge-nudge.R

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,32 @@
66
#' combines into one function the action of
77
#' \code{\link[ggplot2]{position_dodge2}} and
88
#' \code{\link[ggplot2]{position_nudge}}. They are useful when labelling plots
9-
#' such as grouped bars, columns, etc. and when adding dodged to text labels
10-
#' linked to observations plotted without dodge. It can replace other position
11-
#' functions as it is backwards compatible. Like all other position functions in
12-
#' 'ggpp' and 'ggrepel' it preserves the initial position to allow drawing of
13-
#' segments or arrow linking the original position to the displaced one.
9+
#' such as grouped bars, columns, etc. and when adding dodged text labels linked
10+
#' to observations plotted without dodge. It can replace other position
11+
#' functions as they are backwards compatible. Like all other position functions
12+
#' in 'ggpp' and 'ggrepel' they preserve the initial position to allow drawing
13+
#' of segments or arrow linking the original position to the displaced one.
1414
#'
1515
#' @details The applied dodge is identical to that by
1616
#' \code{\link[ggplot2]{position_dodge}} or
1717
#' \code{\link[ggplot2]{position_dodge2}} while nudging is similar to that by
1818
#' \code{\link[ggplot2]{position_nudge}}.
1919
#'
20-
#' There are two posible uses for these functions. First they can be used to
20+
#' There are two possible uses for these functions. First they can be used to
2121
#' label dodged bars or boxplots. In this case, it is mandatory to use the same
2222
#' argument to \code{width} when passing \code{position_dodge()} to
2323
#' \code{geom_col()} and \code{position_dodgenudge()} to \code{geom_text()} or
2424
#' \code{geom_label()} or their repulsive equivalents. Otherwise the arrows or
25-
#' segments will fail to connect to the labels. In other words jittering is
26-
#' computed twice. Jitter should be identical with the same arguments as
27-
#' \code{position_dodgenudge()} as this last function simply call the same code
28-
#' from package 'ggplot2'.
25+
#' segments will fail to connect to the labels. In other words dodging is
26+
#' computed twice. Dodge is identical to that obtained with the same arguments
27+
#' in \code{\link[ggplot2]{position_dodge}} as \code{position_dodgenudge()} last
28+
#' function simply calls the same code from package 'ggplot2' ahead of applying
29+
#' nudging.
2930
#'
3031
#' The second use is to dodge labels to be connected to elements that have not
31-
#' been jittered. The return of original positions instead of the dodged
32+
#' been dodged.
33+
#'
34+
#' When applying dodging, the return of original positions instead of the dodged
3235
#' ones is achieved by passing \code{origin = "original"} instead of the default
3336
#' of \code{origin = "dodged"}.
3437
#'
@@ -54,8 +57,6 @@
5457
#' @param kept.origin One of \code{"original"}, \code{"dodged"} or
5558
#' \code{"none"}.
5659
#'
57-
#' @param kept.origin One of \code{"original"} or \code{"none"}.
58-
#'
5960
#' @seealso \code{\link[ggplot2]{position_nudge}},
6061
#' \code{\link[ggrepel]{position_nudge_repel}}.
6162
#'
File renamed without changes.
File renamed without changes.
File renamed without changes.

ggpp.Rproj

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 9247d305-d490-4fc6-aa77-72c17dd2cbeb
23

34
RestoreWorkspace: No
45
SaveWorkspace: No

man/ggpp-ggproto.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)