Skip to content

Single element panels still need to be resized #2564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ up correct aspect ratio, and draws a graticule.
* `fortify()` gains a method for tbls (@karawoo, #2218)

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

* `ggproto()` produces objects with class `c("ggproto", "gg")`, allowing for
a more informative error message when adding layers, scales, or other ggproto
Expand Down
2 changes: 0 additions & 2 deletions R/plot.r
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ ggplot.grouped_df <- function(data, mapping = aes(), ...,
environment = parent.frame()) {

data$.group <- dplyr::group_indices(data)
mapping$group <- mapping$group %||% quote(.group)

ggplot.data.frame(data, mapping = mapping, ..., environment = environment)
}

Expand Down
10 changes: 3 additions & 7 deletions R/position-dodge2.r
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ PositionDodge2 <- ggproto("PositionDodge2", PositionDodge,
)

pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) {

if (length(unique(df$group)) == 1) {
return(df)
}

if (!all(c("xmin", "xmax") %in% names(df))) {
df$xmin <- df$x
df$xmax <- df$x
Expand Down Expand Up @@ -128,9 +123,10 @@ pos_dodge2 <- function(df, width, n = NULL, padding = 0.1) {

# Find groups of overlapping elements that need to be dodged from one another
find_x_overlaps <- function(df) {
overlaps <- vector(mode = "numeric", length = nrow(df))
overlaps <- numeric(nrow(df))
overlaps[1] <- counter <- 1
for (i in 2:nrow(df)) {

for (i in seq_asc(2, nrow(df))) {
if (df$xmin[i] >= df$xmax[i - 1]) {
counter <- counter + 1
}
Expand Down
8 changes: 8 additions & 0 deletions R/utilities.r
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ with_seed_null <- function(seed, code) {
}
}

seq_asc <- function(to, from) {
if (to > from) {
integer()
} else {
to:from
}
}

# Needed to trigger package loading
#' @importFrom tibble tibble
NULL
4 changes: 4 additions & 0 deletions man/ggplot2-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions tests/testthat/test-position-dodge2.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ test_that("find_x_overlaps identifies overlapping groups", {
expect_equal(find_x_overlaps(df2), c(1, 1, 2, 2, 3))
})

test_that("single element is rescaled based on n", {
df <- data.frame(xmin = 1, xmax = 2)
out <- pos_dodge2(df, n = 2)
expect_equal(out$xmax - out$xmin, 0.5)
})

test_that("rectangles are dodged", {
df <- data.frame(
xmin = c(1, 3, 6, 11, 13),
Expand All @@ -38,7 +44,7 @@ test_that("cols at the same x position are dodged", {
stringsAsFactors = FALSE
)

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

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

expect_equal(gaps(d1), 0)
expect_equal(gaps(d2), 0.0375)
expect_equal(gaps(d2), 0.0375)
})

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