Skip to content

Commit

Permalink
Memoise Cartesian scale training
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 8, 2011
1 parent e82e737 commit 6649e7a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -20,7 +20,8 @@ Imports:
digest,
grid,
reshape2,
scales
scales,
memoise
Suggests:
quantreg,
Hmisc,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Expand Up @@ -187,6 +187,7 @@ export(ylab)
export(ylim)
import(digest)
import(grid)
import(memoise)
import(plyr)
import(reshape2)
import(scales)
Expand Down
48 changes: 18 additions & 30 deletions R/coord-cartesian-.r
Expand Up @@ -63,42 +63,30 @@ coord_transform.cartesian <- function(., data, details) {

#' @S3method coord_train cartesian
coord_train.cartesian <- function(coord, scales) {
if (is.null(coord$limits$x)) {
x.range <- scale_dimension(scales$x)
} else {
x.range <- range(scale_transform(scales$x, coord$limits[["x"]]))
if (coord$wise) {
scales$x$limits <- x.range
x.range <- expand_range(x.range,
scales$x$expand[1], scales$x$expand[2])
}
}

x.major <- rescale(scale_break_positions(scales$x), from = x.range)
x.minor <- rescale(scale_breaks_minor(scales$x), from = x.range)
x.labels <- scale_labels(scales$x)
c(train_cartesian(scales$x, coord$limits$x, "x"),
train_cartesian(scales$y, coord$limits$y, "y"))
}

if (is.null(coord$limits$y)) {
y.range <- scale_dimension(scales$y)
train_cartesian <- memoise(function(scale, limits, name) {
if (is.null(limits)) {
range <- scale_dimension(scale)
} else {
y.range <- range(scale_transform(scales$y, coord$limits$y))

range <- range(scale_transform(scale, limits))
if (coord$wise) {
scales$y$limits <- y.range
y.range <- expand_range(y.range,
scales$y$expand[1], scales$y$expand[2])
scale$limits <- x.range
range <- expand_range(range, scale$expand[1], scale$expand[2])
}
}
y.major <- rescale(scale_break_positions(scales$y), from = y.range)
y.minor <- rescale(scale_breaks_minor(scales$y), from = y.range)
y.labels <- scale_labels(scales$y)

list(
x.range = x.range, y.range = y.range,
x.major = x.major, x.minor = x.minor, x.labels = x.labels,
y.major = y.major, y.minor = y.minor, y.labels = y.labels
)
}
major <- rescale(scale_break_positions(scale), from = range)
minor <- rescale(scale_breaks_minor(scale), from = range)
labels <- scale_labels(scale)

out <- list(range = range, major = major, minor = minor, labels = labels)
names(out) <- paste(name, names(out), sep = ".")
out
})


icon.cartesian <- function(.) {
gTree(children = gList(
Expand Down
2 changes: 1 addition & 1 deletion R/ggplot2.r
Expand Up @@ -3,7 +3,7 @@
#' @name ggplot2
#' @docType package
#' @aliases ggplot2 package-ggplot2
#' @import plyr digest scales grid reshape2
#' @import plyr digest scales grid reshape2 memoise
NULL

#' Prices of 50,000 round cut diamonds
Expand Down
2 changes: 1 addition & 1 deletion R/guides-axis.r
Expand Up @@ -87,4 +87,4 @@ guide_axis <- function(at, labels, position="right", theme) {
width = grobWidth(fg),
height = grobHeight(fg)
)
}
}

0 comments on commit 6649e7a

Please sign in to comment.