diff --git a/NAMESPACE b/NAMESPACE index 2f9d179e..ea5b1a5c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +S3method(tinyplot,data.frame) S3method(tinyplot,default) S3method(tinyplot,density) S3method(tinyplot,formula) diff --git a/R/tinyplot.data.frame.R b/R/tinyplot.data.frame.R new file mode 100644 index 00000000..d01365c4 --- /dev/null +++ b/R/tinyplot.data.frame.R @@ -0,0 +1,58 @@ +#' tinyplot Method for Plotting Data Frames +#' +#' @description Convenience interface for visualizing data.frame objects +#' with tinyplot. +#' +#' @details This is a convenience function for plotting data frames with +#' or without a formula. The case with the formula mainly facilitates +#' using `tinyplot()` in combination with pipes. The case without +#' formula provides a quick way of plotting the variables in data frames, +#' either only one variable or a pair of variables. In the future, the +#' latter might be extended to a pairs display for data frames with +#' more than two variables but this is not implemented, yet. See the +#' examples for illustrations. +#' +#' @param x an object of class `"data.frame"`. +#' @param formula a \code{\link[stats]{formula}} that is passed on to +#' \code{\link{tinyplot.formula}}. If `formula` is `NULL` a formula of +#' type `y ~ 1` or `y ~ x` is set up for 1- and 2-dimensional data frames, +#' respectively. For data frames with more than 2 variables the `facet = NULL` +#' case is not supported, yet, and currently leads to an error. +#' @param ... further arguments passed to `tinyplot`. +#' +#' @examples +#' tinytheme("clean2") +#' +#' ## using tinyplot() with data frames and pipes +#' cars |> tinyplot() +#' iris |> tinyplot(Sepal.Length ~ Petal.Width | Species) +#' +#' ## tinyplot(df) only works for data frames with 1 or 2 variables +#' ## in the future we might add a pairs-style display as follows +#' ## but this would require better handling of axes and their labels +#' par(mfrow = c(5, 5)) +#' for (i in names(iris)) for(j in names(iris)) tinyplot(iris[, unique(c(j, i)), drop = FALSE]) +#' +#' tinytheme() ## reset +#' +#' @export +tinyplot.data.frame = function (x, formula = NULL, ...) { + ## original call + cl = match.call() + + ## default formula + ## 1d: y ~ 1 + ## 2d: y ~ x + ## 3d: pairs? (not supported, yet) + if (is.null(formula)) { + if (ncol(x) > 2L) stop("'formula' is missing with no default for more than 2 columns") + f = names(x) + if (length(f) < 2L) f = c("1", f) + cl$formula = as.formula(paste(f[2L:1L], collapse = " ~ ")) + } + + ## evaluate updated call + names(cl)[names(cl) == "x"] = "data" + cl[[1L]] = quote(tinyplot::tinyplot) + eval.parent(cl) +} diff --git a/man/tinyplot.data.frame.Rd b/man/tinyplot.data.frame.Rd new file mode 100644 index 00000000..9b0ecbdb --- /dev/null +++ b/man/tinyplot.data.frame.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/tinyplot.data.frame.R +\name{tinyplot.data.frame} +\alias{tinyplot.data.frame} +\title{tinyplot Method for Plotting Data Frames} +\usage{ +\method{tinyplot}{data.frame}(x, formula = NULL, ...) +} +\arguments{ +\item{x}{an object of class \code{"data.frame"}.} + +\item{formula}{a \code{\link[stats]{formula}} that is passed on to +\code{\link{tinyplot.formula}}. If \code{formula} is \code{NULL} a formula of +type \code{y ~ 1} or \code{y ~ x} is set up for 1- and 2-dimensional data frames, +respectively. For data frames with more than 2 variables the \code{facet = NULL} +case is not supported, yet, and currently leads to an error.} + +\item{...}{further arguments passed to \code{tinyplot}.} +} +\description{ +Convenience interface for visualizing data.frame objects +with tinyplot. +} +\details{ +This is a convenience function for plotting data frames with +or without a formula. The case with the formula mainly facilitates +using \code{tinyplot()} in combination with pipes. The case without +formula provides a quick way of plotting the variables in data frames, +either only one variable or a pair of variables. In the future, the +latter might be extended to a pairs display for data frames with +more than two variables but this is not implemented, yet. See the +examples for illustrations. +} +\examples{ +tinytheme("clean2") + +## using tinyplot() with data frames and pipes +cars |> tinyplot() +iris |> tinyplot(Sepal.Length ~ Petal.Width | Species) + +## tinyplot(df) only works for data frames with 1 or 2 variables +## in the future we might add a pairs-style display as follows +## but this would require better handling of axes and their labels +par(mfrow = c(5, 5)) +for (i in names(iris)) for(j in names(iris)) tinyplot(iris[, unique(c(j, i)), drop = FALSE]) + +tinytheme() ## reset + +}