diff --git a/NEWS.md b/NEWS.md
index 63abd001..66c51b5a 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -120,6 +120,9 @@ visualizations.
and spacing now scale correctly with `facet.cex`, multiline facet titles
inter-panel gaps remain fixed regardless of strip height, and strip text is
now vertically centered within the background rect. (#586 @grantmcdermott)
+- Fixed Issue #593 where `palette.qualitative` in themes could not be a
+ function. Thanks to @katrinabrock for the report. (#594 @zeileis)
+
## v0.6.1
diff --git a/R/by_aesthetics.R b/R/by_aesthetics.R
index 87453e95..b53b8035 100755
--- a/R/by_aesthetics.R
+++ b/R/by_aesthetics.R
@@ -156,7 +156,7 @@ resolve_palette_colors = function(palette, theme_palette, ngrps, ordered, gradie
# Pick theme palette if no explicit palette provided
if (is.null(palette_choice) && !is.null(theme_palette)) {
palette_choice = theme_palette
- if (length(theme_palette) == 1) {
+ if (is.character(theme_palette) && length(theme_palette) == 1) {
# Check if theme palette needs to switch to sequential
use_sequential = FALSE
idx = match_palette_name(theme_palette, palette.pals())
@@ -301,7 +301,11 @@ by_col = function(col, palette, alpha, by_ordered, by_continuous, ngrps, adjustc
return(cols)
}
- pal_theme = get_tpar("palette.qualitative", default = NULL)
+ pal_theme = if (ordered || gradient) {
+ get_tpar("palette.sequential", default = NULL)
+ } else {
+ get_tpar("palette.qualitative", default = NULL)
+ }
cols = resolve_palette_colors(
palette = palette,
theme_palette = pal_theme,
@@ -323,9 +327,16 @@ by_bg = function(bg, fill, col, palette, alpha, by_ordered, by_continuous, ngrps
bg = "by"
}
if (!is.null(bg) && length(bg) == 1 && is_by_keyword(bg)) {
+ ordered = if (is.null(by_ordered)) FALSE else by_ordered
+ gradient = if (is.null(by_continuous)) FALSE else by_continuous
+ pal_theme = if (ordered || gradient) {
+ get_tpar("palette.sequential", default = NULL)
+ } else {
+ get_tpar("palette.qualitative", default = NULL)
+ }
bg = resolve_palette_colors(
palette = palette,
- theme_palette = get_tpar("palette.qualitative", default = NULL),
+ theme_palette = pal_theme,
ngrps = ngrps,
ordered = if (is.null(by_ordered)) FALSE else by_ordered,
gradient = if (is.null(by_continuous)) FALSE else by_continuous,
diff --git a/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_qualitative.svg b/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_qualitative.svg
new file mode 100644
index 00000000..d634e8d2
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_qualitative.svg
@@ -0,0 +1,113 @@
+
+
diff --git a/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_sequential.svg b/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_sequential.svg
new file mode 100644
index 00000000..e70b506d
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/tinytheme_ephemeral_palette_function_sequential.svg
@@ -0,0 +1,95 @@
+
+
diff --git a/inst/tinytest/_tinysnapshot/tinytheme_palette_function_qualitative.svg b/inst/tinytest/_tinysnapshot/tinytheme_palette_function_qualitative.svg
new file mode 100644
index 00000000..d634e8d2
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/tinytheme_palette_function_qualitative.svg
@@ -0,0 +1,113 @@
+
+
diff --git a/inst/tinytest/_tinysnapshot/tinytheme_palette_function_sequential.svg b/inst/tinytest/_tinysnapshot/tinytheme_palette_function_sequential.svg
new file mode 100644
index 00000000..e70b506d
--- /dev/null
+++ b/inst/tinytest/_tinysnapshot/tinytheme_palette_function_sequential.svg
@@ -0,0 +1,95 @@
+
+
diff --git a/inst/tinytest/test-tinytheme.R b/inst/tinytest/test-tinytheme.R
index 9584c772..e63f63e6 100644
--- a/inst/tinytest/test-tinytheme.R
+++ b/inst/tinytest/test-tinytheme.R
@@ -157,6 +157,29 @@ expect_snapshot_plot(f, label = "tinytheme_dynamic_clean_spineplot")
tinytheme()
+## palette functions (#593)
+
+pal = colorRampPalette(c("darkblue", "deeppink", "cornsilk"))
+
+f = function () {
+ tinytheme("clean", palette.sequential = pal, pch = 21)
+ tinyplot(1:9, by = 1:9, cex = 3, lwd = 3, bg = "by")
+}
+expect_snapshot_plot(f, label = "tinytheme_palette_function_sequential")
+
+f = function () {
+ tinytheme("clean", palette.qualitative = pal, pch = 21)
+ tinyplot(1:9, by = factor(1:9), cex = 3, lwd = 3, bg = "by")
+}
+expect_snapshot_plot(f, label = "tinytheme_palette_function_qualitative")
+
+
+#
+## reset
+
+tinytheme()
+
+
#
## ephemeral theme
@@ -187,3 +210,18 @@ f = function() {
tinytheme()
}
expect_snapshot_plot(f, label = "tinytheme_dynmar_mar_override")
+
+## palette functions (#593)
+pal = colorRampPalette(c("darkblue", "deeppink", "cornsilk"))
+
+f = function () {
+ tinyplot(1:9, by = 1:9, cex = 3, lwd = 3, bg = "by",
+ theme = list("clean", palette.sequential = pal, pch = 21))
+}
+expect_snapshot_plot(f, label = "tinytheme_ephemeral_palette_function_sequential")
+
+f = function () {
+ tinyplot(1:9, by = factor(1:9), cex = 3, lwd = 3, bg = "by",
+ theme = list("clean", palette.qualitative = pal, pch = 21))
+}
+expect_snapshot_plot(f, label = "tinytheme_ephemeral_palette_function_qualitative")