diff --git a/DESCRIPTION b/DESCRIPTION index e53f3c76ee..fcc02e6f6e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,6 +9,7 @@ Description: An implementation of the grammar of graphics in R. It combines the Depends: R (>= 2.6), grid, reshape (>= 0.8.0), proto, splines, MASS, RColorBrewer, colorspace Suggests: quantreg, Hmisc, mapproj, maps, digest +Imports: proto License: GPL URL: http://had.co.nz/ggplot2/ LazyLoad: false diff --git a/R/coordinates-cartesian-flipped.r b/R/coordinates-cartesian-flipped.r index 161ddde049..2bdcbce8ce 100644 --- a/R/coordinates-cartesian-flipped.r +++ b/R/coordinates-cartesian-flipped.r @@ -25,16 +25,16 @@ CoordFlipped <- proto(CoordCartesian, expr={ # Very useful for creating boxplots, and other interval # geoms in the horizontal instead of vertical position. qplot(cut, price, data=diamonds, geom="boxplot") - .last_plot + coord_flip() + last_plot() + coord_flip() qplot(cut, data=diamonds, geom="bar") - .last_plot + coord_flip() + last_plot() + coord_flip() qplot(carat, data=diamonds, geom="histogram") - .last_plot + coord_flip() + last_plot() + coord_flip() # You can also use it to flip lines and area plots: qplot(1:5, (1:5)^2, geom="line") - .last_plot + coord_flip() + last_plot() + coord_flip() } }) diff --git a/R/matrix.r b/R/matrix.r index 41f82dc8e9..863676bc89 100644 --- a/R/matrix.r +++ b/R/matrix.r @@ -4,10 +4,8 @@ # @arguments data frame # @arguments any additional aesthetic mappings (do not use x and y) # @keyword hplot -#X plotmatrix(mtcars) -#X plotmatrix(mtcars, aes(colour=factor(cyl))) -#X plotmatrix(mtcars) + geom_smooth(method="lm") -#X plotmatrix(mtcars, aes(colour=factor(cyl))) +#X plotmatrix(mtcars[, 1:3]) +#X plotmatrix(mtcars[, 1:3]) + geom_smooth(method="lm") plotmatrix <- function(data, mapping=aes(), colour="black") { data <- rescaler(data, "range") grid <- expand.grid(x=1:ncol(data), y=1:ncol(data)) @@ -29,17 +27,17 @@ plotmatrix <- function(data, mapping=aes(), colour="black") { densities <- do.call("rbind", lapply(1:ncol(data), function(i) { data.frame( xvar = names(data)[i], - yvar=names(data)[i], + yvar = names(data)[i], x = data[, i] ) })) - mapping <- defaults(mapping, aes(x=x, y=y)) + mapping <- defaults(mapping, aes_string(x="x", y="y")) class(mapping) <- "uneval" ggplot(all, mapping) + facet_grid(xvar ~ yvar) + geom_point(colour = colour) + scale_x_continuous("", limits=c(0, 1), breaks = seq(0,1, length=4), labels = "") + scale_y_continuous("", limits=c(0, 1), breaks = seq(0,1, length=4), labels = "") + - stat_density(aes(x=x, y = ..scaled..), data=densities, position="identity", fill="grey60", colour=NA) + stat_density(aes_string(x="x", y = "..scaled.."), data=densities, position="identity", fill="grey60", colour=NA) } diff --git a/R/options.r b/R/options.r index 3ec4c81e05..4803655f81 100644 --- a/R/options.r +++ b/R/options.r @@ -158,4 +158,16 @@ update.ggplot <- function(object, ...) { if (length(dots) == 1 && is.list(dots[[1]])) dots <- dots[[1]] structure(defaults(dots, object), class="ggplot") -} \ No newline at end of file +} + +.plot_store <- function() { + .last_plot <- NULL + + list( + get = function() .last_plot, + set = function(value) .last_plot <<- value + ) +} +.store <- .plot_store() +set_last_plot <- function(value) .store$set(value) +last_plot <- function() .store$get() \ No newline at end of file diff --git a/R/plot-construction.r b/R/plot-construction.r index 4c3066611d..9c711b2a5b 100644 --- a/R/plot-construction.r +++ b/R/plot-construction.r @@ -37,5 +37,6 @@ ) } if (ggopt()$auto.print & length(p$layers) > 0) try(print(p)) - (.last_plot <<- p) + set_last_plot(p) + p } \ No newline at end of file diff --git a/R/plot.r b/R/plot.r index 5815976d10..112781d1eb 100644 --- a/R/plot.r +++ b/R/plot.r @@ -24,7 +24,8 @@ ggplot.default <- function(data = NULL, mapping=aes(), ...) { p$facet <- FacetGrid$new() p$scales$add_defaults(p$data, p$defaults) - (.last_plot <<- p) + set_last_plot(p) + p } diff --git a/R/save.r b/R/save.r index 518862d336..91b9cc747f 100644 --- a/R/save.r +++ b/R/save.r @@ -26,7 +26,7 @@ #X # make twice as big as on screen #X ggsave(ratings, file="ratings.pdf", scale=2) #X } -ggsave <- function(plot = .last_plot, filename=default_name(plot), device=default_device(filename), scale=1, width=par("din")[1], height=par("din")[2], dpi=96, ...) { +ggsave <- function(plot = last_plot(), filename=default_name(plot), device=default_device(filename), scale=1, width=par("din")[1], height=par("din")[2], dpi=96, ...) { ps <- function(..., width, height) grDevices::ps(..., width=width, height=height) tex <- function(..., width, height) grDevices::pictex(..., width=width, height=height) diff --git a/R/scale-continuous-.r b/R/scale-continuous-.r index bfb5b77057..581d5966d5 100644 --- a/R/scale-continuous-.r +++ b/R/scale-continuous-.r @@ -27,6 +27,7 @@ ScaleContinuous <- proto(Scale, { } train <- function(., x) { + if (is.null(x)) return() if (!is.numeric(x)) warning("Non-numeric variable supplied to continuous scale ", .$name, ".", call.=FALSE) if (all(is.na(x))) return() diff --git a/R/scales-.r b/R/scales-.r index f9171dc324..064e1d55fe 100644 --- a/R/scales-.r +++ b/R/scales-.r @@ -60,6 +60,8 @@ Scales <- proto(Scale, expr={ # Train scale from a data frame train_df <- function(., df) { + if (is.null(df)) return() + lapply(.$.scales, function(scale) { if (all(scale$input() %in% names(df))) scale$train_df(df) }) diff --git a/R/stat-sortx.r b/R/stat-sortx.r index 5023e4dd98..6d1341d468 100644 --- a/R/stat-sortx.r +++ b/R/stat-sortx.r @@ -4,7 +4,7 @@ StatSort <- proto(Stat, { default_geom <- function(.) GeomPath calculate_groups <- function(., data, scales, variable="x", ...) { - as.data.frame(data)[order(data$group, data$x[[variable]]), ] + as.data.frame(data)[order(data$group, data[[variable]]), ] } calculate <- calculate_groups diff --git a/R/templates.r b/R/templates.r index e0803d40d3..d83f2c7e37 100644 --- a/R/templates.r +++ b/R/templates.r @@ -41,7 +41,7 @@ ggpcp <- function(data, vars=names(data), scale="range", ...) { data$ROWID <- 1:nrow(data) molten <- melt(data, m=vars) - p <- ggplot(molten, aes(x=variable, y=value, group=ROWID), ...) + p <- ggplot(molten, aes_string(x="variable", y="value", group="ROWID"), ...) p + scale_x_discrete() } @@ -88,9 +88,9 @@ ggfluctuation <- function(table, type="size", floor=0, ceiling=max(table$freq, n table <- subset(table, freq * ceiling >= floor) if (type=="size") { - p <- ggplot(table, aes(x=x, y=y, height=freq, width=freq, fill=border)) + geom_tile(colour="white") + scale_fill_identity() + p <- ggplot(table, aes_string(x="x", y="y", height="freq", width="freq", fill="border")) + geom_tile(colour="white") + scale_fill_identity() } else { - p <- ggplot(table, aes(x=x, y=y, fill=freq)) + + p <- ggplot(table, aes_string(x="x", y="y", fill="freq")) + geom_tile(colour="grey50") + scale_fill_gradient2(low="white", high="darkgreen") } @@ -140,7 +140,7 @@ ggmissing <- function(data, avoid="stack", order=TRUE, missing.only = TRUE) { df$variable <- factor(df$variable) } - ggplot(df, aes(y=Freq, x=variable, fill=missing)) + geom_bar(position=avoid) + ggplot(df, aes_string(y="Freq", x="variable", fill="missing")) + geom_bar(position=avoid) } # Structure plot @@ -152,7 +152,7 @@ ggmissing <- function(data, avoid="stack", order=TRUE, missing.only = TRUE) { #X ggstructure(mtcars) ggstructure <- function(data, scale = "rank") { ggpcp(data, scale=scale) + - aes(y=ROWID, fill=value, x=variable) + + aes_string(y="ROWID", fill="value", x="variable") + geom_tile() + scale_y_continuous("row number", expand = c(0, 1)) + scale_fill_gradient2(low="blue", mid="white", high="red", midpoint=0) @@ -166,7 +166,7 @@ ggstructure <- function(data, scale = "rank") { # @keyword hplot ggorder <- function(data, scale="rank") { ggpcp(data, scale="rank") + - aes(x=ROWID, group=variable, y=value) + + aes_string(x="ROWID", group="variable", y="value") + facet_grid(. ~ variable) + geom_line() + scale_x_continuous("row number") @@ -186,7 +186,7 @@ ggdist <- function(data, vars=names(data), facets = . ~ .) { mapply(function(name, cat, i) { p <- ggplot(data) + facet_grid(facets) + - aes(x=as.name(name), y=1) + + aes_string(x=name, y=1) + geom_bar() pushViewport(viewport(layout.pos.col=i)) diff --git a/R/utilities-geoms.r b/R/utilities-geoms.r index c41c515dd7..60b02a3e82 100644 --- a/R/utilities-geoms.r +++ b/R/utilities-geoms.r @@ -30,7 +30,7 @@ aes <- function(...) { # Generate aesthetic mappings from a string # Aesthetic mappings describe how variables in the data are mapped to visual properties (aesthetics) of geoms. Compared to aes this function operates on strings rather than expressions. # -# aes_string is particularly useful when writing functions that create +# \code{aes_string} is particularly useful when writing functions that create # plots because you can use strings to define the aesthetic mappings, rather # than having to mess around with expressions. # diff --git a/R/weighting.r b/R/weighting.r index ce8354d838..00cdeca00a 100644 --- a/R/weighting.r +++ b/R/weighting.r @@ -17,11 +17,11 @@ # qplot(wt, mpg, data=mtcars, weight=cyl, geom="wdensity", radius=1/20) # radius in proportion of data range, defaults to 1% # qplot(wt, mpg, data=mtcars, weight=cyl, geom="wdensity", trans=function(x) sqrt(x)) # -# scalpha <- function(plot = .last_plot, name=NULL, colour="black", maxalpha=0.5) { +# scalpha <- function(plot = last_plot(), name=NULL, colour="black", maxalpha=0.5) { # add_scale(plot, scale_gradient(name=name, low=alpha(colour, 0), mid=alpha(colour, 0), high=alpha(colour, maxalpha), variable="weight", range=c(0,NA))) # } # -# scconserve <- function(plot = .last_plot, name=NULL, max=NA, colour="black", size.to = c(0.2, 5), alpha.to = c(1, 0)) { +# scconserve <- function(plot = last_plot(), name=NULL, max=NA, colour="black", size.to = c(0.2, 5), alpha.to = c(1, 0)) { # add_scale(plot, scale_conserve(name=name, max=max, colour=colour, size.to=size.to, alpha.to=alpha.to)) # } # @@ -56,7 +56,7 @@ # # #sm.density(mtcars[,c("wt", "mpg")], display = "none", ngrid=200, h=c(0.1,0.1), eval.points=as.matrix((mtcars[,c("wt", "mpg")]))) # -# ggwdensity <- function(plot = .last_plot, aes=aes(), ..., data=NULL) { +# ggwdensity <- function(plot = last_plot(), aes=aes(), ..., data=NULL) { # gg_add("wdensity", plot, aesthetics, ..., data=data) # } # diff --git a/man/aes-string-9e.rd b/man/aes-string-9e.rd index e15d8ce454..1de0b7557f 100644 --- a/man/aes-string-9e.rd +++ b/man/aes-string-9e.rd @@ -11,7 +11,7 @@ Aesthetic mappings describe how variables in the data are mapped to visual prope \item{...}{List of name value pairs} } -\details{aes_string is particularly useful when writing functions that create +\details{\code{aes_string} is particularly useful when writing functions that create plots because you can use strings to define the aesthetic mappings, rather than having to mess around with expressions.} \seealso{\code{\link{aes}}} diff --git a/man/build-options-8a.rd b/man/build-options-8a.rd index 1317f0baf1..b34ca0c7f7 100644 --- a/man/build-options-8a.rd +++ b/man/build-options-8a.rd @@ -70,10 +70,9 @@ p # a very ugly plot! ggopt(background.fill = "white", background.color ="black") p <- qplot(wt, mpg, data=mtcars, colour=factor(cyl)) -p$legend.position <- c(0.9,0.9); p -p$legend.position <- c(0.5,0.5) -p$legend.justification <- "center" -p +p + opts(legend.position = c(0.9,0.9)) +(p <- p + opts(legend.position = c(0.5,0.5))) +p + opts(legend.justification = "centre") DF <- data.frame( x=rnorm(20), diff --git a/man/ggaxis-60.rd b/man/ggaxis-60.rd index 753c72a672..62c4d7654b 100644 --- a/man/ggaxis-60.rd +++ b/man/ggaxis-60.rd @@ -6,12 +6,13 @@ \description{ Grob for axes } -\usage{ggaxis(at, labels, position="right", scale=c(0,1))} +\usage{ggaxis(at, labels, position="right", scale=c(0,1), colour = ggopt()$axis.colour)} \arguments{ \item{at}{position of ticks} \item{labels}{labels at ticks} \item{position}{position of axis (top, bottom, left or right)} \item{scale}{range of data values} +\item{colour}{} } \details{} diff --git a/man/ggsave-ao.rd b/man/ggsave-ao.rd index 7220127a3e..2ff49cfed1 100644 --- a/man/ggsave-ao.rd +++ b/man/ggsave-ao.rd @@ -6,7 +6,7 @@ \description{ Save a ggplot with sensible defaults } -\usage{ggsave(plot = .last_plot, filename=default_name(plot), device=default_device(filename), scale=1, width=par("din")[1], height=par("din")[2], dpi=96, ...)} +\usage{ggsave(plot = last_plot(), filename=default_name(plot), device=default_device(filename), scale=1, width=par("din")[1], height=par("din")[2], dpi=96, ...)} \arguments{ \item{plot}{plot to save, defaults to last plot displayed} \item{filename}{file name/path of plot} diff --git a/man/ignore.rd b/man/ignore.rd index 17bd792452..be74bf2d65 100644 --- a/man/ignore.rd +++ b/man/ignore.rd @@ -100,6 +100,7 @@ \alias{nice_ramp} \alias{opts} \alias{pd} +\alias{plist} \alias{plot_clone} \alias{PositionDodge} \alias{PositionFill} @@ -140,10 +141,12 @@ \alias{ScaleLog2} \alias{ScaleLogit} \alias{ScaleLog} +\alias{ScaleManual} \alias{ScalePow10} \alias{ScalePower} \alias{ScaleProbability} \alias{ScaleProbit} +\alias{ScaleReverse} \alias{ScaleShape} \alias{ScaleSizeDiscrete} \alias{ScaleSize} @@ -158,6 +161,7 @@ \alias{scale_colour_gradient} \alias{scale_colour_hue} \alias{scale_colour_identity} +\alias{scale_colour_manual} \alias{scale_fill_brewer} \alias{scale_fill_colour} \alias{scale_fill_continuous} @@ -166,12 +170,16 @@ \alias{scale_fill_gradient} \alias{scale_fill_hue} \alias{scale_fill_identity} +\alias{scale_fill_manual} \alias{scale_linetype_identity} +\alias{scale_linetype_manual} \alias{scale_linetype} \alias{scale_shape_identity} +\alias{scale_shape_manual} \alias{scale_shape} \alias{scale_size_discrete} \alias{scale_size_identity} +\alias{scale_size_manual} \alias{scale_size} \alias{scale_xend_asn} \alias{scale_xend_atanh} @@ -186,6 +194,7 @@ \alias{scale_xend_pow} \alias{scale_xend_probit} \alias{scale_xend_prob} +\alias{scale_xend_reverse} \alias{scale_xend_sqrt} \alias{scale_x_asn} \alias{scale_x_atanh} @@ -202,6 +211,7 @@ \alias{scale_x_pow} \alias{scale_x_probit} \alias{scale_x_prob} +\alias{scale_x_reverse} \alias{scale_x_sqrt} \alias{scale_yend_asn} \alias{scale_yend_atanh} @@ -216,6 +226,7 @@ \alias{scale_yend_pow} \alias{scale_yend_probit} \alias{scale_yend_prob} +\alias{scale_yend_reverse} \alias{scale_yend_sqrt} \alias{scale_y_asn} \alias{scale_y_atanh} @@ -232,6 +243,7 @@ \alias{scale_y_pow} \alias{scale_y_probit} \alias{scale_y_prob} +\alias{scale_y_reverse} \alias{scale_y_sqrt} \alias{scale_z_asn} \alias{scale_z_atanh} @@ -247,6 +259,7 @@ \alias{scale_z_pow} \alias{scale_z_probit} \alias{scale_z_prob} +\alias{scale_z_reverse} \alias{scale_z_sqrt} \alias{Scale} \alias{StatBin} @@ -304,6 +317,7 @@ \alias{TransLog} \alias{TransPow10} \alias{TransProbit} +\alias{TransReverse} \alias{TransSqrt} \alias{Trans} \alias{try_require} @@ -314,7 +328,8 @@ \alias{ymedian} \alias{yrange} \alias{\%+\%} - +\alias{set_last_plot} +\alias{last_plot} \keyword{hplot} \title{See website for documentation} \author{Hadley Wickham } diff --git a/man/plotmatrix-95.rd b/man/plotmatrix-95.rd index 8965666400..d8f358ac80 100644 --- a/man/plotmatrix-95.rd +++ b/man/plotmatrix-95.rd @@ -15,8 +15,6 @@ Crude experimental scatterplot matrix \details{} -\examples{plotmatrix(mtcars) -plotmatrix(mtcars, aes(colour=factor(cyl))) -plotmatrix(mtcars) + geom_smooth(method="lm") -plotmatrix(mtcars, aes(colour=factor(cyl))) } +\examples{plotmatrix(mtcars[, 1:3]) +plotmatrix(mtcars[, 1:3]) + geom_smooth(method="lm")} \keyword{hplot}