Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 83 lines (82 sloc) 3.802 kB
119b102 Add aes pages and translate pages
unknown authored
1 #' Translating between qplot and ggplot
1244716 @hadley Trim whitespace
authored
2 #'
119b102 Add aes pages and translate pages
unknown authored
3 #' Within ggplot2, there are two basic methods to create plots, with qplot()
4 #' and ggplot(). qplot() is designed primarily for interactive use: it makes
5 #' a number of assumptions that speed most cases, but when designing multilayered
6 #' plots with different data sources it can get in the way. This section
7 #' describes what those defaults are, and how they map to the fuller ggplot()
8 #' syntax.
9 #'
10 #' @name translate_qplot_ggplot
11 #' @examples
12 #'
1244716 @hadley Trim whitespace
authored
13 #' # By default, qplot() assumes that you want a scatterplot,
119b102 Add aes pages and translate pages
unknown authored
14 #' # i.e., you want to use geom_point()
5b1f9e9 @hadley Updates for R CMD check
authored
15 #' # qplot(x, y, data = data)
16 #' # ggplot(data, aes(x, y)) + geom_point()
119b102 Add aes pages and translate pages
unknown authored
17 #'
18 #' # Using Aesthetics
1244716 @hadley Trim whitespace
authored
19 #'
119b102 Add aes pages and translate pages
unknown authored
20 #' # If you map additional aesthetics, these will be added to the defaults. With
21 #' # qplot() there is no way to use different aesthetic mappings (or data) in
22 #' # different layers
5b1f9e9 @hadley Updates for R CMD check
authored
23 #' # qplot(x, y, data = data, shape = shape, colour = colour)
24 #' # ggplot(data, aes(x, y, shape = shape, colour = colour)) + geom_point()
1244716 @hadley Trim whitespace
authored
25 #' #
119b102 Add aes pages and translate pages
unknown authored
26 #' # Aesthetic parameters in qplot() always try to map the aesthetic to a
27 #' # variable. If the argument is not a variable but a value, effectively a new column
28 #' # is added to the original dataset with that value. To set an aesthetic to a
29 #' # value and override the default appearance, you surround the value with I() in
30 #' # qplot(), or pass it as a parameter to the layer.
5b1f9e9 @hadley Updates for R CMD check
authored
31 #' # qplot(x, y, data = data, colour = I("red"))
32 #' # ggplot(data, aes(x, y)) + geom_point(colour = "red")
1244716 @hadley Trim whitespace
authored
33 #'
119b102 Add aes pages and translate pages
unknown authored
34 #' # Changing the geom parameter changes the geom added to the plot
5b1f9e9 @hadley Updates for R CMD check
authored
35 #' # qplot(x, y, data = data, geom = "line")
36 #' # ggplot(data, aes(x, y)) + geom_line()
119b102 Add aes pages and translate pages
unknown authored
37 #'
38 #' # Not all geoms require both x and y, e.g., geom_bar() and geom_histogram().
1244716 @hadley Trim whitespace
authored
39 #' # For these two geoms, if the y aesthetic is not supplied, both qplot and
119b102 Add aes pages and translate pages
unknown authored
40 #' # ggplot commands default to "count" on the y-axis
5b1f9e9 @hadley Updates for R CMD check
authored
41 #' # ggplot(data, aes(x)) + geom_bar()
42 #' # qplot(x, data = data, geom = "bar")
1244716 @hadley Trim whitespace
authored
43 #'
119b102 Add aes pages and translate pages
unknown authored
44 #' # If a vector of multiple geom names is supplied to the geom argument, each
45 #' # geom will be added in turn
5b1f9e9 @hadley Updates for R CMD check
authored
46 #' # qplot(x, y, data = data, geom = c("point", "smooth"))
47 #' # ggplot(data, aes(x, y)) + geom_point() + geom_smooth()
1244716 @hadley Trim whitespace
authored
48 #'
119b102 Add aes pages and translate pages
unknown authored
49 #' # Unlike the rest of ggplot2, stats and geoms are independent
5b1f9e9 @hadley Updates for R CMD check
authored
50 #' # qplot(x, y, data = data, stat = "bin")
51 #' # ggplot(data, aes(x, y)) + geom_point(stat = "bin")
1244716 @hadley Trim whitespace
authored
52 #' #
119b102 Add aes pages and translate pages
unknown authored
53 #' # Any layer parameters will be passed on to all layers. Most layers will ignore
54 #' # parameters that they don't need
5b1f9e9 @hadley Updates for R CMD check
authored
55 #' # qplot(x, y, data = data, geom = c("point", "smooth"), method = "lm")
56 #' # ggplot(data, aes(x, y)) + geom_point(method = "lm") + geom_smooth(method = "lm")
1244716 @hadley Trim whitespace
authored
57 #'
119b102 Add aes pages and translate pages
unknown authored
58 #' # Scales and axes
59 #'
60 #' # You can control basic properties of the x and y scales with the xlim, ylim,
61 #' # xlab and ylab arguments
5b1f9e9 @hadley Updates for R CMD check
authored
62 #' # qplot(x, y, data = data, xlim = c(1, 5), xlab = "my label")
1244716 @hadley Trim whitespace
authored
63 #' # ggplot(data, aes(x, y)) + geom_point() +
5b1f9e9 @hadley Updates for R CMD check
authored
64 #' # scale_x_continuous("my label", limits = c(1, 5))
1244716 @hadley Trim whitespace
authored
65 #'
5b1f9e9 @hadley Updates for R CMD check
authored
66 #' # qplot(x, y, data = data, xlim = c(1, 5), ylim = c(10, 20))
67 #' # ggplot(data, aes(x, y)) + geom_point() +
68 #' # scale_x_continuous(limits = c(1, 5)) + scale_y_continuous(limits = c(10, 20))
1244716 @hadley Trim whitespace
authored
69 #'
119b102 Add aes pages and translate pages
unknown authored
70 #' # Like plot(), qplot() has a convenient way of log transforming the axes.
5b1f9e9 @hadley Updates for R CMD check
authored
71 #' # qplot(x, y, data = data, log = "xy")
72 #' # ggplot(data, aes(x, y)) + geom_point() + scale_x_log10() + scale_y_log10()
1244716 @hadley Trim whitespace
authored
73 #' # There are many other possible transformations, but not all are
119b102 Add aes pages and translate pages
unknown authored
74 #' # accessible from within qplot(), see ?scale_continuous for more
1244716 @hadley Trim whitespace
authored
75 #'
119b102 Add aes pages and translate pages
unknown authored
76 #' # Plot options
77 #'
78 #' # qplot() recognises the same options as plot does, and converts them to their
d877655 @wch Replace references to opts() with themes()
wch authored
79 #' # ggplot2 equivalents. See ?theme for more on ggplot options
5b1f9e9 @hadley Updates for R CMD check
authored
80 #' # qplot(x, y, data = data, main="title", asp = 1)
4f5b5f6 @wch Change theme(title=...) to labs(title=...)
wch authored
81 #' # ggplot(data, aes(x, y)) + geom_point() + labs(title = "title") + theme(aspect.ratio = 1)
c113780 @hadley Add trailing newlines
authored
82 NULL
Something went wrong with that request. Please try again.