diff --git a/R/draw_legend.R b/R/draw_legend.R index 744a320f..51108cf2 100644 --- a/R/draw_legend.R +++ b/R/draw_legend.R @@ -150,43 +150,47 @@ draw_legend = function( ## Use `!exists` rather than `is.null` for title in case user specified no title if (!exists("title", where = legend_args)) legend_args[["title"]] = by_dep - fill_null = function(name, value) { - if (is.null(legend_args[[name]])) legend_args[[name]] = value - return(legend_args) - } - legend_args = fill_null("pch", pch) - legend_args = fill_null("lty", lty) - legend_args = fill_null("col", col) - legend_args = fill_null("bty", "n") - legend_args = fill_null("horiz", FALSE) - legend_args = fill_null("xpd", NA) + legend_args[["pch"]] = legend_args[["pch"]] %||% pch + legend_args[["lty"]] = legend_args[["lty"]] %||% lty + legend_args[["col"]] = legend_args[["col"]] %||% col + legend_args[["bty"]] = legend_args[["bty"]] %||% "n" + legend_args[["horiz"]] = legend_args[["horiz"]] %||% FALSE + legend_args[["xpd"]] = legend_args[["xpd"]] %||% NA + if (!isTRUE(type %in% c("p", "ribbon", "polygon", "polypath"))) { - legend_args = fill_null("lwd", lwd) + legend_args[["lwd"]] = legend_args[["lwd"]] %||% lwd } + if (isTRUE(type %in% c("p", "pointrange", "errorbar")) && (length(col) == 1 || length(cex) == 1)) { - legend_args = fill_null("pt.cex", cex) + legend_args[["pt.cex"]] = legend_args[["pt.cex"]] %||% cex } + if (isTRUE(type %in% c("rect", "ribbon", "polygon", "polypath", "boxplot", "hist", "histogram", "spineplot", "ridge")) || isTRUE(gradient)) { legend_args[["pch"]] = 22 - legend_args = fill_null("pt.cex", 3.5) - legend_args = fill_null("y.intersp", 1.25) - legend_args = fill_null("seg.len", 1.25) + legend_args[["pt.cex"]] = legend_args[["pt.cex"]] %||% 3.5 + legend_args[["y.intersp"]] = legend_args[["y.intersp"]] %||% 1.25 + legend_args[["seg.len"]] = legend_args[["seg.len"]] %||% 1.25 } + if (isTRUE(type %in% c("ribbon", "hist", "histogram", "spineplot"))) { - legend_args = fill_null("pt.lwd", 0) + legend_args[["pt.lwd"]] = legend_args[["pt.lwd"]] %||% 0 } + if (identical(type, "n") && isFALSE(gradient)) { - legend_args = fill_null("pch", par("pch")) + legend_args[["pch"]] = legend_args[["pch"]] %||% par("pch") } + if (identical(type, "spineplot")) { - legend_args = fill_null("pt.bg", legend_args[["col"]]) + legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% legend_args[["col"]] } + if (identical(type, "ridge") && isFALSE(gradient)) { - legend_args = fill_null("pt.bg", sapply(legend_args[["col"]], function(ccol) seq_palette(ccol, n = 2)[2])) - } - legend_args = fill_null("pt.bg", bg) + legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% sapply(legend_args[["col"]], function(ccol) seq_palette(ccol, n = 2)[2]) + } + + legend_args[["pt.bg"]] = legend_args[["pt.bg"]] %||% bg - legend_args = fill_null("legend", lgnd_labs) + legend_args[["legend"]] = legend_args[["legend"]] %||% lgnd_labs if (length(lgnd_labs) != length(eval(legend_args[["legend"]]))) { warning( "\nUser-supplied legend labels do not match the number of groups.\n", diff --git a/R/utils.R b/R/utils.R index 59a4075d..ef093249 100644 --- a/R/utils.R +++ b/R/utils.R @@ -27,3 +27,8 @@ more_than_n_unique = function(x, n, small_vec_len = 1e3L) { return(FALSE) } } + + +if (getRversion() <= "4.4.0") { + `%||%` = function(x, y) if (is.null(x)) y else x +}