-
Notifications
You must be signed in to change notification settings - Fork 22
Types: abline, hline, vline
#249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
grantmcdermott
merged 9 commits into
grantmcdermott:main
from
vincentarelbundock:type_hline
Nov 13, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0c414a4
abline, hline, vline
vincentarelbundock b23b6be
quarto
vincentarelbundock bf2eb28
respect facet colors / by
vincentarelbundock b60ec10
docs
vincentarelbundock 805418e
type_function
vincentarelbundock 529956c
type_function() test
vincentarelbundock 76d7f7d
news tinyplot() consistency
vincentarelbundock 8569e36
type_abline etc. informative error if no existing data points
vincentarelbundock b250297
Merge branch 'main' into type_hline
vincentarelbundock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #' 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) { | ||
| data_abline = function(datapoints, ...) { | ||
| if (nrow(datapoints) == 0) { | ||
| msg = "`type_abline() only works on existing plots with x and y data points." | ||
| stop(msg, call. = FALSE) | ||
| } | ||
| return(list()) | ||
| } | ||
| draw_abline = function() { | ||
| fun = function(ifacet, data_facet, icol, ilty, ilwd, ...) { | ||
| nfacets = length(data_facet) | ||
|
|
||
| if (length(a) == 1) { | ||
| a = rep(a, nfacets) | ||
| } else if (length(a) != nfacets) { | ||
| msg = "Length of 'a' must be 1 or equal to the number of facets" | ||
| stop(msg, call. = FALSE) | ||
| } | ||
|
|
||
| if (length(b) == 1) { | ||
| b = rep(b, nfacets) | ||
| } else if (length(b) != nfacets) { | ||
| msg = "Length of 'b' must be 1 or equal to the number of facets" | ||
| 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]) | ||
| } | ||
| return(fun) | ||
| } | ||
| out = list( | ||
| draw = draw_abline(), | ||
| data = data_abline, | ||
| name = "abline" | ||
| ) | ||
| class(out) = "tinyplot_type" | ||
| return(out) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #' 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)) | ||
| #' @export | ||
| type_hline = function(h = 0, col = NULL, lty = NULL, lwd = NULL) { | ||
| data_hline = function(datapoints, ...) { | ||
| if (nrow(datapoints) == 0) { | ||
| msg = "`type_hline() only works on existing plots with x and y data points." | ||
| stop(msg, call. = FALSE) | ||
| } | ||
| return(list()) | ||
| } | ||
| draw_hline = function() { | ||
| fun = function(ifacet, data_facet, icol, ilty, ilwd, ...) { | ||
| nfacets = length(data_facet) | ||
|
|
||
| if (length(h) == 1) { | ||
| h = rep(h, nfacets) | ||
| } else if (length(h) != nfacets) { | ||
| msg = "Length of 'h' must be 1 or equal to the number of facets" | ||
| 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]) | ||
| } | ||
| return(fun) | ||
| } | ||
| out = list( | ||
| draw = draw_hline(), | ||
| data = data_hline, | ||
| name = "hline" | ||
| ) | ||
| class(out) = "tinyplot_type" | ||
| return(out) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #' 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 | ||
| #' )) | ||
| #' @export | ||
| type_vline = function(v = 0, col = "black", lty = 1, lwd = 1) { | ||
| assert_numeric(v) | ||
| data_vline = function(datapoints, ...) { | ||
| if (nrow(datapoints) == 0) { | ||
| msg = "`type_vline() only works on existing plots with x and y data points." | ||
| stop(msg, call. = FALSE) | ||
| } | ||
| return(list()) | ||
| } | ||
| draw_vline = function() { | ||
| fun = function(ifacet, data_facet, icol, ilty, ilwd, ...) { | ||
| nfacets = length(data_facet) | ||
|
|
||
| if (length(v) == 1) { | ||
| v = rep(v, nfacets) | ||
| } else if (length(v) != nfacets) { | ||
| msg = "Length of 'v' must be 1 or equal to the number of facets" | ||
| 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]) | ||
| } | ||
| return(fun) | ||
| } | ||
| out = list( | ||
| draw = draw_vline(), | ||
| data = data_vline, | ||
| name = "vline" | ||
| ) | ||
| class(out) = "tinyplot_type" | ||
| return(out) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.