Skip to content
This repository
Browse code

Re-roxygenise

  • Loading branch information...
commit f62b084cd1b3ba2e5e3fe0888879f9be0208f733 1 parent f2dac1c
Hadley Wickham authored
6 DESCRIPTION
@@ -198,3 +198,9 @@ Collate:
198 198 'zzz.r'
199 199 'fortify-multcomp.r'
200 200 'annotation-custom.r'
  201 + 'aes-group-order.r'
  202 + 'aes-position.r'
  203 + 'translate-qplot-base.r'
  204 + 'translate-qplot-ggplot.r'
  205 + 'translate-qplot-gpl.r'
  206 + 'translate-qplot-lattice.r'
3  NAMESPACE
@@ -235,6 +235,9 @@ S3method(coord_distance,trans)
235 235 S3method(coord_labels,default)
236 236 S3method(coord_labels,flip)
237 237 S3method(coord_labels,polar)
  238 +S3method(coord_range,default)
  239 +S3method(coord_range,flip)
  240 +S3method(coord_range,polar)
238 241 S3method(coord_render_axis_h,default)
239 242 S3method(coord_render_axis_h,map)
240 243 S3method(coord_render_axis_h,polar)
90 man/aes_group_order.Rd
... ... @@ -0,0 +1,90 @@
  1 +\name{aes_group_order}
  2 +\alias{aes_group_order}
  3 +\alias{group}
  4 +\alias{order}
  5 +\title{Aesthetics: group, order}
  6 +\description{
  7 + Aesthetics: group, order
  8 +}
  9 +\examples{
  10 +# By default, the group is set to the interaction of all discrete variables in the
  11 +# plot. This often partitions the data correctly, but when it does not, or when
  12 +# no discrete variable is used in the plot, you will need to explicitly define the
  13 +# grouping structure, by mapping group to a variable that has a different value
  14 +# for each group.
  15 +
  16 +# For most applications you can simply specify the grouping with
  17 +# various aesthetics (colour, shape, fill, linetype) or with facets.
  18 +
  19 +p <- ggplot(mtcars, aes(wt, mpg))
  20 +# A basic scatter plot
  21 +p + geom_point(size = 4)
  22 +# The colour aesthetic
  23 +p + geom_point(aes(colour = factor(cyl)), size = 4)
  24 +# Or you can use shape to distinguish the data
  25 +p + geom_point(aes(shape = factor(cyl)), size = 4)
  26 +
  27 +# Using fill
  28 +a <- ggplot(mtcars, aes(factor(cyl)))
  29 +a + geom_bar()
  30 +a + geom_bar(aes(fill = factor(cyl)))
  31 +a + geom_bar(aes(fill = factor(vs)))
  32 +
  33 +# Using linetypes
  34 +library(reshape2) # for melt
  35 +ec_scaled <- data.frame(date = economics$date, rescaler(economics[, -(1:2)], "range"))
  36 +ecm <- melt(ec_scaled, id = "date")
  37 +f <- ggplot(ecm, aes(date, value))
  38 +f + geom_line(aes(linetype = variable))
  39 +
  40 +# Using facets
  41 +k <- ggplot(diamonds, aes(carat, ..density..)) + geom_histogram(binwidth = 0.2)
  42 +k + facet_grid(. ~ cut)
  43 +
  44 +# There are three common cases where the default is not enough, and we
  45 +# will consider each one below. In the following examples, we will use a simple
  46 +# longitudinal dataset, Oxboys, from the nlme package. It records the heights
  47 +# (height) and centered ages (age) of 26 boys (Subject), measured on nine
  48 +# occasions (Occasion).
  49 +
  50 +# Multiple groups with one aesthetic
  51 +library(nlme)
  52 +h <- ggplot(Oxboys, aes(age, height))
  53 +# A single line tries to connect all the observations
  54 +h + geom_line()
  55 +# The group aesthetic maps a different line for each subject
  56 +h + geom_line(aes(group = Subject))
  57 +
  58 +# Different groups on different layers
  59 +h <- h + geom_line(aes(group = Subject))
  60 +# Using the group aesthetic with both geom_line() and geom_smooth()
  61 +# groups the data the same way for both layers
  62 +h + geom_smooth(aes(group = Subject), method = "lm", se = F)
  63 +# Changing the group aesthetic for the smoother layer
  64 +# fits a single line of best fit across all boys
  65 +h + geom_smooth(aes(group = 1), size = 2, method = "lm", se = F)
  66 +
  67 +# Overriding the default grouping
  68 +# The plot has a discrete scale but you want to draw lines that connect across
  69 +# groups. This is the strategy used in interaction plots, profile plots, and parallel
  70 +# coordinate plots, among others. For example, we draw boxplots of height at
  71 +# each measurement occasion
  72 +boysbox <- ggplot(Oxboys, aes(Occasion, height))
  73 +boysbox + geom_boxplot()
  74 +# There is no need to specify the group aesthetic here; the default grouping
  75 +# works because occasion is a discrete variable. To overlay individual trajectories
  76 +# we again need to override the default grouping for that layer with aes(group = Subject)
  77 +boysbox <- boysbox + geom_boxplot()
  78 +boysbox + geom_line(aes(group = Subject), colour = "blue")
  79 +
  80 +# Use the order aesthetic to change stacking order of bar charts
  81 +w <- ggplot(diamonds, aes(clarity, fill = cut))
  82 +w + geom_bar()
  83 +w + geom_bar(aes(order = desc(cut)))
  84 +
  85 +# Can also be used to change plot order of scatter plots
  86 +d <- ggplot(diamonds, aes(carat, price, colour = cut))
  87 +d + geom_point()
  88 +d + geom_point(aes(order = sample(seq_along(carat))))
  89 +}
  90 +
49 man/aes_position.Rd
... ... @@ -0,0 +1,49 @@
  1 +\name{aes_position}
  2 +\alias{aes_position}
  3 +\alias{x}
  4 +\alias{xend}
  5 +\alias{xmax}
  6 +\alias{xmin}
  7 +\alias{y}
  8 +\alias{yend}
  9 +\alias{ymax}
  10 +\alias{ymin}
  11 +\title{Position related aesthetics: x, y, xmin, xmax, ymin, ymax, xend, yend}
  12 +\description{
  13 + This page demonstrates the usage of a sub-group of
  14 + aesthetics; x, y, xmin, xmax, ymin, ymax, xend, and yend.
  15 +}
  16 +\examples{
  17 +# Generate data: means and standard errors of means for prices
  18 +# for each type of cut
  19 +dmod <- lm(price ~ cut, data = diamonds)
  20 +cuts <- data.frame(cut = unique(diamonds$cut), predict(dmod, data.frame(cut =
  21 +unique(diamonds$cut)), se = T)[c("fit", "se.fit")])
  22 +se <- ggplot(cuts, aes(x = cut, y = fit, ymin = fit - se.fit,
  23 +ymax = fit + se.fit, colour = cut))
  24 +se + geom_pointrange()
  25 +
  26 +# Boxplot with precomputed statistics
  27 +# generate sample data
  28 +abc <- adply(matrix(rnorm(100), ncol = 5), 2, quantile, c(0, .25, .5, .75, 1))
  29 +b <- ggplot(abc, aes(x = X1, ymin = "0\%", lower = "25\%", middle = "50\%", upper = "75\%", ymax = "100\%"))
  30 +b + geom_boxplot(stat = "identity")
  31 +
  32 +# Using annotate
  33 +p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
  34 +p + annotate("rect", xmin = 2, xmax = 3.5, ymin = 2, ymax = 25, fill = "dark grey", alpha = .5)
  35 +
  36 +# Geom_segment examples
  37 +library(grid)
  38 +p + geom_segment(aes(x = 2, y = 15, xend = 2, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
  39 +p + geom_segment(aes(x = 2, y = 15, xend = 3, yend = 15), arrow = arrow(length = unit(0.5, "cm")))
  40 +p + geom_segment(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
  41 +
  42 +# You can also use geom_segment to recreate plot(type = "h") :
  43 +counts <- as.data.frame(table(x = rpois(100, 5)))
  44 +counts$x <- as.numeric(as.character(counts$x))
  45 +with(counts, plot(x, Freq, type = "h", lwd = 10))
  46 +
  47 +qplot(x, Freq, data = counts, geom = "segment", yend = 0, xend = x, size = I(10))
  48 +}
  49 +
3  man/annotation_custom.Rd
@@ -45,5 +45,8 @@ base + annotation_custom(grob = tableGrob(head(iris[ ,1:3])),
45 45 # Inset plot
46 46 g <- ggplotGrob(qplot(1, 1) + opts(plot.background = theme_rect(col = "black")))
47 47 base + annotation_custom(grob = g, xmin = 1, xmax = 10, ymin = 8, ymax = 10)
  48 +# full panel
  49 +base + annotation_custom(grob = roundrectGrob(),
  50 + xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf)
48 51 }
49 52
4 man/geom_abline.Rd
@@ -75,6 +75,10 @@ p + geom_abline(data=coefs, aes(intercept=a, slope=b))
75 75 # It's actually a bit easier to do this with stat_smooth
76 76 p + geom_smooth(aes(group=cyl), method="lm")
77 77 p + geom_smooth(aes(group=cyl), method="lm", fullrange=TRUE)
  78 +
  79 +# With coordinate transforms
  80 +p + geom_abline(intercept = 37, slope = -5) + coord_flip()
  81 +p + geom_abline(intercept = 37, slope = -5) + coord_polar()
78 82 }
79 83 \seealso{
80 84 \code{\link{stat_smooth}} to add lines derived from the
15 man/geom_blank.Rd
@@ -30,7 +30,20 @@
30 30 ensuring common scales between different plots.
31 31 }
32 32 \examples{
33   -qplot(length, rating, data=movies, geom="blank")
  33 +qplot(length, rating, data = movies, geom = "blank")
34 34 # Nothing to see here!
  35 +
  36 +# Take the following scatter plot
  37 +a <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl) + geom_point()
  38 +# Add to that some lines with geom_abline()
  39 +df <- data.frame(a = rnorm(10, 25), b = rnorm(10, 0))
  40 +a + geom_abline(aes(intercept = a, slope = b), data = df)
  41 +# Suppose you then wanted to remove the geom_point layer
  42 +# If you just remove geom_point, you will get an error
  43 +b <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl)
  44 +b + geom_abline(aes(intercept = a, slope = b), data = df)
  45 +# Switching to geom_blank() gets the desired plot
  46 +c <- ggplot(mtcars, aes(x = wt, y = mpg), . ~ cyl) + geom_blank()
  47 +c + geom_abline(aes(intercept = a, slope = b), data = df)
35 48 }
36 49
2  man/geom_boxplot.Rd
@@ -57,6 +57,8 @@
57 57 the inter-quartile range, or distance between the first
58 58 and third quartiles. The lower whisker extends from the
59 59 hinge to the lowest value within 1.5 * IQR of the hinge.
  60 + Data beyond the end of the whiskers are outliers and
  61 + plotted as points (as specified by Tukey).
60 62
61 63 In a notched box plot, the notches extend \code{1.58 *
62 64 IQR / sqrt(n)}. This gives a roughly 95% confidence
18 man/geom_dotplot.Rd
@@ -62,7 +62,19 @@
62 62 for more details.}
63 63 }
64 64 \description{
65   - Dot plot
  65 + In a dot plot, the width of a dot corresponds to the bin
  66 + width (or maximum width, depending on the binning
  67 + algorithm), and dots are stacked, with each dot
  68 + representing one observation.
  69 +}
  70 +\details{
  71 + With dot-density binning, the bin positions are
  72 + determined by the data and \code{binwidth}, which is the
  73 + maximum width of each bin. See Wilkinson (1999) for
  74 + details on the dot-density binning algorithm.
  75 +
  76 + With histodot binning, the bins have fixed positions and
  77 + fixed widths, much like a histogram.
66 78 }
67 79 \examples{
68 80 ggplot(mtcars, aes(x = mpg)) + geom_dotplot()
@@ -106,4 +118,8 @@ ggplot(mtcars, aes(x = factor(vs), fill = factor(cyl), y = mpg)) +
106 118 ggplot(mtcars, aes(x = factor(am), y = mpg)) +
107 119 geom_dotplot(binaxis = "y", stackdir = "center", binpositions="all")
108 120 }
  121 +\references{
  122 + Wilkinson, L. (1999) Dot plots. The American
  123 + Statistician, 53(3), 276-281.
  124 +}
109 125
5 man/geom_hline.Rd
@@ -48,6 +48,11 @@ p + geom_hline(aes(yintercept=mpg))
48 48 p + geom_hline(yintercept=20)
49 49 p + geom_hline(yintercept=seq(10, 30, by=5))
50 50
  51 +# With coordinate transforms
  52 +p + geom_hline(aes(yintercept=mpg)) + coord_equal()
  53 +p + geom_hline(aes(yintercept=mpg)) + coord_flip()
  54 +p + geom_hline(aes(yintercept=mpg)) + coord_polar()
  55 +
51 56 # To display different lines in different facets, you need to
52 57 # create a data frame.
53 58 p <- qplot(mpg, wt, data=mtcars, facets = vs ~ am)
13 man/geom_segment.Rd
@@ -35,7 +35,7 @@
35 35 \examples{
36 36 library(grid) # needed for arrow function
37 37 p <- ggplot(seals, aes(x = long, y = lat))
38   -(p <- p + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow=arrow(length=unit(0.1,"cm"))))
  38 +(p <- p + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.1,"cm"))))
39 39
40 40 if (require("maps")) {
41 41
@@ -47,7 +47,7 @@ usamap <- rbind(usamap, NA, data.frame(map('state', xlim = xlim, ylim
47 47 = ylim, plot = FALSE)[c("x","y")]))
48 48 names(usamap) <- c("long", "lat")
49 49
50   -p + geom_path(data = usamap) + scale_x_continuous(limits=xlim)
  50 +p + geom_path(data = usamap) + scale_x_continuous(limits = xlim)
51 51 }
52 52
53 53 # You can also use geom_segment to recreate plot(type = "h") :
@@ -55,8 +55,15 @@ counts <- as.data.frame(table(x = rpois(100,5)))
55 55 counts$x <- as.numeric(as.character(counts$x))
56 56 with(counts, plot(x, Freq, type = "h", lwd = 10))
57 57
58   -qplot(x, Freq, data = counts, geom="segment",
  58 +qplot(x, Freq, data = counts, geom = "segment",
59 59 yend = 0, xend = x, size = I(10))
  60 +
  61 +# Adding line segments
  62 +library(grid) # needed for arrow function
  63 +b <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
  64 +b + geom_segment(aes(x = 2, y = 15, xend = 2, yend = 25))
  65 +b + geom_segment(aes(x = 2, y = 15, xend = 3, yend = 15))
  66 +b + geom_segment(aes(x = 5, y = 30, xend = 3.5, yend = 25), arrow = arrow(length = unit(0.5, "cm")))
60 67 }
61 68 \seealso{
62 69 \code{\link{geom_path}} and \code{\link{geom_line}} for
6 man/geom_smooth.Rd
@@ -53,4 +53,10 @@ grid$lcl <- err$fit - 1.96 * err$se.fit
53 53 qplot(wt, mpg, data=mtcars, colour=factor(cyl)) +
54 54 geom_smooth(aes(ymin = lcl, ymax = ucl), data=grid, stat="identity")
55 55 }
  56 +\seealso{
  57 + The default stat for this geom is
  58 + \code{\link{stat_smooth}} see that documentation for more
  59 + options to control the underlying statistical
  60 + transformation.
  61 +}
56 62
6 man/geom_vline.Rd
@@ -49,8 +49,10 @@ p + geom_vline(xintercept = 1:5)
49 49 p + geom_vline(xintercept = 1:5, colour="green", linetype = "longdash")
50 50 p + geom_vline(aes(xintercept = wt))
51 51
52   -last_plot() + coord_equal()
53   -last_plot() + coord_flip()
  52 +# With coordinate transforms
  53 +p + geom_vline(aes(xintercept = wt)) + coord_equal()
  54 +p + geom_vline(aes(xintercept = wt)) + coord_flip()
  55 +p + geom_vline(aes(xintercept = wt)) + coord_polar()
54 56
55 57 p2 <- p + aes(colour = factor(cyl))
56 58 p2 + geom_vline(xintercept = 15)
12 man/position_jitter.Rd
@@ -15,17 +15,21 @@
15 15 Jitter points to avoid overplotting.
16 16 }
17 17 \examples{
18   -qplot(am, vs, data=mtcars)
  18 +qplot(am, vs, data = mtcars)
19 19
20 20 # Default amount of jittering will generally be too much for
21 21 # small datasets:
22   -qplot(am, vs, data=mtcars, position="jitter")
  22 +qplot(am, vs, data = mtcars, position = "jitter")
23 23 # Control the amount as follows
24   -qplot(am, vs, data=mtcars, position=position_jitter(w=0.1, h=0.1))
  24 +qplot(am, vs, data = mtcars, position = position_jitter(w = 0.1, h = 0.1))
  25 +
  26 +# With ggplot
  27 +ggplot(mtcars, aes(x = am, y = vs)) + geom_point(position = "jitter")
  28 +ggplot(mtcars, aes(x = am, y = vs)) + geom_point(position = position_jitter(w = 0.1, h = 0.1))
25 29
26 30 # The default works better for large datasets, where it will
27 31 # take up as much space as a boxplot or a bar
28   -qplot(cut, price, data=diamonds, geom=c("boxplot", "jitter"))
  32 +qplot(cut, price, data = diamonds, geom = c("boxplot", "jitter"))
29 33 }
30 34 \seealso{
31 35 Other position adjustments: \code{\link{position_dodge}},
3  man/stat_bin2d.Rd
@@ -7,7 +7,8 @@
7 7 }
8 8 \arguments{
9 9 \item{bins}{numeric vector giving number of bins in both
10   - vertical and horizontal directions.}
  10 + vertical and horizontal directions. Set to 30 by
  11 + default.}
11 12
12 13 \item{drop}{if \code{TRUE} removes all cells with 0
13 14 counts.}
2  man/stat_binhex.Rd
@@ -7,7 +7,7 @@
7 7 }
8 8 \arguments{
9 9 \item{bins}{numeric vector specifying number of bins in
10   - both x and y directions.}
  10 + both x and y directions. Set to 30 by default.}
11 11
12 12 \item{na.rm}{If \code{FALSE} (the default), removes
13 13 missing values with a warning. If \code{TRUE} silently
25 man/stat_function.Rd
@@ -42,8 +42,31 @@
42 42 }
43 43 \examples{
44 44 x <- rnorm(100)
45   -base <- qplot(x, geom="density")
  45 +base <- qplot(x, geom = "density")
46 46 base + stat_function(fun = dnorm, colour = "red")
47 47 base + stat_function(fun = dnorm, colour = "red", arg = list(mean = 3))
  48 +
  49 +# Plot functions without data
  50 +# Examples adapted from Kohske Takahashi
  51 +
  52 +# Specify range of x-axis
  53 +qplot(c(0, 2), stat = "function", fun = exp, geom = "line")
  54 +ggplot(data.frame(x = c(0, 2)), aes(x)) + stat_function(fun = exp)
  55 +# Plot a normal curve
  56 +ggplot(data.frame(x = c(-5, 5)), aes(x)) + stat_function(fun = dnorm)
  57 +# With qplot
  58 +qplot(c(-5, 5), stat = "function", fun = dnorm, geom = "line")
  59 +# Or
  60 +qplot(c(-5, 5), geom = "blank") + stat_function(fun = dnorm)
  61 +# To specify a different mean or sd, use the args parameter to supply new values
  62 +ggplot(data.frame(x = c(-5, 5)), aes(x)) + stat_function(fun = dnorm, args = list(mean = 2, sd = .5))
  63 +
  64 +# Two functions on the same plot
  65 +f <- ggplot(data.frame(x = c(0, 10)), aes(x))
  66 +f + stat_function(fun = sin, colour = "red") + stat_function(fun = cos, colour = "blue")
  67 +
  68 +# Using a custom function
  69 +test <- function(x) {x ^ 2 + x + 20}
  70 +f + stat_function(fun = test)
48 71 }
49 72
5 man/stat_smooth.Rd
@@ -9,7 +9,10 @@
9 9 }
10 10 \arguments{
11 11 \item{method}{smoothing method (function) to use, eg. lm,
12   - glm, gam, loess, rlm}
  12 + glm, gam, loess, rlm. For datasets with n < 1000 default
  13 + is \code{\link{loess}}. For datasets with 1000 or more
  14 + observations defaults to gam, see \code{\link[mgcv]{gam}}
  15 + for more details.}
13 16
14 17 \item{formula}{formula to use in smoothing function, eg.
15 18 \code{y ~ x}, \code{y ~ poly(x, 2)}, \code{y ~ log(x)}}
14 man/stat_sum.Rd
@@ -35,14 +35,24 @@
35 35 }
36 36 \examples{
37 37 d <- ggplot(diamonds, aes(x = cut, y = clarity))
38   -# Need to control which group proportion calculated over
39   -# Overall proportion
  38 +# By default, all categorical variables in the plot form grouping
  39 +# variables, and the default behavior in stat_sum is to show the
  40 +# proportion. Specifying stat_sum with no group identifier leads to
  41 +# a plot which is not meaningful:
  42 +d + stat_sum()
  43 +# To correct this problem and achieve a more desirable plot, we need
  44 +# to specify which group the proportion is to be calculated over.
  45 +# There are several ways to do this:
  46 +
  47 +# by overall proportion
40 48 d + stat_sum(aes(group = 1))
41 49 d + stat_sum(aes(group = 1)) + scale_size(range = c(3, 10))
42 50 d + stat_sum(aes(group = 1)) + scale_area(range = c(3, 10))
  51 +
43 52 # by cut
44 53 d + stat_sum(aes(group = cut))
45 54 d + stat_sum(aes(group = cut, colour = cut))
  55 +
46 56 # by clarity
47 57 d + stat_sum(aes(group = clarity))
48 58 d + stat_sum(aes(group = clarity, colour = cut))
142 man/translate_qplot_base.Rd
... ... @@ -0,0 +1,142 @@
  1 +\name{translate_qplot_base}
  2 +\alias{translate_qplot_base}
  3 +\title{Translating between qplot and base graphics}
  4 +\description{
  5 + There are two types of graphics functions in base
  6 + graphics, those that draw complete graphics and those
  7 + that add to existing graphics.
  8 +}
  9 +\details{
  10 + qplot() has been designed to mimic plot(), and can do the
  11 + job of all other high-level plotting commands. There are
  12 + only two graph types from base graphics that cannot be
  13 + replicated with ggplot2: filled.contour() and persp()
  14 +}
  15 +\examples{
  16 +# High-level plotting commands
  17 +
  18 +plot(x, y); dotchart(x, y); stripchart(x, y)
  19 +qplot(x, y)
  20 +
  21 +plot(x, y, type = "l")
  22 +qplot(x, y, geom = "line")
  23 +
  24 +plot(x, y, type = "s")
  25 +qplot(x, y, geom = "step")
  26 +
  27 +plot(x, y, type = "b")
  28 +qplot(x, y, geom = c("point", "line"))
  29 +
  30 +boxplot(x, y)
  31 +qplot(x, y, geom = "boxplot")
  32 +
  33 +hist(x)
  34 +qplot(x, geom = "histogram")
  35 +
  36 +cdplot(x, y)
  37 +qplot(x, fill = y, geom = "density", position = "fill")
  38 +
  39 +coplot(y ~ x | a + b)
  40 +qplot(x, y, facets = a ~ b)
  41 +
  42 +# Many of the geoms are parameterised differently than base graphics. For
  43 +# example, hist() is parameterised in terms of the number of bins, while
  44 +# geom_histogram() is parameterised in terms of the width of each bin.
  45 +hist(x, bins = 100)
  46 +qplot(x, geom = "histogram", binwidth = 1)
  47 +
  48 +# qplot() often requires data in a slightly different format to the base
  49 +# graphics functions. For example, the bar geom works with untabulated data,
  50 +# not tabulated data like barplot(); the tile and contour geoms expect data
  51 +# in a data frame, not a matrix like image() and contour().
  52 +barplot(table(x))
  53 +qplot(x, geom = "bar")
  54 +
  55 +barplot(x)
  56 +qplot(names(x), x, geom = "bar", stat = "identity")
  57 +
  58 +image(x)
  59 +qplot(X1, X2, data = melt(x), geom = "tile", fill = value)
  60 +
  61 +contour(x)
  62 +qplot(X1, X2, data = melt(x), geom = "contour", fill = value)
  63 +
  64 +# Generally, the base graphics functions work with individual vectors, not
  65 +# data frames like ggplot2. qplot() will try to construct a data frame if one
  66 +# is not specified, but it is not always possible. If you get strange errors,
  67 +# you may need to create the data frame yourself.
  68 +with(df, plot(x, y))
  69 +qplot(x, y, data = df)
  70 +
  71 +# By default, qplot() maps values to aesthetics with a scale. To override
  72 +# this behaviour and set aesthetics, overriding the defaults, you need to use I().
  73 +plot(x, y, col = "red", cex = 1)
  74 +qplot(x, y, colour = I("red"), size = I(1))
  75 +
  76 +# Low-level drawing
  77 +
  78 +# The low-level drawing functions which add to an existing plot are equivalent
  79 +# to adding a new layer in ggplot2.
  80 +
  81 +# Base function ggplot2 layer
  82 +# curve() geom_curve()
  83 +# hline() geom_hline()
  84 +# lines() geom_line()
  85 +# points() geom_point()
  86 +# polygon() geom_polygon()
  87 +# rect() geom_rect()
  88 +# rug() geom_rug()
  89 +# segments() geom_segment()
  90 +# text() geom_text()
  91 +# vline() geom_vline()
  92 +# abline(lm(y ~ x)) geom_smooth(method = "lm")
  93 +# lines(density(x)) geom_density()
  94 +# lines(loess(x, y)) geom_smooth()
  95 +
  96 +plot(x, y)
  97 +lines(x, y)
  98 +
  99 +qplot(x, y) + geom_line()
  100 +
  101 +# Or, building up piece-meal
  102 +qplot(x, y)
  103 +last_plot() + geom_line()
  104 +
  105 +# Legends, axes and grid lines
  106 +
  107 +# In ggplot2, the appearance of legends and axes is controlled by the scales.
  108 +# Axes are produced by the x and y scales, while all other scales produce legends.
  109 +# See ?opts for help changing the appearance of axes and legends.
  110 +# The appearance of grid lines is controlled by the grid.major and grid.minor
  111 +# theme options, and their position by the breaks of the x and y scales.
  112 +
  113 +# Colour palettes
  114 +
  115 +# Instead of global colour palettes, ggplot2 has scales for individual plots. Much
  116 +# of the time you can rely on the default colour scale (which has somewhat better
  117 +# perceptual properties), but if you want to reuse an existing colour palette, you
  118 +# can use scale_colour_manual(). You will need to make sure that the colour
  119 +# is a factor for this to work.
  120 +
  121 +palette(rainbow(5))
  122 +plot(1:5, 1:5, col = 1:5, pch = 19, cex = 4)
  123 +
  124 +qplot(1:5, 1:5, col = factor(1:5), size = I(4))
  125 +last_plot() + scale_colour_manual(values = rainbow(5))
  126 +
  127 +# In ggplot2, you can also use palettes with continuous values, with intermediate
  128 +# values being linearly interpolated.
  129 +
  130 +qplot(0:100, 0:100, col = 0:100, size = I(4)) + scale_colour_gradientn(colours = rainbow(7))
  131 +last_plot() + scale_colour_gradientn(colours = terrain.colors(7))
  132 +
  133 +# Graphical parameters
  134 +
  135 +# The majority of par settings have some analogue within the theme system, or
  136 +# in the defaults of the geoms and scales. The appearance plot border drawn
  137 +# by box() can be controlled in a similar way by the panel.background and
  138 +# plot.background theme elements. Instead of using title(), the plot title is
  139 +# set with the title option. See ?opts for more theme elements.
  140 +last_plot() + opts(title = "My Plot Title")
  141 +}
  142 +
84 man/translate_qplot_ggplot.Rd
... ... @@ -0,0 +1,84 @@
  1 +\name{translate_qplot_ggplot}
  2 +\alias{translate_qplot_ggplot}
  3 +\title{Translating between qplot and ggplot}
  4 +\description{
  5 + Within ggplot2, there are two basic methods to create
  6 + plots, with qplot() and ggplot(). qplot() is designed
  7 + primarily for interactive use: it makes a number of
  8 + assumptions that speed most cases, but when designing
  9 + multilayered plots with different data sources it can get
  10 + in the way. This section describes what those defaults
  11 + are, and how they map to the fuller ggplot() syntax.
  12 +}
  13 +\examples{
  14 +# By default, qplot() assumes that you want a scatterplot,
  15 +# i.e., you want to use geom_point()
  16 +qplot(x, y, data = data)
  17 +ggplot(data, aes(x, y)) + geom_point()
  18 +
  19 +# Using Aesthetics
  20 +
  21 +# If you map additional aesthetics, these will be added to the defaults. With
  22 +# qplot() there is no way to use different aesthetic mappings (or data) in
  23 +# different layers
  24 +qplot(x, y, data = data, shape = shape, colour = colour)
  25 +ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
  26 +
  27 +# Aesthetic parameters in qplot() always try to map the aesthetic to a
  28 +# variable. If the argument is not a variable but a value, effectively a new column
  29 +# is added to the original dataset with that value. To set an aesthetic to a
  30 +# value and override the default appearance, you surround the value with I() in
  31 +# qplot(), or pass it as a parameter to the layer.
  32 +qplot(x, y, data = data, colour = I("red"))
  33 +ggplot(data, aes(x, y)) + geom_point(colour = "red")
  34 +
  35 +# Changing the geom parameter changes the geom added to the plot
  36 +qplot(x, y, data = data, geom = "line")
  37 +ggplot(data, aes(x, y)) + geom_line()
  38 +
  39 +# Not all geoms require both x and y, e.g., geom_bar() and geom_histogram().
  40 +# For these two geoms, if the y aesthetic is not supplied, both qplot and
  41 +# ggplot commands default to "count" on the y-axis
  42 +ggplot(data, aes(x)) + geom_bar()
  43 +qplot(x, data = data, geom = "bar")
  44 +
  45 +# If a vector of multiple geom names is supplied to the geom argument, each
  46 +# geom will be added in turn
  47 +qplot(x, y, data = data, geom = c("point", "smooth"))
  48 +ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
  49 +
  50 +# Unlike the rest of ggplot2, stats and geoms are independent
  51 +qplot(x, y, data = data, stat = "bin")
  52 +ggplot(data, aes(x, y)) + geom_point(stat = "bin")
  53 +
  54 +# Any layer parameters will be passed on to all layers. Most layers will ignore
  55 +# parameters that they don't need
  56 +qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
  57 +ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")
  58 +
  59 +# Scales and axes
  60 +
  61 +# You can control basic properties of the x and y scales with the xlim, ylim,
  62 +# xlab and ylab arguments
  63 +qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label")
  64 +ggplot(data, aes(x, y)) + geom_point() +
  65 +scale_x_continuous("my label", limits = c(1, 5))
  66 +
  67 +qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
  68 +ggplot(data, aes(x, y)) + geom_point() +
  69 +scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
  70 +
  71 +# Like plot(), qplot() has a convenient way of log transforming the axes.
  72 +qplot(x, y, data = data, log = "xy")
  73 +ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()
  74 +# There are many other possible transformations, but not all are
  75 +# accessible from within qplot(), see ?scale_continuous for more
  76 +
  77 +# Plot options
  78 +
  79 +# qplot() recognises the same options as plot does, and converts them to their
  80 +# ggplot2 equivalents. See ?opts for more on ggplot options
  81 +qplot(x, y, data = data, main="title", asp = 1)
  82 +ggplot(data, aes(x, y)) + geom_point() + opts(title = "title", aspect.ratio = 1)
  83 +}
  84 +
58 man/translate_qplot_gpl.Rd
... ... @@ -0,0 +1,58 @@
  1 +\name{translate_qplot_gpl}
  2 +\alias{translate_qplot_gpl}
  3 +\title{Translating between qplot and Graphics Production Library (GPL)}
  4 +\description{
  5 + The Grammar of Graphics uses two specifications. A
  6 + concise format is used to caption figures, and a more
  7 + detailed xml format stored on disk.
  8 +}
  9 +\examples{
  10 +# The following example of the concise format is adapted from Figure 1.5,
  11 +# page 13, of Leland Wilkinson's "The Grammar of Graphics."
  12 +# Springer, 2nd edition, 2005.
  13 +
  14 +DATA: source("demographics")
  15 +DATA: longitude, latitude = map(source("World"))
  16 +TRANS: bd = max(birth - death, 0)
  17 +COORD: project.mercator()
  18 +ELEMENT: point(position(lon * lat), size(bd), color(color.red))
  19 +ELEMENT: polygon(position(longitude * latitude))
  20 +
  21 +# This is relatively simple to adapt to the syntax of ggplot2:
  22 +
  23 +# ggplot() is used to specify the default data and default aesthetic mappings.
  24 +# Data is provided as standard R data.frames existing in the global environment;
  25 +# it does not need to be explicitly loaded. We also use a slightly
  26 +# different world dataset, with columns lat and long. This lets us use the
  27 +# same aesthetic mappings for both datasets. Layers can override the default
  28 +# data and aesthetic mappings provided by the plot.
  29 +
  30 +# We replace TRANS with an explicit transformation by R code.
  31 +
  32 +# ELEMENTs are replaced with layers, which explicitly specify the data
  33 +# source. Each geom has a default statistic which is used to transform the
  34 +# data prior to plotting. For the geoms in this example, the default statistic
  35 +# is the identity function. Fixed aesthetics (the colour red in this example)
  36 +# are supplied as additional arguments to the layer, rather than as special
  37 +# constants.
  38 +
  39 +# The SCALE component has been omitted from this example (so that the
  40 +# defaults are used). In both the ggplot2 and GoG examples, scales are
  41 +# defined by default. In ggplot you can override the defaults by adding a
  42 +# scale object, e.g., scale colour or scale size.
  43 +
  44 +# COORD uses a slightly different format. In general, most of the components
  45 +# specifications in ggplot are slightly different to those in GoG, in order to
  46 +# be more familiar to R users.
  47 +
  48 +# Each component is added together with + to create the final plot.
  49 +
  50 +# Resulting ggplot2 code:
  51 +demographics <- transform(demographics, bd = pmax(birth - death, 0))
  52 +p <- ggplot(demographic, aes(lon, lat))
  53 +p <- p + geom_polyogon(data = world)
  54 +p <- p + geom_point(aes(size = bd), colour = "red")
  55 +p <- p + coord_map(projection = "mercator")
  56 +print(p)
  57 +}
  58 +
74 man/translate_qplot_lattice.Rd
... ... @@ -0,0 +1,74 @@
  1 +\name{translate_qplot_lattice}
  2 +\alias{translate_qplot_lattice}
  3 +\title{Translating between qplot and lattice}
  4 +\description{
  5 + The major difference between lattice and ggplot2 is that
  6 + lattice uses a formula based interface. ggplot2 does not
  7 + because the formula does not generalise well to more
  8 + complicated situations.
  9 +}
  10 +\examples{
  11 +xyplot(rating ~ year, data=movies)
  12 +qplot(year, rating, data=movies)
  13 +
  14 +xyplot(rating ~ year | Comedy + Action, data = movies)
  15 +qplot(year, rating, data = movies, facets = ~ Comedy + Action)
  16 +# Or maybe
  17 +qplot(year, rating, data = movies, facets = Comedy ~ Action)
  18 +
  19 +# While lattice has many different functions to produce different types of
  20 +# graphics (which are all basically equivalent to setting the panel argument),
  21 +# ggplot2 has qplot().
  22 +
  23 +stripplot(~ rating, data = movies, jitter.data = TRUE)
  24 +qplot(rating, 1, data = movies, geom = "jitter")
  25 +
  26 +histogram(~ rating, data = movies)
  27 +qplot(rating, data = movies, geom = "histogram")
  28 +
  29 +bwplot(Comedy ~ rating ,data = movies)
  30 +qplot(factor(Comedy), rating, data = movies, type = "boxplot")
  31 +
  32 +xyplot(wt ~ mpg, mtcars, type = c("p","smooth"))
  33 +qplot(mpg, wt, data = mtcars, geom = c("point","smooth"))
  34 +
  35 +xyplot(wt ~ mpg, mtcars, type = c("p","r"))
  36 +qplot(mpg, wt, data = mtcars, geom = c("point","smooth"), method = "lm")
  37 +
  38 +# The capabilities for scale manipulations are similar in both ggplot2 and
  39 +# lattice, although the syntax is a little different.
  40 +
  41 +xyplot(wt ~ mpg | cyl, mtcars, scales = list(y = list(relation = "free")))
  42 +qplot(mpg, wt, data = mtcars) + facet_wrap(~ cyl, scales = "free")
  43 +
  44 +xyplot(wt ~ mpg | cyl, mtcars, scales = list(log = 10))
  45 +qplot(mpg, wt, data = mtcars, log = "xy")
  46 +
  47 +xyplot(wt ~ mpg | cyl, mtcars, scales = list(log = 2))
  48 +qplot(mpg, wt, data = mtcars) + scale_x_log2() + scale_y_log2()
  49 +
  50 +xyplot(wt ~ mpg, mtcars, group = cyl, auto.key = TRUE)
  51 +# Map directly to an aesthetic like colour, size, or shape.
  52 +qplot(mpg, wt, data = mtcars, colour = cyl)
  53 +
  54 +xyplot(wt ~ mpg, mtcars, xlim = c(20,30))
  55 +# Works like lattice, except you can't specify a different limit
  56 +# for each panel/facet
  57 +qplot(mpg, wt, data = mtcars, xlim = c(20,30))
  58 +
  59 +# Both lattice and ggplot2 have similar options for controlling labels on the plot.
  60 +
  61 +xyplot(wt ~ mpg, mtcars, xlab = "Miles per gallon", ylab = "Weight",
  62 + main = "Weight-efficiency tradeoff")
  63 +qplot(mpg, wt, data = mtcars, xlab = "Miles per gallon", ylab = "Weight",
  64 + main = "Weight-efficiency tradeoff")
  65 +
  66 +xyplot(wt ~ mpg, mtcars, aspect = 1)
  67 +qplot(mpg, wt, data = mtcars, asp = 1)
  68 +
  69 +# par.settings() is equivalent to + opts() and trellis.options.set()
  70 +# and trellis.par.get() to theme_set() and theme_get().
  71 +# More complicated lattice formulas are equivalent to rearranging the data
  72 +# before using ggplot2.
  73 +}
  74 +

0 comments on commit f62b084

Please sign in to comment.
Something went wrong with that request. Please try again.