Skip to content

Commit

Permalink
Merge branch 'main' into continuous-legend
Browse files Browse the repository at this point in the history
  • Loading branch information
grantmcdermott authored Feb 14, 2024
2 parents a093b86 + 3eb19d9 commit 8431eb7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tinyplot
Type: Package
Title: Lightweight Extension of the Base R Graphics System
Version: 0.0.5
Version: 0.0.5.9000
Authors@R:
c(
person(
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# News

## 0.0.5.9000 (development version)

- Support user-supplied polygons. (#127 @grantmcdermott)

## 0.0.5

**IMPORTANT BREAKING CHANGE:**
Expand Down
4 changes: 2 additions & 2 deletions R/draw_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ draw_legend = function(
) {
legend.args[["pt.cex"]] = cex
}
if (type=="ribbon" || isTRUE(gradient)) {
if (type %in% c("ribbon", "polygon") || isTRUE(gradient)) {
if (is.null(legend.args[["pch"]])) legend.args[["pch"]] = 22
if (is.null(legend.args[["pt.cex"]])) legend.args[["pt.cex"]] = 3.5
if (is.null(legend.args[["pt.lwd"]])) legend.args[["pt.lwd"]] = 0
if (is.null(legend.args[["pt.lwd"]]) && type != "polygon") legend.args[["pt.lwd"]] = 0
if (is.null(legend.args[["y.intersp"]])) legend.args[["y.intersp"]] = 1.25
if (is.null(legend.args[["seg.len"]])) legend.args[["seg.len"]] = 1.25
}
Expand Down
29 changes: 23 additions & 6 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
#' lines, "o" for overplotted points and lines, "s" and "S" for stair steps
#' and "h" for histogram-like vertical lines. "n" does not produce
#' any points or lines.
#' - Additional tinyplot types: "density" for densities, "pointrange" or
#' "errorbar" for segement intervals, and "ribbon" or "area" for polygon
#' intervals (where area plots are a special case of ribbon plots with `ymin`
#' set to 0 and `ymax` set to `y`; see below).
#' - Additional tinyplot types: "density" for densities, "polygon" for
#' polygons, "pointrange" or "errorbar" for segment intervals, and "polygon",
#' "ribbon" or "area" for polygon intervals (where area plots are a special
#' case of ribbon plots with `ymin` set to 0 and `ymax` set to `y`; see below).
#' @param xlim the x limits (x1, x2) of the plot. Note that x1 > x2 is allowed
#' and leads to a ‘reversed axis’. The default value, NULL, indicates that
#' the range of the `finite` values to be plotted should be used.
Expand Down Expand Up @@ -480,11 +480,13 @@ tinyplot.default = function(

if (is.null(xlab)) xlab = x_dep
if (is.null(ylab)) ylab = y_dep


was_area_type = FALSE # flag to keep track for some legend adjustments below
if (type == "area") {
ymax = y
ymin = rep.int(0, length(y))
type = "ribbon"
was_area_type = TRUE
}

xlabs = NULL
Expand Down Expand Up @@ -684,7 +686,9 @@ tinyplot.default = function(
# place and draw the legend
has_legend = FALSE # simple indicator variable for later use

legend.args = dots[["legend.args"]]
if (!exists("legend.args")) {
legend.args = dots[["legend.args"]]
}
if (is.null(legend.args)) legend.args = list(x = NULL)
legend = substitute(legend)

Expand Down Expand Up @@ -715,6 +719,11 @@ tinyplot.default = function(

has_sub = !is.null(sub)

if (isTRUE(was_area_type)) {
legend.args[["pt.lwd"]] = par("lwd")
legend.args[["lty"]] = 0
}

draw_legend(
legend = legend,
legend.args = legend.args,
Expand Down Expand Up @@ -1236,6 +1245,14 @@ tinyplot.default = function(
lty = ilty#lty[i]
)
if (rtype) type = "ribbon"
} else if (type == "polygon") {
polygon(
x = xx,
y = yy,
border = col[i],
col = bg[i],
lty = lty[i]
)
} else {
stop("`type` argument not supported.", call. = FALSE)
}
Expand Down
33 changes: 33 additions & 0 deletions inst/tinytest/_tinysnapshot/polygon_heart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions inst/tinytest/test-polygon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source("helpers.R")
using("tinysnapshot")

i = seq(0, 2*pi, by = 0.01)
x = 16*sin(i)^3
y = 13*cos(i) - 5*cos(2*i) - 2*cos(3*i) - cos(4*i)

f = function() {
plt(
x, y,
type = "polygon",
col = "red", fill = adjustcolor("firebrick", 0.8),
axes = FALSE, xlab = NA, ylab = NA
)
}
expect_snapshot_plot(f, label = "polygon_heart")
8 changes: 4 additions & 4 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 8431eb7

Please sign in to comment.