Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
58 changes: 58 additions & 0 deletions R/tinyplot.data.frame.R
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about the n>2 case below.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it a little bit now. But depending on where we go with the pairs-esque display, further updates will probably be necessary.

#' 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)
Comment thread
zeileis marked this conversation as resolved.
Outdated
#'
#' ## 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])
Comment thread
zeileis marked this conversation as resolved.
Outdated
#'
#' tinytheme() ## reset
Comment thread
zeileis marked this conversation as resolved.
Outdated
#'
#' @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")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, I think we could do better than erroring out here. My own preference is that we either:

  1. Subset to the first 2 columns and display the resulting simple two-way plot (maybe with a message), or

  2. Subset to the first N (<=5?) columns and then internally implement the "manual" pairs-esque plot that you demonstrate in the Examples above (maybe with a message if any columns are dropped), or

  3. Don't subset at all and just try to implement the "manual" pairs-esque plot you demonstrate above (but risk poor viewability/zooming.)

P.S. I don't think we should try to mimic base plot(df)/pairs(df) exactly. Apart from the top + right axes variation being a pain, I think your example shows several improvements, especially w.r.t. to the histogram and way that factor/character variables are handled consistently (barplots + spineplots).

P.P.S. If we go with option 2 or 3, one other thing to consider is whether we support an (atomic) by argument without a formula. E.g., tinyplot(iris, by = "species").

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. I think we should just go for 3 and explain the limitations in the manual page.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have attempted to implement 3 now including some very simple setting of xlab, ylab, and main. It's not great but probably better than nothing. Further improvements would be welcome.

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)
}
49 changes: 49 additions & 0 deletions man/tinyplot.data.frame.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading