1
1
# ' Quick plot
2
2
# '
3
- # ' \code{qplot} is the basic plotting function in the ggplot2 package,
4
- # ' designed to be familiar if you're used to \code{\link{plot}}
5
- # ' from the base package. It is a convenient wrapper for creating
6
- # ' a number of different types of plots using a consistent
3
+ # ' \code{qplot} is the basic plotting function in the ggplot2 package,
4
+ # ' designed to be familiar if you're used to \code{\link{plot}}
5
+ # ' from the base package. It is a convenient wrapper for creating
6
+ # ' a number of different types of plots using a consistent
7
7
# ' calling scheme. See \url{http://had.co.nz/ggplot2/book/qplot.pdf}
8
8
# ' for the chapter in the \code{ggplot2} book which describes the usage
9
9
# ' of \code{qplot} in detail.
10
- # '
10
+ # '
11
11
# ' @param x x values
12
12
# ' @param y y values
13
13
# ' @param ... other aesthetics passed for each layer
14
- # ' @param data data frame to use (optional). If not specified, will create
14
+ # ' @param data data frame to use (optional). If not specified, will create
15
15
# ' one, extracting vectors from the current environment.
16
16
# ' @param facets faceting formula to use. Picks \code{\link{facet_wrap}} or
17
17
# ' \code{\link{facet_grid}} depending on whether the formula is one sided
18
18
# ' or two-sided
19
19
# ' @param margins whether or not margins will be displayed
20
- # ' @param geom character vector specifying geom to use. Defaults to
21
- # ' "point" if x and y are specified, and "histogram" is on x is specified.
20
+ # ' @param geom character vector specifying geom to use. Defaults to
21
+ # ' "point" if x and y are specified, and "histogram" if only x is specified.
22
22
# ' @param stat character vector specifying statistics to use
23
23
# ' @param position character vector giving position adjustment to use
24
24
# ' @param xlim limits for x axis
28
28
# ' @param xlab character vector or expression for x axis label
29
29
# ' @param ylab character vector or expression for y axis label
30
30
# ' @param asp the y/x aspect ratio
31
- # ' @aliases qplot quickplot
32
- # ' @export qplot quickplot
31
+ # ' @aliases qplot quickplot
32
+ # ' @export qplot quickplot
33
33
# ' @examples
34
34
# ' \donttest{
35
35
# ' # Use data from data.frame
59
59
# ' a <- 1:10
60
60
# ' b <- a ^ 2
61
61
# ' qplot(a, b)
62
- # ' }
62
+ # ' }
63
63
# ' f()
64
- # '
64
+ # '
65
65
# ' # qplot will attempt to guess what geom you want depending on the input
66
66
# ' # both x and y supplied = scatterplot
67
67
# ' qplot(mpg, wt, data = mtcars)
68
68
# ' # just x supplied = histogram
69
69
# ' qplot(mpg, data = mtcars)
70
70
# ' # just y supplied = scatterplot, with x = seq_along(y)
71
71
# ' qplot(y = mpg, data = mtcars)
72
- # '
72
+ # '
73
73
# ' # Use different geoms
74
74
# ' qplot(mpg, wt, data = mtcars, geom="path")
75
75
# ' qplot(factor(cyl), wt, data = mtcars, geom=c("boxplot", "jitter"))
@@ -78,13 +78,13 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins=FALSE, geom = "
78
78
79
79
argnames <- names(as.list(match.call(expand.dots = FALSE )[- 1 ]))
80
80
arguments <- as.list(match.call()[- 1 ])
81
-
81
+
82
82
aesthetics <- compact(arguments [.all_aesthetics ])
83
83
aesthetics <- aesthetics [! is.constant(aesthetics )]
84
84
aes_names <- names(aesthetics )
85
85
aesthetics <- rename_aes(aesthetics )
86
86
class(aesthetics ) <- " uneval"
87
-
87
+
88
88
if (missing(data )) {
89
89
# If data not explicitly specified, will be pulled from workspace
90
90
data <- data.frame ()
@@ -115,46 +115,46 @@ qplot <- function(x, y = NULL, ..., data, facets = NULL, margins=FALSE, geom = "
115
115
116
116
env <- parent.frame()
117
117
p <- ggplot(data , aesthetics , environment = env )
118
-
118
+
119
119
if (is.null(facets )) {
120
120
p <- p + facet_null()
121
121
} else if (is.formula(facets ) && length(facets ) == 2 ) {
122
122
p <- p + facet_wrap(facets )
123
123
} else {
124
124
p <- p + facet_grid(facets = deparse(facets ), margins = margins )
125
125
}
126
-
126
+
127
127
if (! is.null(main )) p <- p + ggtitle(main )
128
128
129
129
# Add geoms/statistics
130
130
if (is.proto(position )) position <- list (position )
131
-
131
+
132
132
mapply(function (g , s , ps ) {
133
133
if (is.character(g )) g <- Geom $ find(g )
134
134
if (is.character(s )) s <- Stat $ find(s )
135
135
if (is.character(ps )) ps <- Position $ find(ps )
136
-
136
+
137
137
# Have to use non-standard evaluation because we can't evaluate ...
138
138
params <- arguments [setdiff(names(arguments ), c(aes_names , argnames ))]
139
139
# 1: mapply, 2: qplot, 3: caller of qplot
140
140
params <- lapply(params , eval , parent.frame(3 ))
141
-
141
+
142
142
p <<- p + layer(geom = g , stat = s , geom_params = params , stat_params = params , position = ps )
143
143
}, geom , stat , position )
144
-
144
+
145
145
logv <- function (var ) var %in% strsplit(log , " " )[[1 ]]
146
146
147
147
if (logv(" x" )) p <- p + scale_x_log10()
148
148
if (logv(" y" )) p <- p + scale_y_log10()
149
-
149
+
150
150
if (! is.na(asp )) p <- p + theme(aspect.ratio = asp )
151
-
151
+
152
152
if (! missing(xlab )) p <- p + xlab(xlab )
153
153
if (! missing(ylab )) p <- p + ylab(ylab )
154
-
154
+
155
155
if (! missing(xlim )) p <- p + xlim(xlim )
156
156
if (! missing(ylim )) p <- p + ylim(ylim )
157
-
157
+
158
158
p
159
159
}
160
160
quickplot <- qplot
0 commit comments