Skip to content

Commit

Permalink
Train identity scales when they have a guide.
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley authored and kohske committed Apr 27, 2012
1 parent 3852c08 commit 7c7a228
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ BUG FIXES
triggered when scale was limited to a range smaller than the range of
the data. (Bug reported by Tao Gao; diagnosed and fixed by Brian Diggs)

* `scale_*_identity` will now produce a legend when `guide = "legend"` and no
breaks or labels are supplied (Fixes #453)


ggplot2 0.9.0
----------------------------------------------------------------

Expand Down
14 changes: 9 additions & 5 deletions R/scale-identity.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#' qplot(1:4, 1:4, fill = colour, geom = "tile")
#' qplot(1:4, 1:4, fill = colour, geom = "tile") + scale_fill_identity()
#'
#' # To get a legend guide, you also need to supply labels, and specify
#' # guide = "legend"
#' qplot(1:4, 1:4, fill = colour, geom = "tile") +
#' # To get a legend guide, specify guide = "legend"
#' qplot(1:4, 1:4, fill = colour, geom = "tile") +
#' scale_fill_identity(guide = "legend")
#' # But you'll typically also need to supply breaks and labels:
#' qplot(1:4, 1:4, fill = colour, geom = "tile") +
#' scale_fill_identity("trt", labels = letters[1:4], breaks = colour,
#' guide = "legend")
#' guide = "legend")
#'
#' # cyl scaled to appropriate size
#' qplot(mpg, wt, data = mtcars, size = cyl)
Expand Down Expand Up @@ -72,7 +74,9 @@ scale_map.identity <- function(scale, x) {
}
#' @S3method scale_train identity
scale_train.identity <- function(scale, x) {
# do nothing
# do nothing if no guide, otherwise train so we know what breaks to use
if (scale$guide == "none") return()
NextMethod()
}

icon.identity <- function() textGrob("f(x) = x", gp=gpar(cex=1.2))
8 changes: 5 additions & 3 deletions man/scale_identity.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ colour <- c("red", "green", "blue", "yellow")
qplot(1:4, 1:4, fill = colour, geom = "tile")
qplot(1:4, 1:4, fill = colour, geom = "tile") + scale_fill_identity()

# To get a legend guide, you also need to supply labels, and specify
# guide = "legend"
# To get a legend guide, specify guide = "legend"
qplot(1:4, 1:4, fill = colour, geom = "tile") +
scale_fill_identity(guide = "legend")
# But you'll typically also need to supply breaks and labels:
qplot(1:4, 1:4, fill = colour, geom = "tile") +
scale_fill_identity("trt", labels = letters[1:4], breaks = colour,
guide = "legend")
guide = "legend")

# cyl scaled to appropriate size
qplot(mpg, wt, data = mtcars, size = cyl)
Expand Down

0 comments on commit 7c7a228

Please sign in to comment.