Skip to content

Commit

Permalink
Even better naming scheme, which makes scales more consistent and eas…
Browse files Browse the repository at this point in the history
…ier to understand operation
  • Loading branch information
hadley committed Jun 7, 2008
1 parent 472a0a4 commit c2e0d84
Show file tree
Hide file tree
Showing 28 changed files with 101 additions and 104 deletions.
16 changes: 8 additions & 8 deletions R/coordinates-cartesian-.r
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ CoordCartesian <- proto(Coord, expr={
)
}

frange <- function(.) {
output_set <- function(.) {
expand <- .$expand()
list(
x = expand_range(range(.$x()$frange()), expand$x[1], expand$x[2]),
y = expand_range(range(.$y()$frange()), expand$y[1], expand$y[2])
x = expand_range(range(.$x()$output_set()), expand$x[1], expand$x[2]),
y = expand_range(range(.$y()$output_set()), expand$y[1], expand$y[2])
)
}

guide_axes <- function(.) {
range <- .$frange()
range <- .$output_set()
list(
x = ggaxis(.$x()$domain_breaks(), .$x()$labels(), "bottom", range$x),
y = ggaxis(.$y()$domain_breaks(), .$y()$labels(), "left", range$y)
x = ggaxis(.$x()$input_breaks(), .$x()$labels(), "bottom", range$x),
y = ggaxis(.$y()$input_breaks(), .$y()$labels(), "left", range$y)
)
}

Expand All @@ -41,8 +41,8 @@ CoordCartesian <- proto(Coord, expr={
# Axis labels should go in here somewhere too
guide_inside <- function(., plot) {
breaks <- list(
x = list(major = .$x()$domain_breaks(), minor = .$x()$minor_breaks()),
y = list(major = .$y()$domain_breaks(), minor = .$y()$minor_breaks())
x = list(major = .$x()$input_breaks(), minor = .$x()$minor_breaks()),
y = list(major = .$y()$input_breaks(), minor = .$y()$minor_breaks())
)

draw_grid(plot, breaks)
Expand Down
10 changes: 5 additions & 5 deletions R/coordinates-cartesian-equal.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ CoordEqual <- proto(CoordCartesian, {
list(.$proto(ratio=ratio), opts(aspect.ratio = ratio))
}

frange <- function(.) {
xlim <- .$x()$frange()
ylim <- .$y()$frange()
output_set <- function(.) {
xlim <- .$x()$output_set()
ylim <- .$y()$output_set()

xr <- diff(xlim)
yr <- diff(ylim)
Expand Down Expand Up @@ -40,15 +40,15 @@ CoordEqual <- proto(CoordCartesian, {
}

guide_axes <- function(.) {
range <- .$frange()
range <- .$output_set()
list(
x = ggaxis(grid.pretty(range$x), grid.pretty(range$x), "bottom", range$x),
y = ggaxis(grid.pretty(range$y), grid.pretty(range$y), "left", range$y)
)
}

guide_inside <- function(., plot) {
range <- .$frange()
range <- .$output_set()
breaks <- list(
x = list(major = grid.pretty(range$x), minor = .$x()$minor_breaks(b = grid.pretty(range$x))),
y = list(major = grid.pretty(range$y), minor = .$y()$minor_breaks(b = grid.pretty(range$y)))
Expand Down
14 changes: 7 additions & 7 deletions R/coordinates-map.r
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ CoordMap <- proto(CoordCartesian, {

mproject <- function(., data) {
if (is.null(.$orientation))
.$orientation <- c(90, 0, mean(.$x()$frange()))
.$orientation <- c(90, 0, mean(.$x()$output_set()))

suppressWarnings(do.call("mapproject",
list(data, projection=.$projection, parameters = .$params, orientation = .$orientation)
))
}

frange <- function(.) {
output_set <- function(.) {
expand <- .$expand()
xrange <- expand_range(.$x()$frange(), expand$x[1], expand$x[2])
yrange <- expand_range(.$y()$frange(), expand$y[1], expand$y[2])
xrange <- expand_range(.$x()$output_set(), expand$x[1], expand$x[2])
yrange <- expand_range(.$y()$output_set(), expand$y[1], expand$y[2])

df <- data.frame(x = xrange, y = yrange)
range <- .$mproject(df)$range
Expand All @@ -37,7 +37,7 @@ CoordMap <- proto(CoordCartesian, {
}

guide_axes <- function(.) {
range <- .$frange()
range <- .$output_set()
list(
x = ggaxis(NA, "", "bottom", range$x),
y = ggaxis(NA, "", "left", range$y)
Expand All @@ -46,8 +46,8 @@ CoordMap <- proto(CoordCartesian, {

guide_inside <- function(., plot) {
range <- list(
x = expand_range(.$x()$frange(), 0.1),
y = expand_range(.$y()$frange(), 0.1)
x = expand_range(.$x()$output_set(), 0.1),
y = expand_range(.$y()$output_set(), 0.1)
)
x <- grid.pretty(range$x)
y <- grid.pretty(range$y)
Expand Down
12 changes: 6 additions & 6 deletions R/coordinates-polar.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CoordPolar <- proto(Coord, {
theta_scale <- function(.) .$.scales$get_scales(.$theta)
theta_range <- function(.) {
expand <- .$expand()
expand_range(.$theta_scale()$frange(), expand$theta[1], expand$theta[2])
expand_range(.$theta_scale()$output_set(), expand$theta[1], expand$theta[2])
}
theta_rescale <- function(., x) {
rotate <- function(x) (x + .$start) %% (2 * pi) * .$direction
Expand All @@ -32,7 +32,7 @@ CoordPolar <- proto(Coord, {
r_scale <- function(.) .$.scales$get_scales(.$r)
r_range <- function(.) {
expand <- .$expand()
expand_range(.$r_scale()$frange(), 0, expand$r[2])
expand_range(.$r_scale()$output_set(), 0, expand$r[2])
}

r_rescale <- function(., x) rescale(x, c(0, 0.9), .$r_range())
Expand All @@ -51,7 +51,7 @@ CoordPolar <- proto(Coord, {

guide_inside <- function(., plot) {

theta <- .$theta_rescale(.$theta_scale()$domain_breaks())
theta <- .$theta_rescale(.$theta_scale()$input_breaks())
thetamin <- .$theta_rescale(.$theta_scale()$minor_breaks())
thetafine <- seq(0, 2*pi, length=100)

Expand All @@ -64,7 +64,7 @@ CoordPolar <- proto(Coord, {
}

r <- 1
rfine <- .$r_rescale(.$r_scale()$domain_breaks())
rfine <- .$r_rescale(.$r_scale()$input_breaks())

gp <- gpar(fill=plot$grid.fill, col=plot$grid.colour)

Expand All @@ -79,7 +79,7 @@ CoordPolar <- proto(Coord, {
}


frange <- function(.) {
output_set <- function(.) {
list(
x = expand_range(c(-1, 1), 0.1, 0),
y = expand_range(c(-1, 1), 0.1, 0)
Expand All @@ -89,7 +89,7 @@ CoordPolar <- proto(Coord, {
guide_axes <- function(.) {
list(
x = ggaxis(c(-1, 1), "", "bottom", c(-1,1)),
y = ggaxis(.$r_rescale(.$r_scale()$domain_breaks()) / 2 + 0.6, .$r_scale()$labels(), "left", c(0, 1.2))
y = ggaxis(.$r_rescale(.$r_scale()$input_breaks()) / 2 + 0.6, .$r_scale()$labels(), "left", c(0, 1.2))
)
}

Expand Down
16 changes: 8 additions & 8 deletions R/coordinates-transform.r
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ CoordTrans <- proto(CoordCartesian, expr={
.$proto(xtr=xtrans, ytr=ytrans)
}

frange <- function(.) {
output_set <- function(.) {
expand <- .$expand()
list(
x = expand_range(.$xtr$transform(.$x()$frange()), expand$x[1], expand$x[2]),
y = expand_range(.$ytr$transform(.$y()$frange()), expand$y[1], expand$y[2])
x = expand_range(.$xtr$transform(.$x()$output_set()), expand$x[1], expand$x[2]),
y = expand_range(.$ytr$transform(.$y()$output_set()), expand$y[1], expand$y[2])
)
}

guide_axes <- function(.) {
range <- .$frange()
range <- .$output_set()
list(
x = ggaxis(.$xtr$transform(.$x()$domain_breaks()), .$x()$labels(), "bottom", range$x),
y = ggaxis(.$ytr$transform(.$y()$domain_breaks()), .$y()$labels(), "left", range$y)
x = ggaxis(.$xtr$transform(.$x()$input_breaks()), .$x()$labels(), "bottom", range$x),
y = ggaxis(.$ytr$transform(.$y()$input_breaks()), .$y()$labels(), "left", range$y)
)
}

Expand All @@ -45,10 +45,10 @@ CoordTrans <- proto(CoordCartesian, expr={
ggname("background", rectGrob(gp=gpar(fill=plot$grid.fill, col=NA))),

ggname("minor-vertical", segmentsGrob(.$xtr$transform(.$x()$minor_breaks()), unit(0, "npc"), .$xtr$transform(.$x()$minor_breaks()), unit(1, "npc"), gp = gpar(col=plot$grid.minor.colour, lwd=0.5), default.units="native")),
ggname("major-vertical", segmentsGrob(.$xtr$transform(.$x()$domain_breaks()), unit(0, "npc"), .$xtr$transform(.$x()$domain_breaks()), unit(1, "npc"), gp = gp, default.units="native")),
ggname("major-vertical", segmentsGrob(.$xtr$transform(.$x()$input_breaks()), unit(0, "npc"), .$xtr$transform(.$x()$input_breaks()), unit(1, "npc"), gp = gp, default.units="native")),

ggname("minor-horizontal", segmentsGrob(unit(0, "npc"), .$ytr$transform(.$y()$minor_breaks()), unit(1, "npc"), .$ytr$transform(.$y()$minor_breaks()), gp = gpar(col=plot$grid.minor.colour, lwd=0.5), default.units="native")),
ggname("major-horizontal",segmentsGrob(unit(0, "npc"), .$ytr$transform(.$y()$domain_breaks()), unit(1, "npc"), .$ytr$transform(.$y()$domain_breaks()), gp = gp, default.units="native")),
ggname("major-horizontal",segmentsGrob(unit(0, "npc"), .$ytr$transform(.$y()$input_breaks()), unit(1, "npc"), .$ytr$transform(.$y()$input_breaks()), gp = gp, default.units="native")),

ggname("border", rectGrob(gp=gpar(col=plot$grid.colour, lwd=3, fill=NA)))
)))
Expand Down
2 changes: 1 addition & 1 deletion R/facet-grid-viewports.r
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ viewport_default <- function(plot, guides, scales, coordinates) {
cols <- ncol(gm) + ncol(row.labels) + 1

layout <- plot_layout(gm, rows, cols, row.labels, col.labels, guides$axes_h, guides$axes_v, plot$aspect.ratio)
range <- coordinates$frange()
range <- coordinates$output_set()

viewports <- do.call("vpList", c(
setup_viewports("strip_h", data=t(col.labels), offset=c(0,1), range=range),
Expand Down
2 changes: 1 addition & 1 deletion R/geom-abline.r
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ GeomAbline <- proto(Geom, {
draw <- function(., data, scales, coordinates, ...) {
data <- aesdefaults(data, .$default_aes(), list(...))

xrange <- coordinates$frange()$x
xrange <- coordinates$output_set()$x

data <- transform(data,
x = xrange[1],
Expand Down
2 changes: 1 addition & 1 deletion R/geom-hline.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GeomHline <- proto(Geom, {
data$intercept <- intercept
}

xrange <- coordinates$frange()$x
xrange <- coordinates$output_set()$x

data <- transform(data,
x = xrange[1],
Expand Down
2 changes: 1 addition & 1 deletion R/geom-vline.r
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GeomVline <- proto(Geom, {
data$intercept <- intercept
}

yrange <- coordinates$frange()$y
yrange <- coordinates$output_set()$y

data <- transform(data,
y = yrange[1],
Expand Down
4 changes: 2 additions & 2 deletions R/position-jitter.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ PositionJitter <- proto(Position, {
adjust <- function(., data, scales) {
check_required_aesthetics(c("x", "y"), names(data), "position_jitter")

xrange <- diff(scales$get_scales("x")$frange())
yrange <- diff(scales$get_scales("y")$frange())
xrange <- diff(scales$get_scales("x")$output_set())
yrange <- diff(scales$get_scales("y")$output_set())

if (is.null(.$xjitter)) .$xjitter <- (is.integeric(resolution(data$x))) * 1
if (is.null(.$yjitter)) .$yjitter <- (is.integeric(resolution(data$y))) * 1
Expand Down
11 changes: 5 additions & 6 deletions R/scale-.r
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ Scale <- proto(TopLevel, expr={
new <- function(., name="Unknown") {
.$proto(name=name)
}
discrete <- function(.) FALSE

clone <- function(.) {
as.proto(.$as.list(all.names=TRUE), parent=.)
}

trained <- function(.) {
!is.null(.$domain())
!is.null(.$input_set())
}

find <- function(., output, only.documented = FALSE) {
Expand All @@ -35,7 +34,7 @@ Scale <- proto(TopLevel, expr={
input <- function(.) .$.input
output <- function(.) .$.output

domain <- function(.) {
input_set <- function(.) {
nulldefault(.$limits, .$.domain)
}

Expand Down Expand Up @@ -65,7 +64,7 @@ Scale <- proto(TopLevel, expr={
legend_desc <- function(.) {
if (identical(., Scale) || !.$legend) return()

breaks <- .$range_breaks()
breaks <- .$output_breaks()
labels <- .$labels()

if (is.null(breaks) || is.null(labels)) return()
Expand All @@ -84,8 +83,8 @@ Scale <- proto(TopLevel, expr={
clist <- function(x) paste(x, collapse=",")

cat("scale_", .$objname, ": ", clist(.$input()), " -> ", clist(.$output()), sep="")
if (!is.null(.$domain())) {
cat(" (", clist(.$domain()), " -> ", clist(.$frange()), ")", sep="")
if (!is.null(.$input_set())) {
cat(" (", clist(.$input_set()), " -> ", clist(.$output_set()), ")", sep="")
}
if (newline) cat("\n")
}
Expand Down
19 changes: 10 additions & 9 deletions R/scale-continuous-.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ScaleContinuous <- proto(Scale, {
ScaleContinuous <- proto(Scale, funEnvir = globalenv(), {
.domain <- c()
.range <- c()
.expand <- c(0.05, 0)
.labels <- NULL
discrete <- function(.) FALSE

tr_default <- "identity"

Expand Down Expand Up @@ -45,7 +46,7 @@ ScaleContinuous <- proto(Scale, {
if (!is.numeric(x))
warning("Non-continuous variable supplied to continuous ", .$my_name(), ".", call.=FALSE)
if (all(is.na(x))) return()

.$.domain <- range(x, .$.domain, na.rm=TRUE, finite=TRUE)
}

Expand All @@ -55,19 +56,19 @@ ScaleContinuous <- proto(Scale, {

# By default, the range of a continuous scale is the same as its
# (transformed) domain
frange <- function(.) .$domain()
output_set <- function(.) .$input_set()

# By default, breaks are regularly spaced along the (transformed) domain
breaks <- NULL
domain_breaks <- function(.) {
nulldefault(.$breaks, grid.pretty(.$domain()))
input_breaks <- function(.) {
nulldefault(.$breaks, grid.pretty(.$input_set()))
}
range_breaks <- function(.) .$map(.$domain_breaks())
output_breaks <- function(.) .$map(.$input_breaks())

.minor_breaks <- 2
# Minor breaks are regular on the original scale
# and need to cover entire range of plot
minor_breaks <- function(., n = .$.minor_breaks, b = .$domain_breaks(), r = .$frange()) {
minor_breaks <- function(., n = .$.minor_breaks, b = .$input_breaks(), r = .$output_set()) {
if (length(b) == 1) return(b)

bd <- diff(b)[1]
Expand All @@ -78,7 +79,7 @@ ScaleContinuous <- proto(Scale, {

labels <- function(.) {
if (!is.null(.$.labels)) return(.$.labels)
b <- .$domain_breaks()
b <- .$input_breaks()

l <- .$.tr$label(b)
numeric <- sapply(l, is.numeric)
Expand All @@ -88,7 +89,7 @@ ScaleContinuous <- proto(Scale, {

test <- function(.) {
m <- .$minor_breaks(10)
b <- .$domain_breaks()
b <- .$input_breaks()

plot(x=0,y=0,xlim=range(c(b,m)), ylim=c(1,5), type="n", axes=F,xlab="", ylab="")
for(i in 1:(length(b))) axis(1, b[[i]], as.expression(.$labels()[[i]]))
Expand Down
Loading

0 comments on commit c2e0d84

Please sign in to comment.