Skip to content

Commit 1c617e5

Browse files
Swap order of data and formula arguments
- Resolves #1
1 parent 66bde54 commit 1c617e5

6 files changed

Lines changed: 154 additions & 139 deletions

File tree

R/plotluck.R

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1855,10 +1855,10 @@ gplt.blank <- function(text=NULL, ...) {
18551855
#'
18561856
#' data(iris)
18571857
#' # default with violin plot
1858-
#' plotluck(Petal.Length~Species, iris)
1858+
#' plotluck(iris, Petal.Length~Species)
18591859
#'
18601860
#' # use box-and-whiskers plot instead
1861-
#' plotluck(Petal.Length~Species, iris, opts=plotluck.options(geom='box'))
1861+
#' plotluck(iris, Petal.Length~Species, opts=plotluck.options(geom='box'))
18621862
#'
18631863
#' @export
18641864
plotluck.options <- function(opts,...) {
@@ -2043,6 +2043,7 @@ info.threshold <- function(cond, msg, threshold, ...) {
20432043
#' axis scaling, ordering and pruning of factor levels, and overlaying smoothing
20442044
#' curves or median lines.
20452045
#'
2046+
#' @param data a data frame.
20462047
#' @param formula an object of class \code{\link[stats]{formula}}: a symbolic description
20472048
#' of the relationship of up to three variables.
20482049
#' \tabular{lll}{
@@ -2062,7 +2063,6 @@ info.threshold <- function(cond, msg, threshold, ...) {
20622063
#' \code{.~x}\tab Plot each variable in the data frame against \code{x}\cr
20632064
#' \code{.~.}\tab Plot each variable in the data frame against each other.\cr}
20642065
#' See also section "Generating multiple plots at once" below.
2065-
#' @param data a data frame.
20662066
#' @param weights observation weights or frequencies (optional).
20672067
#' @param opts a named list of options (optional); See also \code{\link{plotluck.options}}.
20682068
#' @param ... additional parameters to be passed to the respective ggplot2 geom objects.
@@ -2254,44 +2254,44 @@ info.threshold <- function(cond, msg, threshold, ...) {
22542254
#' @examples
22552255
#' # Single-variable density
22562256
#' data(diamonds, package='ggplot2')
2257-
#' plotluck(price~1, diamonds)
2257+
#' plotluck(diamonds, price~1)
22582258
#' invisible(readline(prompt="Press [enter] to continue"))
22592259
#'
22602260
#' # Violin plot
22612261
#' data(iris)
2262-
#' plotluck(Species~Petal.Length,iris)
2262+
#' plotluck(iris, Species~Petal.Length)
22632263
#' invisible(readline(prompt="Press [enter] to continue"))
22642264
#'
22652265
#' # Scatter plot
22662266
#' data(mpg, package='ggplot2')
2267-
#' plotluck(cty~model, data=mpg)
2267+
#' plotluck(mpg, cty~model)
22682268
#' invisible(readline(prompt="Press [enter] to continue"))
22692269
#'
22702270
#' # Spine plot
22712271
#' data(Titanic)
2272-
#' plotluck(Survived~Class+Sex, weights=Freq, as.data.frame(Titanic))
2272+
#' plotluck(as.data.frame(Titanic), Survived~Class+Sex, weights=Freq)
22732273
#' invisible(readline(prompt="Press [enter] to continue"))
22742274
#'
22752275
#' # Facetting
22762276
#' data(msleep, package='ggplot2')
2277-
#' plotluck(sleep_total~bodywt|vore, msleep)
2277+
#' plotluck(msleep, sleep_total~bodywt|vore)
22782278
#' invisible(readline(prompt="Press [enter] to continue"))
22792279
#'
22802280
#' # Heat map
2281-
#' plotluck(price~cut+color, diamonds)
2281+
#' plotluck(diamonds, price~cut+color)
22822282
#'
22832283
#'\donttest{
22842284
#' # Multi plots
22852285
#
22862286
#' # All 1D distributions
2287-
#' plotluck(.~1, iris)
2287+
#' plotluck(iris, .~1)
22882288
#'
22892289
#' # 2D dependencies with one fixed variable on vertical axis
2290-
#' plotluck(Species~., iris)
2290+
#' plotluck(iris, Species~.)
22912291
#'}
22922292
#' # See also tests/testthat/test_plotluck.R for more examples!
22932293
#'
2294-
plotluck <- function(formula, data, weights,
2294+
plotluck <- function(data, formula, weights,
22952295
opts=plotluck.options(),
22962296
...) {
22972297
parsed <- parse.formula(formula)
@@ -2832,7 +2832,7 @@ add.conditional.layer <- function(p, data, response, indep, cond, type.plot, opt
28322832
# add.facet.wrap(p, data, cond[1], preferred.order, opts)
28332833
# }
28342834
# } else {
2835-
# example: plotluck(price~1|cut+clarity, diamonds)
2835+
# example: plotluck(diamonds, price~1|cut+clarity)
28362836
p <- redundant.factor.color(p, data, response, indep, type.plot, opts)
28372837
add.facet.grid(p, data, cond)
28382838
#}
@@ -3127,10 +3127,10 @@ plotluck.multi <- function(response, indep, data, w='NULL',
31273127

31283128
if (w != 'NULL')
31293129
{
3130-
call.strs <- sprintf('plotluck(%s~%s, data, w=%s, opts=plotluck.options(opts,%s), ...)%s%s',
3130+
call.strs <- sprintf('plotluck(data, %s~%s, w=%s, opts=plotluck.options(opts,%s), ...)%s%s',
31313131
combi$y, combi$x, w, combi$opts, combi$labs, theme.multi)
31323132
} else {
3133-
call.strs <- sprintf('plotluck(%s~%s, data, opts=plotluck.options(opts,%s), ...)%s%s',
3133+
call.strs <- sprintf('plotluck(data, %s~%s, opts=plotluck.options(opts,%s), ...)%s%s',
31343134
combi$y, combi$x, combi$opts, combi$labs, theme.multi)
31353135
}
31363136

@@ -3238,26 +3238,30 @@ sample.plotluck <- function(data, ...) {
32383238

32393239
# collect extra arguments
32403240
arg <- sapply(match.call()[seq(2,length(match.call()))], deparse)
3241-
s <- list()
3242-
for (i in seq(length(arg))) {
3243-
s[[length(s)+1]] <- sprintf('%s = %s', names(arg)[i], arg[i])
3241+
if (length(arg) == 1) {
3242+
s <- sprintf('plotluck(%s, %s)\n', as.character(match.call()[2]), form)
3243+
} else {
3244+
s <- list()
3245+
for (i in seq(2, length(arg))) {
3246+
s[[length(s)+1]] <- sprintf('%s = %s', names(arg)[i], arg[i])
3247+
}
3248+
s <- do.call(paste, c(s, sep=', '))
3249+
s <- sprintf('plotluck(%s, %s, %s)\n', as.character(match.call()[2]), form, s)
32443250
}
3245-
s <- do.call(paste,c(s,sep=', '))
3246-
s <- sprintf('plotluck(%s, %s)\n', form, s)
32473251
cat(s)
3248-
plotluck(as.formula(form), data, ...) + labs(title=s) + theme(plot.title=element_text(size=10))
3252+
plotluck(data, as.formula(form), ...) + labs(title=s) + theme(plot.title=element_text(size=10))
32493253
}
32503254

32513255
# same as sample.plotluck, but can be called with a list of options
32523256
# only used for testing/debugging!
32533257

32543258
# e.g.
32553259
# opts.list<-list()
3256-
# opts[[1]]<-plotluck.options(verbose=T)
3257-
# opts[[2]]<-plotluck.options(verbose=T,prefer.factors.vert=F)
3258-
# opts[[3]]<-plotluck.options(verbose=T,prefer.factors.vert=F,max.factor.levels.color=100)
3259-
# opts[[4]]<-plotluck.options(verbose=T,prefer.factors.vert=T,max.factor.levels.color=100,dedupe.scatter='jitter',min.points.hex=10000,min.points.density=1E20,min.points.violin=1E20)
3260-
# opts[[5]]<-plotluck.options(verbose=T,prefer.factors.vert=F,max.factor.levels=3)
3260+
# opts.list[[1]]<-plotluck.options(verbose=T)
3261+
# opts.list[[2]]<-plotluck.options(verbose=T,prefer.factors.vert=F)
3262+
# opts.list[[3]]<-plotluck.options(verbose=T,prefer.factors.vert=F,max.factor.levels.color=100)
3263+
# opts.list[[4]]<-plotluck.options(verbose=T,prefer.factors.vert=T,max.factor.levels.color=100,dedupe.scatter='jitter',min.points.hex=10000,min.points.density=1E20,min.points.violin=1E20)
3264+
# opts.list[[5]]<-plotluck.options(verbose=T,prefer.factors.vert=F,max.factor.levels=3)
32613265

32623266

32633267
sample.plotluck.testopts <- function(data, opts.list, ...) {
@@ -3315,15 +3319,20 @@ sample.plotluck.testopts <- function(data, opts.list, ...) {
33153319

33163320
# collect extra arguments
33173321
arg <- sapply(match.call()[seq(2,length(match.call()))], deparse)
3318-
s <- list()
3319-
for (i in seq(length(arg))) {
3320-
s[[length(s)+1]] <- sprintf('%s = %s', names(arg)[i], arg[i])
3322+
if (length(arg) <= 2) {
3323+
s <- sprintf('plotluck(%s, %s)', as.character(match.call()[2]), form)
3324+
} else {
3325+
s <- list()
3326+
for (i in seq(3, length(arg))) {
3327+
s[[length(s)+1]] <- sprintf('%s = %s', names(arg)[i], arg[i])
3328+
}
3329+
s <- do.call(paste, c(s, sep=', '))
3330+
s <- sprintf('plotluck(%s, %s, %s)', as.character(match.call()[2]), form, s)
33213331
}
3322-
s <- do.call(paste,c(s,sep=', '))
3323-
s <- sprintf('plotluck(%s, %s)\n', form, s)
3324-
cat(s)
3332+
33253333
for (i in seq(length(opts.list))) {
3326-
print(i)
3327-
print(plotluck(as.formula(form), data, opts=opts.list[[i]],...) + labs(title=s) + theme(plot.title=element_text(size=10)))
3334+
s.opt <- paste(s, sprintf(' [OPTION %d]', i), sep='')
3335+
cat(s.opt)
3336+
print(plotluck(data, as.formula(form), opts=opts.list[[i]],...) + labs(title=s.opt) + theme(plot.title=element_text(size=10)))
33283337
}
33293338
}

inst/doc/plotluck.html

Lines changed: 27 additions & 21 deletions
Large diffs are not rendered by default.

man/plotluck.Rd

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plotluck.options.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)