Skip to content

Commit

Permalink
Setting box limits outside plot limits now causes a warning rather th…
Browse files Browse the repository at this point in the history
…an an error
  • Loading branch information
wilkox committed Jul 21, 2019
1 parent d89f3c0 commit df19c27
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS.md
Expand Up @@ -3,6 +3,9 @@
## Bug fixes

- Silently ignore NA values of 'label' rather than stopping with an error
- Skip drawing text when the box limits are outside the plot limits and emit a
warning, rather than stopping with an error (see #11, thanks
@alastairrushworth)

## Minor changes

Expand Down
12 changes: 12 additions & 0 deletions R/geom_fit_text.R
Expand Up @@ -354,6 +354,18 @@ makeContent.fittexttree <- function(x) {
)
}

# Remove any rows with NA boundaries
na_rows <- which(is.na(data$xmin) | is.na(data$xmax) | is.na(data$ymin) |
is.na(data$ymax))
if (length(na_rows) > 0) {
data <- data[-na_rows, ]
warning(
length(na_rows),
" rows removed where box limits were outside plot limits",
.call = FALSE
)
}

# Convert padding.x and padding.y to npc units
padding.x <- grid::convertWidth(x$padding.x, "npc", valueOnly = TRUE)
padding.y <- grid::convertHeight(x$padding.y, "npc", valueOnly = TRUE)
Expand Down
34 changes: 33 additions & 1 deletion tests/testthat/test-geom_fit_text.R
@@ -1,3 +1,4 @@
library(ggplot2)
testdata <- data.frame(
vehicle = c("light plane", "jumbo jet", "space shuttle"),
xmin = c(10, 20, 80),
Expand All @@ -6,6 +7,7 @@ testdata <- data.frame(
ymax = c(20, 95, 50),
class = c("plane", "plane", "spaceship")
)
z <- data.frame(x = letters[1:5], y = 0:4, lb = 3)

context("shrinking text")

Expand Down Expand Up @@ -220,11 +222,41 @@ context("blank labels")

test_that("a blank label should not result in an error", {
expect_silent( {
library(ggplot2)
presidential$name[1] <- ""
p <- ggplot(presidential,
aes(ymin = start, ymax = end, label = name, x = party)) +
geom_fit_text(grow = TRUE)
print(p)
} )
})

context("box limits out of plot limits")

test_that("box limits outside of plot limits should produce a warning", {
expect_warning( {
p <- ggplot(z, aes(x = x, y = y, label = lb)) +
geom_bar(stat = "identity", position = "dodge") +
ylim(0.5, 3) +
geom_fit_text()
print(p)
}, "box limits were outside plot limits")

expect_warning( {
p <- ggplot(z, aes(x = x, y = y, label = lb)) +
geom_bar(stat = "identity", position = "dodge") +
ylim(-0.1, 6) +
geom_fit_text()
print(p)
}, "box limits were outside plot limits")

expect_warning( {
p <- ggplot(z, aes(x = y, y = x, label = lb)) +
geom_bar(stat = "identity", position = "dodge") +
xlim(-0.1, 6) +
geom_fit_text()
print(p)
}, "box limits were outside plot limits")
} )



1 change: 1 addition & 0 deletions tests/testthat/test_plots.R
Expand Up @@ -102,4 +102,5 @@ test_that("plots look the way they should", {
values = c(1.0,0.8,0.6,0.4,0.2,0)
)
})

})

0 comments on commit df19c27

Please sign in to comment.