Skip to content

Commit

Permalink
Getting closer to passing R CMD check
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jan 4, 2008
1 parent 995ce82 commit 2d21dd1
Show file tree
Hide file tree
Showing 19 changed files with 68 additions and 39 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions R/coordinates-cartesian-flipped.r
Expand Up @@ -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()
}
})
12 changes: 5 additions & 7 deletions R/matrix.r
Expand Up @@ -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))
Expand All @@ -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)
}

14 changes: 13 additions & 1 deletion R/options.r
Expand Up @@ -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")
}
}

.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()
3 changes: 2 additions & 1 deletion R/plot-construction.r
Expand Up @@ -37,5 +37,6 @@
)
}
if (ggopt()$auto.print & length(p$layers) > 0) try(print(p))
(.last_plot <<- p)
set_last_plot(p)
p
}
3 changes: 2 additions & 1 deletion R/plot.r
Expand Up @@ -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
}


Expand Down
2 changes: 1 addition & 1 deletion R/save.r
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions R/scale-continuous-.r
Expand Up @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions R/scales-.r
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion R/stat-sortx.r
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions R/templates.r
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion R/utilities-geoms.r
Expand Up @@ -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.
#
Expand Down
6 changes: 3 additions & 3 deletions R/weighting.r
Expand Up @@ -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))
# }
#
Expand Down Expand Up @@ -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)
# }
#
Expand Down
2 changes: 1 addition & 1 deletion man/aes-string-9e.rd
Expand Up @@ -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}}}
Expand Down
7 changes: 3 additions & 4 deletions man/build-options-8a.rd
Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion man/ggaxis-60.rd
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion man/ggsave-ao.rd
Expand Up @@ -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}
Expand Down
17 changes: 16 additions & 1 deletion man/ignore.rd
Expand Up @@ -100,6 +100,7 @@
\alias{nice_ramp}
\alias{opts}
\alias{pd}
\alias{plist}
\alias{plot_clone}
\alias{PositionDodge}
\alias{PositionFill}
Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -304,6 +317,7 @@
\alias{TransLog}
\alias{TransPow10}
\alias{TransProbit}
\alias{TransReverse}
\alias{TransSqrt}
\alias{Trans}
\alias{try_require}
Expand All @@ -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 <h.wickham@gmail.com>}
Expand Down
6 changes: 2 additions & 4 deletions man/plotmatrix-95.rd
Expand Up @@ -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}

0 comments on commit 2d21dd1

Please sign in to comment.