Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion R/by_aesthetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ by_pch = function(ngrps, type, pch = NULL) {

by_lty = function(ngrps, type, lty = NULL) {
# We only care about line types, otherwise return NULL
if (!type %in% c("l", "b", "o", "c", "h", "s", "S", "ribbon", "boxplot", "rect", "segments", "qq")) {
if (!type %in% c("l", "b", "o", "c", "h", "s", "S", "ribbon", "boxplot", "rect", "segments", "qq", "abline", "hline", "vline")) {
out = NULL

# special "by" convenience keyword
Expand Down
6 changes: 3 additions & 3 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#' precedence if both are specified together. Ignored if a two-sided formula
#' is passed to the main `facet` argument, since the layout is arranged in a
#' fixed grid.
#' - `free` a logical value indicating whether the axis limits (scales) for
#' each individual facet should adjust independently to match the range of
#' - `free` a logical value indicating whether the axis limits (scales) for
#' each individual facet should adjust independently to match the range of
#' the data within that facet. Default is `FALSE`. Separate free scaling of
#' the x- or y-axis (i.e., whilst holding the other axis fixed) is not
#' currently supported.
Expand Down Expand Up @@ -1101,7 +1101,7 @@ tinyplot.default = function(
mfgj = ii %% nfacet_cols
if (mfgj == 0) mfgj = nfacet_cols
par(mfg = c(mfgi, mfgj))

# For free facets, we need to reset par(usr) based extent of that
# particular facet... which we calculated and saved to the .fusr env var
# (list) back in draw_facet_window()
Expand Down
35 changes: 2 additions & 33 deletions R/type_abline.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#' Add straight lines to a plot
#'
#' @inheritParams graphics::abline
#' @inheritParams tinyplot
#' @examples
#' mod = lm(mpg ~ hp, data = mtcars)
#' y = mtcars$mpg
#' yhat = predict(mod)
#' tinyplot(y, yhat, xlim = c(0, 40), ylim = c(0, 40))
#' tinyplot_add(type = type_abline(a = 0, b = 1))
#' @export
type_abline = function(a = 0, b = 1, col = NULL, lty = NULL, lwd = NULL) {
type_abline = function(a = 0, b = 1) {
data_abline = function(datapoints, ...) {
if (nrow(datapoints) == 0) {
msg = "`type_abline() only works on existing plots with x and y data points."
Expand All @@ -35,37 +34,7 @@ type_abline = function(a = 0, b = 1, col = NULL, lty = NULL, lwd = NULL) {
stop(msg, call. = FALSE)
}

if (is.null(col)) {
col = icol
}
if (length(col) == 1) {
col = rep(col, nfacets)
} else if (length(col) != nfacets) {
msg = "Length of 'col' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lty)) {
lty = if (!is.null(ilty)) ilty else 1
}
if (length(lty) == 1) {
lty = rep(lty, nfacets)
} else if (length(lty) != nfacets) {
msg = "Length of 'lty' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lwd)) {
lwd = if (!is.null(ilwd)) ilwd else 1
}
if (length(lwd) == 1) {
lwd = rep(lwd, nfacets)
} else if (length(lwd) != nfacets) {
msg = "Length of 'lwd' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

abline(a = a[ifacet], b = b[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet])
abline(a = a[ifacet], b = b[ifacet], col = icol, lty = ilty, lwd = ilwd)
}
return(fun)
}
Expand Down
38 changes: 3 additions & 35 deletions R/type_hline.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#' Trace a horizontal line on the plot
#'
#' @param h y-value(s) for horizontal line(s). Numeric of length 1 or equal to the number of facets.
#' @inheritParams graphics::abline
#' @inheritParams tinyplot
#' @examples
#' tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars)
#' tinyplot_add(type = type_hline(h = 12, col = "pink", lty = 3, lwd = 3))
#' tinyplot_add(type = type_hline(h = 12), col = "pink", lty = 3, lwd = 3)
#' @export
type_hline = function(h = 0, col = NULL, lty = NULL, lwd = NULL) {
type_hline = function(h = 0) {
data_hline = function(datapoints, ...) {
if (nrow(datapoints) == 0) {
msg = "`type_hline() only works on existing plots with x and y data points."
Expand All @@ -26,37 +24,7 @@ type_hline = function(h = 0, col = NULL, lty = NULL, lwd = NULL) {
stop(msg, call. = FALSE)
}

if (is.null(col)) {
col = icol
}
if (length(col) == 1) {
col = rep(col, nfacets)
} else if (length(col) != nfacets) {
msg = "Length of 'col' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lty)) {
lty = if (!is.null(ilty)) ilty else 1
}
if (length(lty) == 1) {
lty = rep(lty, nfacets)
} else if (length(lty) != nfacets) {
msg = "Length of 'lty' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lwd)) {
lwd = if (!is.null(ilwd)) ilwd else 1
}
if (length(lwd) == 1) {
lwd = rep(lwd, nfacets)
} else if (length(lwd) != nfacets) {
msg = "Length of 'lwd' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

abline(h = h[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet])
abline(h = h[ifacet], col = icol, lty = ilty, lwd = ilwd)
}
return(fun)
}
Expand Down
40 changes: 4 additions & 36 deletions R/type_vline.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#' Trace a vertical line on the plot
#'
#' @param v x-value(s) for vertical line(s). Numeric of length 1 or equal to the number of facets.
#' @inheritParams tinyplot
#' @examples
#' tinyplot(mpg ~ hp, data = mtcars)
#' tinyplot_add(type = type_vline(150))
#'
#' # facet-specify location and colors
#' cols = c("black", "green", "orange")
#' tinyplot(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars, col = cols)
#' tinyplot_add(type = type_vline(
#' v = c(100, 150, 200), col = cols, lty = 3, lwd = 3
#' ))
#' tinyplot_add(type = type_vline(v = c(100, 150, 200)), lty = 3, lwd = 3)
#'
#' @export
type_vline = function(v = 0, col = "black", lty = 1, lwd = 1) {
type_vline = function(v = 0) {
assert_numeric(v)
data_vline = function(datapoints, ...) {
if (nrow(datapoints) == 0) {
Expand All @@ -33,37 +31,7 @@ type_vline = function(v = 0, col = "black", lty = 1, lwd = 1) {
stop(msg, call. = FALSE)
}

if (is.null(col)) {
col = icol
}
if (length(col) == 1) {
col = rep(col, nfacets)
} else if (length(col) != nfacets) {
msg = "Length of 'col' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lty)) {
lty = if (!is.null(ilty)) ilty else 1
}
if (length(lty) == 1) {
lty = rep(lty, nfacets)
} else if (length(lty) != nfacets) {
msg = "Length of 'lty' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

if (is.null(lwd)) {
lwd = if (!is.null(ilwd)) ilwd else 1
}
if (length(lwd) == 1) {
lwd = rep(lwd, nfacets)
} else if (length(lwd) != nfacets) {
msg = "Length of 'lwd' must be 1 or equal to the number of facets"
stop(msg, call. = FALSE)
}

abline(v = v[ifacet], col = col[ifacet], lty = lty[ifacet], lwd = lwd[ifacet])
abline(v = v[ifacet], col = icol, lty = ilty, lwd = ilwd)
}
return(fun)
}
Expand Down
15 changes: 8 additions & 7 deletions inst/tinytest/test-abline.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ expect_error(tinyplot(type = type_abline(a = 0, b = 1)), pattern = "data points"

f = function() {
plt(mpg ~ hp | factor(cyl), facet = ~ factor(cyl), data = mtcars)
plt_add(type = type_hline(h = 12, col = "pink", lty = 3, lwd = 3))
plt_add(type = type_hline(h = 12), col = "pink", lty = 3, lwd = 3)
}
expect_snapshot_plot(f, label = "hline")


f = function() {
tinyplot(mpg ~ hp | factor(cyl),
facet = ~ factor(cyl), data = mtcars,
tinyplot(
mpg ~ hp | factor(cyl),
facet = ~ factor(cyl),
data = mtcars,
col = c("black", "green", "orange"))
tinyplot_add(type = type_vline(
v = c(100, 150, 200), lty = 3, lwd = 3,
col = c("black", "green", "orange")
))
tinyplot_add(
lty = 3, lwd = 3, col = c("black", "green", "orange"),
type = type_vline(v = c(100, 150, 200)))
}
expect_snapshot_plot(f, label = "vline_vector")

Expand Down
10 changes: 5 additions & 5 deletions man/tinyplot_add.Rd

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

24 changes: 1 addition & 23 deletions man/type_abline.Rd

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

26 changes: 2 additions & 24 deletions man/type_hline.Rd

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

29 changes: 3 additions & 26 deletions man/type_vline.Rd

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