Skip to content

Commit 31322d8

Browse files
authored
Single element panels still need to be resized (#2564)
* Single element panels still need to be resized Fixes #2563 * Soften ggplot2.grouped_df Fixes #2378 * Update authors in docs
1 parent 7ac9e42 commit 31322d8

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ up correct aspect ratio, and draws a graticule.
364364
* `fortify()` gains a method for tbls (@karawoo, #2218)
365365

366366
* `ggplot` gains a method for `grouped_df`s that adds a `.group` variable,
367-
which computes a unique value for each group, and automatically sets
368-
the group aesthetic to use that variable (#2351).
367+
which computes a unique value for each group. Use it with
368+
`aes(group = .group)` (#2351).
369369

370370
* `ggproto()` produces objects with class `c("ggproto", "gg")`, allowing for
371371
a more informative error message when adding layers, scales, or other ggproto

R/plot.r

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ ggplot.grouped_df <- function(data, mapping = aes(), ...,
116116
environment = parent.frame()) {
117117

118118
data$.group <- dplyr::group_indices(data)
119-
mapping$group <- mapping$group %||% quote(.group)
120-
121119
ggplot.data.frame(data, mapping = mapping, ..., environment = environment)
122120
}
123121

R/position-dodge2.r

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge,
6060
)
6161

6262
pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) {
63-
64-
if (length(unique(df$group)) == 1) {
65-
return(df)
66-
}
67-
6863
if (!all(c("xmin", "xmax") %in% names(df))) {
6964
df$xmin <- df$x
7065
df$xmax <- df$x
@@ -128,9 +123,10 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) {
128123

129124
# Find groups of overlapping elements that need to be dodged from one another
130125
find_x_overlaps <- function(df) {
131-
overlaps <- vector(mode = "numeric", length = nrow(df))
126+
overlaps <- numeric(nrow(df))
132127
overlaps[1] <- counter <- 1
133-
for (i in 2:nrow(df)) {
128+
129+
for (i in seq_asc(2, nrow(df))) {
134130
if (df$xmin[i] >= df$xmax[i - 1]) {
135131
counter <- counter + 1
136132
}

R/utilities.r

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ with_seed_null <- function(seed, code) {
403403
}
404404
}
405405

406+
seq_asc <- function(to, from) {
407+
if (to > from) {
408+
integer()
409+
} else {
410+
to:from
411+
}
412+
}
413+
406414
# Needed to trigger package loading
407415
#' @importFrom tibble tibble
408416
NULL

tests/testthat/test-position-dodge2.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ test_that("find_x_overlaps identifies overlapping groups", {
1616
expect_equal(find_x_overlaps(df2), c(1, 1, 2, 2, 3))
1717
})
1818

19+
test_that("single element is rescaled based on n", {
20+
df <- data.frame(xmin = 1, xmax = 2)
21+
out <- pos_dodge2(df, n = 2)
22+
expect_equal(out$xmax - out$xmin, 0.5)
23+
})
24+
1925
test_that("rectangles are dodged", {
2026
df <- data.frame(
2127
xmin = c(1, 3, 6, 11, 13),
@@ -38,7 +44,7 @@ test_that("cols at the same x position are dodged", {
3844
stringsAsFactors = FALSE
3945
)
4046

41-
p <- ggplot(df, aes(1, n, fill = x)) +
47+
p <- ggplot(df, aes(1, n, fill = x)) +
4248
geom_col(position = "dodge2", alpha = 0.5)
4349

4450
expect_false(any(duplicated(find_x_overlaps(layer_data(p)))))
@@ -62,7 +68,7 @@ test_that("padding argument controls space between elements", {
6268
}
6369

6470
expect_equal(gaps(d1), 0)
65-
expect_equal(gaps(d2), 0.0375)
71+
expect_equal(gaps(d2), 0.0375)
6672
})
6773

6874
test_that("boxes in facetted plots keep the correct width", {

0 commit comments

Comments
 (0)