Skip to content

Commit

Permalink
type = NULL (#179)
Browse files Browse the repository at this point in the history
* switch to type = NULL

* run through type sanitizer

* redundant linux check

* oops

- need to keep alias for formula method before passing type sanitizer

* docs
  • Loading branch information
grantmcdermott authored Jul 31, 2024
1 parent b6c6ed2 commit faddcc2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 23 deletions.
16 changes: 11 additions & 5 deletions R/sanitize.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ sanitize_ribbon.alpha = function(ribbon.alpha) {




sanitize_type = function(type, x, y) {
# enforce boxplot type for y ~ factor(x)
if (!is.null(x) && is.factor(x) && !is.factor(y) && !identical(type, "boxplot")) {
type = "boxplot"
warning('The `type` argument was changed to "boxplot" automatically because `x` is a factor but not `y`.', call. = FALSE)
if (is.null(type)) {
# enforce boxplot type for y ~ factor(x)
if (!is.null(x) && is.factor(x) && !is.factor(y)) {
type = "boxplot"
} else {
type = "p"
}
} else if (type %in% c("hist", "histogram")) {
type = "histogram"
} else if (type %in% c("j", "jitter")) {
type = "jitter"
}
return(type)
}
48 changes: 35 additions & 13 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
#' should not be specified in the same call.
#' @param data a data.frame (or list) from which the variables in formula
#' should be taken. A matrix is converted to a data frame.
#' @param type character string giving the type of plot desired. Options are:
#' @param type character string giving the type of plot desired. If no argument
#' is provided, then the plot type will default to something sensible for the
#' type of `x` and `y` inputs (i.e., usually `"p"`). Options are:
#' - The same set of 1-character values supported by
#' \code{\link[graphics]{plot}}: `"p"` for points, `"l"` for lines, `"b"` for
#' both points and lines, `"c"` for empty points joined by lines, `"o"` for
Expand Down Expand Up @@ -470,7 +472,7 @@ tinyplot.default = function(
facet = NULL,
facet.args = NULL,
data = NULL,
type = "p",
type = NULL,
xlim = NULL,
ylim = NULL,
log = "",
Expand Down Expand Up @@ -504,15 +506,17 @@ tinyplot.default = function(
width = NULL,
height = NULL,
empty = FALSE,
...) {
...
) {

dots = list(...)

if (isTRUE(add)) legend = FALSE

# sanitize arguments
ribbon.alpha = sanitize_ribbon.alpha(ribbon.alpha)
type = sanitize_type(type, x, y)

if (isTRUE(add)) legend = FALSE


xlabs = NULL

# Write plot to output file or window with fixed dimensions
Expand Down Expand Up @@ -579,7 +583,7 @@ tinyplot.default = function(
y_dep = paste0("[", ymin_dep, ", ", ymax_dep, "]")
y = rep(NA, length(x))
if (is.null(ylim)) ylim = range(c(ymin, ymax))
} else if (!type %in% c("density", "hist", "histogram")) {
} else if (!type %in% c("density", "histogram")) {
y = x
x = seq_along(x)
xlab = "Index"
Expand All @@ -600,7 +604,7 @@ tinyplot.default = function(
return(do.call(tinyplot.density, args = fargs))
}

if (type %in% c("hist", "histogram")) {
if (type == "histogram") {
fargs = histogram_args(
x = x, by = by, facet = facet, facet_by = facet_by, dots = dots,
ylab = ylab, col = col, bg = bg, fill = fill, ribbon.alpha = ribbon.alpha)
Expand All @@ -616,7 +620,25 @@ tinyplot.default = function(
was_area_type = FALSE # flag to keep track for some legend adjustments below
}

if (type %in% c("j", "jitter")) {
if (type == "jitter") {
if (is.character(x)) x = as.factor(x)
if (is.character(y)) y = as.factor(y)
if (is.factor(x)) {
xlvls = levels(x)
xlabs = seq_along(xlvls)
names(xlabs) = xlvls
x = as.integer(x)
} else {
xlabs = NULL
}
if (is.factor(y)) {
ylvls = levels(y)
ylabs = seq_along(ylvls)
names(ylabs) = ylvls
y = as.integer(y)
} else {
ylabs = NULL
}
x = jitter(x)
y = jitter(y)
type = "p"
Expand Down Expand Up @@ -1059,7 +1081,7 @@ tinyplot.default = function(
if (isTRUE(axes)) {
if (isTRUE(frame.plot)) {
# if plot frame is true then print axes per normal...
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot") && !is.null(xlabs)) {
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) {
Axis(x, side = xside, at = xlabs, labels = names(xlabs))
} else {
Axis(x, side = xside)
Expand All @@ -1068,7 +1090,7 @@ tinyplot.default = function(
} else {
# ... else only print the "outside" axes.
if (ii %in% oxaxis) {
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot") && !is.null(xlabs)) {
if (type %in% c("pointrange", "errorbar", "ribbon", "boxplot", "p") && !is.null(xlabs)) {
Axis(x, side = xside, at = xlabs, labels = names(xlabs))
} else {
Axis(x, side = xside)
Expand Down Expand Up @@ -1301,7 +1323,7 @@ tinyplot.formula = function(
data = parent.frame(),
facet = NULL,
facet.args = NULL,
type = "p",
type = NULL,
xlim = NULL,
ylim = NULL,
# log = "",
Expand Down Expand Up @@ -1463,7 +1485,7 @@ tinyplot.formula = function(
}

## nice axis and legend labels
if (type %in% c("hist", "histogram")) {
if (!is.null(type) && type %in% c("hist", "histogram")) {
if (is.null(ylab)) ylab = "Frequency"
if (is.null(xlab)) xlab = names(mf)[x_loc]
} else if (no_y) {
Expand Down
4 changes: 2 additions & 2 deletions inst/tinytest/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ library(tinytest)
library(tinysnapshot)

# # Skip tests if not on Linux
ON_LINUX = Sys.info()["sysname"] == "Linux"
if (!ON_LINUX) exit_file("Linux snapshots")
# ON_LINUX = Sys.info()["sysname"] == "Linux"
# if (!ON_LINUX) exit_file("Linux snapshots")

options("tinysnapshot_os" = "Linux")
options("tinysnapshot_device" = "svglite")
Expand Down
8 changes: 5 additions & 3 deletions man/tinyplot.Rd

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

0 comments on commit faddcc2

Please sign in to comment.