diff --git a/DESCRIPTION b/DESCRIPTION
index 86f5ef21..3b6ab4a2 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -1,7 +1,7 @@
Package: tinyplot
Type: Package
Title: Lightweight Extension of the Base R Graphics System
-Version: 0.2.1
+Version: 0.2.1.99
Date: 2024-08-25
Authors@R:
c(
diff --git a/R/tinyplot.R b/R/tinyplot.R
index c60bfda5..c622adfa 100644
--- a/R/tinyplot.R
+++ b/R/tinyplot.R
@@ -599,6 +599,7 @@ tinyplot.default = function(
facet.args[["nrow"]] = attr(facet, "facet_nrow")
}
}
+ facet_attr = attributes(facet) ## TODO: better solution for restoring facet attributes?
if (is.null(x)) {
## Special catch for rect and segment plots without a specified y-var
@@ -737,6 +738,7 @@ tinyplot.default = function(
# Note: We're do this up front, so we can make some adjustments to legend cex
# next (if there are facets). But the actual drawing of the facets will only
# come later.
+ attributes(facet) = facet_attr ## TODO: better solution for restoring facet attributes?
fargs = facet_layout(facet = facet, facet.args = facet.args, add = add)
list2env(fargs, environment())
diff --git a/R/type_density.R b/R/type_density.R
index cb3ff672..9799c376 100644
--- a/R/type_density.R
+++ b/R/type_density.R
@@ -32,11 +32,14 @@ tinyplot.density = function(
## override if bg = "by"
if (!is.null(bg) || !is.null(fill)) type = "area"
- # catch for facet_grid
- if (!is.null(facet)) {
- facet_attributes = attributes(facet)
- # facet_grid = attr(facet, "facet_grid")
+ ## ensure factor and preserve facet attributes
+ if (!is.null(facet) && !is.factor(facet)) {
+ facet_attr = attributes(facet)
+ facet = factor(facet)
+ facet_attr[["class"]] <- NULL
+ for (i in names(facet_attr)) attr(facet, i) <- facet_attr[[i]]
}
+ facet_attr = attributes(facet) ## TODO: better solution for restoring facet attributes?
if (inherits(x, "density")) {
object = x
@@ -164,14 +167,10 @@ tinyplot.density = function(
if (is.null(xlab)) xlab = paste0("N = ", object$n, " Bandwidth = ", sprintf("%.4g", object$bw))
if (is.null(main)) main = paste0(paste(object$call, collapse = "(x = "), ")")
- # if (!is.null(facet)) attr(facet, "facet_grid") = facet_grid
+ ## facet handling
if (!is.null(facet)) {
- if (!is.null(facet_attributes[["levels"]])) {
- facet = factor(facet, levels = facet_attributes[["levels"]])
- } else {
- facet = factor(facet)
- }
- attr(facet, "facet_grid") = facet_attributes[["facet_grid"]]
+ facet = factor(facet, levels = facet_attr[["levels"]])
+ attributes(facet) = facet_attr ## TODO: better solution for restoring facet attributes?
}
tinyplot.default(
diff --git a/inst/tinytest/_tinysnapshot/facet_density_3x2.svg b/inst/tinytest/_tinysnapshot/facet_density_3x2.svg
new file mode 100644
index 00000000..413e3498
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/facet_density_3x2.svg
@@ -0,0 +1,273 @@
+
+
diff --git a/inst/tinytest/_tinysnapshot/facet_hist_3x2.svg b/inst/tinytest/_tinysnapshot/facet_hist_3x2.svg
new file mode 100644
index 00000000..f691f7e9
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/facet_hist_3x2.svg
@@ -0,0 +1,308 @@
+
+
diff --git a/inst/tinytest/test-facet.R b/inst/tinytest/test-facet.R
index 9a0a77db..a4277630 100644
--- a/inst/tinytest/test-facet.R
+++ b/inst/tinytest/test-facet.R
@@ -462,6 +462,24 @@ f = function() {
}
expect_snapshot_plot(f, label = "facet_density_grid")
+f = function() {
+ tinyplot(
+ ~ wt, data = mtcars,
+ type = "hist",
+ facet = cyl ~ am
+ )
+}
+expect_snapshot_plot(f, label = "facet_hist_3x2")
+
+f = function() {
+ tinyplot(
+ ~ wt, data = mtcars,
+ type = "density",
+ facet = cyl ~ am
+ )
+}
+expect_snapshot_plot(f, label = "facet_density_3x2")
+
#
# restore original par settings
#