Permalink
Browse files

Updates for R CMD check

  • Loading branch information...
hadley committed Jan 25, 2012
1 parent 9cf98a7 commit 5b1f9e977054bfc810d304122929d327aca3d879
View
@@ -34,7 +34,8 @@ Suggests:
hexbin,
gpclib,
maptools,
- multcomp
+ multcomp,
+ nlme
Extends:
sp
License: GPL-2
View
@@ -30,7 +30,11 @@
#'
#' # Using linetypes
#' library(reshape2) # for melt
-#' ec_scaled <- data.frame(date = economics$date, rescaler(economics[, -(1:2)], "range"))
+#' library(plyr) # for colwise
+#' rescale01 <- function(x) (x - min(x)) / diff(range(x))
+#' ec_scaled <- data.frame(
+#' date = economics$date,
+#' colwise(rescale01)(economics[, -(1:2)]))
#' ecm <- melt(ec_scaled, id = "date")
#' f <- ggplot(ecm, aes(date, value))
#' f + geom_line(aes(linetype = variable))
@@ -57,10 +61,10 @@
#' h <- h + geom_line(aes(group = Subject))
#' # Using the group aesthetic with both geom_line() and geom_smooth()
#' # groups the data the same way for both layers
-#' h + geom_smooth(aes(group = Subject), method = "lm", se = F)
+#' h + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
#' # Changing the group aesthetic for the smoother layer
#' # fits a single line of best fit across all boys
-#' h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = F)
+#' h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE)
#'
#' # Overriding the default grouping
#' # The plot has a discrete scale but you want to draw lines that connect across
View
@@ -11,13 +11,14 @@
#' # for each type of cut
#' dmod <- lm(price ~ cut, data = diamonds)
#' cuts <- data.frame(cut = unique(diamonds$cut), predict(dmod, data.frame(cut =
-#' unique(diamonds$cut)), se = T)[c("fit", "se.fit")])
+#' unique(diamonds$cut)), se = TRUE)[c("fit", "se.fit")])
#' se <- ggplot(cuts, aes(x = cut, y = fit, ymin = fit - se.fit,
#' ymax = fit + se.fit, colour = cut))
#' se + geom_pointrange()
#'
#' # Boxplot with precomputed statistics
#' # generate sample data
+#' library(plyr)
#' abc <- adply(matrix(rnorm(100), ncol = 5), 2, quantile, c(0, .25, .5, .75, 1))
#' b <- ggplot(abc, aes(x = X1, ymin = "0%", lower = "25%", middle = "50%", upper = "75%", ymax = "100%"))
#' b + geom_boxplot(stat = "identity")
View
@@ -28,13 +28,15 @@ NULL
#' require(gridExtra)
#' base + annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
#' xmin = 3, xmax = 6, ymin = 2, ymax = 8)
-#' }
-#' # Inset plot
-#' g <- ggplotGrob(qplot(1, 1) + opts(plot.background = theme_rect(col = "black")))
-#' base + annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
#' # full panel
#' base + annotation_custom(grob = roundrectGrob(),
#' xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
+#' }
+#' # Inset plot
+#' g <- ggplotGrob(qplot(1, 1) +
+#' opts(plot.background = theme_rect(col = "black")))
+#' base +
+#' annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
annotation_custom <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf) {
GeomCustomAnn$new(geom_params = list(grob = grob, xmin = xmin,
xmax = xmax, ymin = ymin, ymax = ymax), stat = "identity",
View
@@ -17,7 +17,7 @@
#' # Suppose you then wanted to remove the geom_point layer
#' # If you just remove geom_point, you will get an error
#' b <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl)
-#' b + geom_abline(aes(intercept = a, slope = b), data = df)
+#' \dontrun{b + geom_abline(aes(intercept = a, slope = b), data = df)}
#' # Switching to geom_blank() gets the desired plot
#' c <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl) + geom_blank()
#' c + geom_abline(aes(intercept = a, slope = b), data = df)
View
@@ -13,7 +13,9 @@
#'
#' # High-level plotting commands
#'
-#' plot(x, y); dotchart(x, y); stripchart(x, y)
+#' x <- runif(10)
+#' y <- 1:10
+#' plot(x, y); dotchart(x, y)
#' qplot(x, y)
#'
#' plot(x, y, type = "l")
@@ -31,17 +33,17 @@
#' hist(x)
#' qplot(x, geom = "histogram")
#'
-#' cdplot(x, y)
-#' qplot(x, fill = y, geom = "density", position = "fill")
+#' # cdplot(factor(x), y)
+#' # qplot(x, fill = y, geom = "density", position = "fill")
#'
-#' coplot(y ~ x | a + b)
-#' qplot(x, y, facets = a ~ b)
+#' # coplot(y ~ x | a + b)
+#' # qplot(x, y, facets = a ~ b)
#'
#' # Many of the geoms are parameterised differently than base graphics. For
#' # example, hist() is parameterised in terms of the number of bins, while
#' # geom_histogram() is parameterised in terms of the width of each bin.
-#' hist(x, bins = 100)
-#' qplot(x, geom = "histogram", binwidth = 1)
+#' hist(x, bins = 10)
+#' qplot(x, geom = "histogram", binwidth = .1)
#'
#' # qplot() often requires data in a slightly different format to the base
#' # graphics functions. For example, the bar geom works with untabulated data,
@@ -51,18 +53,19 @@
#' qplot(x, geom = "bar")
#'
#' barplot(x)
-#' qplot(names(x), x, geom = "bar", stat = "identity")
+#' qplot(seq_along(x), x, geom = "bar", stat = "identity")
#'
-#' image(x)
-#' qplot(X1, X2, data = melt(x), geom = "tile", fill = value)
+#' # image(x)
+#' # qplot(X1, X2, data = melt(x), geom = "tile", fill = value)
#'
-#' contour(x)
-#' qplot(X1, X2, data = melt(x), geom = "contour", fill = value)
+#' # contour(x)
+#' # qplot(X1, X2, data = melt(x), geom = "contour", fill = value)
#'
#' # Generally, the base graphics functions work with individual vectors, not
#' # data frames like ggplot2. qplot() will try to construct a data frame if one
#' # is not specified, but it is not always possible. If you get strange errors,
#' # you may need to create the data frame yourself.
+#' df <- data.frame(x = x, y = y)
#' with(df, plot(x, y))
#' qplot(x, y, data = df)
#'
@@ -122,10 +125,11 @@
#' qplot(1:5, 1:5, col = factor(1:5), size = I(4))
#' last_plot() + scale_colour_manual(values = rainbow(5))
#'
-#' # In ggplot2, you can also use palettes with continuous values, with intermediate
-#' # values being linearly interpolated.
+#' # In ggplot2, you can also use palettes with continuous values,
+#' # with intermediate values being linearly interpolated.
#'
-#' qplot(0:100, 0:100, col = 0:100, size = I(4)) + scale_colour_gradientn(colours = rainbow(7))
+#' qplot(0:100, 0:100, col = 0:100, size = I(4)) +
+#' scale_colour_gradientn(colours = rainbow(7))
#' last_plot() + scale_colour_gradientn(colours = terrain.colors(7))
#'
#' # Graphical parameters
View
@@ -12,71 +12,71 @@
#'
#' # By default, qplot() assumes that you want a scatterplot,
#' # i.e., you want to use geom_point()
-#' qplot(x, y, data = data)
-#' ggplot(data, aes(x, y)) + geom_point()
+#' # qplot(x, y, data = data)
+#' # ggplot(data, aes(x, y)) + geom_point()
#'
#' # Using Aesthetics
#'
#' # If you map additional aesthetics, these will be added to the defaults. With
#' # qplot() there is no way to use different aesthetic mappings (or data) in
#' # different layers
-#' qplot(x, y, data = data, shape = shape, colour = colour)
-#' ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
-#'
+#' # qplot(x, y, data = data, shape = shape, colour = colour)
+#' # ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
+#' #
#' # Aesthetic parameters in qplot() always try to map the aesthetic to a
#' # variable. If the argument is not a variable but a value, effectively a new column
#' # is added to the original dataset with that value. To set an aesthetic to a
#' # value and override the default appearance, you surround the value with I() in
#' # qplot(), or pass it as a parameter to the layer.
-#' qplot(x, y, data = data, colour = I("red"))
-#' ggplot(data, aes(x, y)) + geom_point(colour = "red")
+#' # qplot(x, y, data = data, colour = I("red"))
+#' # ggplot(data, aes(x, y)) + geom_point(colour = "red")
#'
#' # Changing the geom parameter changes the geom added to the plot
-#' qplot(x, y, data = data, geom = "line")
-#' ggplot(data, aes(x, y)) + geom_line()
+#' # qplot(x, y, data = data, geom = "line")
+#' # ggplot(data, aes(x, y)) + geom_line()
#'
#' # Not all geoms require both x and y, e.g., geom_bar() and geom_histogram().
#' # For these two geoms, if the y aesthetic is not supplied, both qplot and
#' # ggplot commands default to "count" on the y-axis
-#' ggplot(data, aes(x)) + geom_bar()
-#' qplot(x, data = data, geom = "bar")
+#' # ggplot(data, aes(x)) + geom_bar()
+#' # qplot(x, data = data, geom = "bar")
#'
#' # If a vector of multiple geom names is supplied to the geom argument, each
#' # geom will be added in turn
-#' qplot(x, y, data = data, geom = c("point", "smooth"))
-#' ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
+#' # qplot(x, y, data = data, geom = c("point", "smooth"))
+#' # ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
#'
#' # Unlike the rest of ggplot2, stats and geoms are independent
-#' qplot(x, y, data = data, stat = "bin")
-#' ggplot(data, aes(x, y)) + geom_point(stat = "bin")
-#'
+#' # qplot(x, y, data = data, stat = "bin")
+#' # ggplot(data, aes(x, y)) + geom_point(stat = "bin")
+#' #
#' # Any layer parameters will be passed on to all layers. Most layers will ignore
#' # parameters that they don't need
-#' qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
-#' ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")
+#' # qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
+#' # ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")
#'
#' # Scales and axes
#'
#' # You can control basic properties of the x and y scales with the xlim, ylim,
#' # xlab and ylab arguments
-#' qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label")
-#' ggplot(data, aes(x, y)) + geom_point() +
-#' scale_x_continuous("my label", limits = c(1, 5))
+#' # qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label")
+#' # ggplot(data, aes(x, y)) + geom_point() +
+#' # scale_x_continuous("my label", limits = c(1, 5))
#'
-#' qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
-#' ggplot(data, aes(x, y)) + geom_point() +
-#' scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
+#' # qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
+#' # ggplot(data, aes(x, y)) + geom_point() +
+#' # scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
#'
#' # Like plot(), qplot() has a convenient way of log transforming the axes.
-#' qplot(x, y, data = data, log = "xy")
-#' ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()
+#' # qplot(x, y, data = data, log = "xy")
+#' # ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()
#' # There are many other possible transformations, but not all are
#' # accessible from within qplot(), see ?scale_continuous for more
#'
#' # Plot options
#'
#' # qplot() recognises the same options as plot does, and converts them to their
#' # ggplot2 equivalents. See ?opts for more on ggplot options
-#' qplot(x, y, data = data, main="title", asp = 1)
-#' ggplot(data, aes(x, y)) + geom_point() + opts(title = "title", aspect.ratio = 1)
+#' # qplot(x, y, data = data, main="title", asp = 1)
+#' # ggplot(data, aes(x, y)) + geom_point() + opts(title = "title", aspect.ratio = 1)
NULL
View
@@ -10,12 +10,12 @@
#' # page 13, of Leland Wilkinson's "The Grammar of Graphics."
#' # Springer, 2nd edition, 2005.
#'
-#' DATA: source("demographics")
-#' DATA: longitude, latitude = map(source("World"))
-#' TRANS: bd = max(birth - death, 0)
-#' COORD: project.mercator()
-#' ELEMENT: point(position(lon * lat), size(bd), color(color.red))
-#' ELEMENT: polygon(position(longitude * latitude))
+#' # DATA: source("demographics")
+#' # DATA: longitude, latitude = map(source("World"))
+#' # TRANS: bd = max(birth - death, 0)
+#' # COORD: project.mercator()
+#' # ELEMENT: point(position(lon * lat), size(bd), color(color.red))
+#' # ELEMENT: polygon(position(longitude * latitude))
#'
#' # This is relatively simple to adapt to the syntax of ggplot2:
#'
@@ -47,10 +47,10 @@
#' # Each component is added together with + to create the final plot.
#'
#' # Resulting ggplot2 code:
-#' demographics <- transform(demographics, bd = pmax(birth - death, 0))
-#' p <- ggplot(demographic, aes(lon, lat))
-#' p <- p + geom_polyogon(data = world)
-#' p <- p + geom_point(aes(size = bd), colour = "red")
-#' p <- p + coord_map(projection = "mercator")
-#' print(p)
+#' # demographics <- transform(demographics, bd = pmax(birth - death, 0))
+#' # p <- ggplot(demographic, aes(lon, lat))
+#' # p <- p + geom_polyogon(data = world)
+#' # p <- p + geom_point(aes(size = bd), colour = "red")
+#' # p <- p + coord_map(projection = "mercator")
+#' # print(p)
NULL
@@ -6,7 +6,7 @@
#'
#' @name translate_qplot_lattice
#' @examples
-#'
+#' \dontrun{
#' xyplot(rating ~ year, data=movies)
#' qplot(year, rating, data=movies)
#'
@@ -69,4 +69,5 @@
#' # and trellis.par.get() to theme_set() and theme_get().
#' # More complicated lattice formulas are equivalent to rearranging the data
#' # before using ggplot2.
+#' }
NULL
View
@@ -32,7 +32,11 @@ a + geom_bar(aes(fill = factor(vs)))
# Using linetypes
library(reshape2) # for melt
-ec_scaled <- data.frame(date = economics$date, rescaler(economics[, -(1:2)], "range"))
+library(plyr) # for colwise
+rescale01 <- function(x) (x - min(x)) / diff(range(x))
+ec_scaled <- data.frame(
+ date = economics$date,
+ colwise(rescale01)(economics[, -(1:2)]))
ecm <- melt(ec_scaled, id = "date")
f <- ggplot(ecm, aes(date, value))
f + geom_line(aes(linetype = variable))
@@ -59,10 +63,10 @@ h + geom_line(aes(group = Subject))
h <- h + geom_line(aes(group = Subject))
# Using the group aesthetic with both geom_line() and geom_smooth()
# groups the data the same way for both layers
-h + geom_smooth(aes(group = Subject), method = "lm", se = F)
+h + geom_smooth(aes(group = Subject), method = "lm", se = FALSE)
# Changing the group aesthetic for the smoother layer
# fits a single line of best fit across all boys
-h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = F)
+h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = FALSE)
# Overriding the default grouping
# The plot has a discrete scale but you want to draw lines that connect across
View
@@ -18,13 +18,14 @@
# for each type of cut
dmod <- lm(price ~ cut, data = diamonds)
cuts <- data.frame(cut = unique(diamonds$cut), predict(dmod, data.frame(cut =
-unique(diamonds$cut)), se = T)[c("fit", "se.fit")])
+unique(diamonds$cut)), se = TRUE)[c("fit", "se.fit")])
se <- ggplot(cuts, aes(x = cut, y = fit, ymin = fit - se.fit,
ymax = fit + se.fit, colour = cut))
se + geom_pointrange()
# Boxplot with precomputed statistics
# generate sample data
+library(plyr)
abc <- adply(matrix(rnorm(100), ncol = 5), 2, quantile, c(0, .25, .5, .75, 1))
b <- ggplot(abc, aes(x = X1, ymin = "0\%", lower = "25\%", middle = "50\%", upper = "75\%", ymax = "100\%"))
b + geom_boxplot(stat = "identity")
View
@@ -41,12 +41,14 @@ base <- qplot(1:10, 1:10, geom = "blank") + theme_bw()
require(gridExtra)
base + annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
xmin = 3, xmax = 6, ymin = 2, ymax = 8)
-}
-# Inset plot
-g <- ggplotGrob(qplot(1, 1) + opts(plot.background = theme_rect(col = "black")))
-base + annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
# full panel
base + annotation_custom(grob = roundrectGrob(),
xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
}
+# Inset plot
+g <- ggplotGrob(qplot(1, 1) +
+ opts(plot.background = theme_rect(col = "black")))
+base +
+ annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
+}
View
@@ -41,7 +41,7 @@ a + geom_abline(aes(intercept = a, slope = b), data = df)
# Suppose you then wanted to remove the geom_point layer
# If you just remove geom_point, you will get an error
b <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl)
-b + geom_abline(aes(intercept = a, slope = b), data = df)
+\dontrun{b + geom_abline(aes(intercept = a, slope = b), data = df)}
# Switching to geom_blank() gets the desired plot
c <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl) + geom_blank()
c + geom_abline(aes(intercept = a, slope = b), data = df)
Oops, something went wrong.

0 comments on commit 5b1f9e9

Please sign in to comment.