Skip to content

Commit

Permalink
Improve free scale + bad coord error message
Browse files Browse the repository at this point in the history
Fixes #2049
  • Loading branch information
hadley committed Nov 1, 2017
1 parent f16978e commit b0eea4b
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 2.2.1.9000

* `facet_wrap()` and `facet_grid()` both give better error messages if you
attempt to use an unsupported coord with free scales (#2049)

* ggplot2 now works on R 3.1 onwards.

* `facet_grid()` gives a more informative error message if you try to use
Expand Down
18 changes: 18 additions & 0 deletions R/facet-.r
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,21 @@ render_strips <- function(x = NULL, y = NULL, labeller, theme) {
y = build_strip(y, labeller, theme, FALSE)
)
}


check_coord_freedom <- function(coord) {
# Check first element of class vector because this is a hideous hack on
# top of another hideous hack
class <- class(coord)[[1]]

if (class %in% c("CoordCartesian", "CoordFlip")) {
return()
}

stop(
"Free scales are only supported with `coord_cartesian()` and `coord_flip()`",
call. = FALSE
)
}


3 changes: 3 additions & 0 deletions R/facet-grid-.r
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ FacetGrid <- ggproto("FacetGrid", Facet,
data[order(data$PANEL), , drop = FALSE]
},
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
if (params$free$x || params$free$y)
check_coord_freedom(coord)

cols <- which(layout$ROW == 1)
rows <- which(layout$COL == 1)
axes <- render_axes(ranges[cols], ranges[rows], coord, theme, transpose = TRUE)
Expand Down
8 changes: 3 additions & 5 deletions R/facet-wrap.r
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ FacetWrap <- ggproto("FacetWrap", Facet,
data[order(data$PANEL), ]
},
draw_panels = function(panels, layout, x_scales, y_scales, ranges, coord, data, theme, params) {
# If coord is non-cartesian and (x is free or y is free)
# then throw error
if ((!inherits(coord, "CoordCartesian")) && (params$free$x || params$free$y)) {
stop("ggplot2 does not currently support free scales with a non-cartesian coord", call. = FALSE)
}
if (params$free$x || params$free$y)
check_coord_freedom(coord)

if (inherits(coord, "CoordFlip")) {
if (params$free$x) {
layout$SCALE_X <- seq_len(nrow(layout))
Expand Down

0 comments on commit b0eea4b

Please sign in to comment.