diff --git a/DESCRIPTION b/DESCRIPTION index db1aa9c2..71138161 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tinyplot Type: Package Title: Lightweight Extension of the Base R Graphics System -Version: 0.0.5.9007 +Version: 0.0.5.9008 Authors@R: c( person( diff --git a/NEWS.md b/NEWS.md index 95030805..94e1e29b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ # News -## 0.0.5.9007 (development version) +## 0.0.5.9008 (development version) License: @@ -39,6 +39,8 @@ Bug fixes: - Fixed bug that prevented `tpar(facet.x = ...)` args from being passed forward and set correctly. (#137 @grantmcdermott) +- Fixed bug where custom legends weren't working with `type = "density"`. (#147 +@grantmcdermott) Internals: diff --git a/R/draw_legend.R b/R/draw_legend.R index 8fa58383..29c9ad51 100644 --- a/R/draw_legend.R +++ b/R/draw_legend.R @@ -118,26 +118,28 @@ draw_legend = function( # ## legend args ---- - if (is.null(legend)) { - legend_args[["x"]] = "right!" - } else if (is.character(legend)) { - legend_args = utils::modifyList(legend_args, list(x = legend)) - } else if (class(legend) %in% c("call", "name")) { - largs = as.list(legend) - if (is.null(largs[["x"]])) { - lnms = names(largs) - # check second position b/c first will be a symbol - if (is.null(lnms)) { - largs = stats::setNames(largs, c("", "x")) - } else if (length(largs)>=2 && lnms[2] == "") { - lnms[2] = "x" - largs = stats::setNames(largs, lnms) - } else { - largs[["x"]] = "right!" + if (is.null(legend_args[["x"]])) { + if (is.null(legend)) { + legend_args[["x"]] = "right!" + } else if (is.character(legend)) { + legend_args = utils::modifyList(legend_args, list(x = legend)) + } else if (class(legend) %in% c("call", "name")) { + largs = as.list(legend) + if (is.null(largs[["x"]])) { + lnms = names(largs) + # check second position b/c first will be a symbol + if (is.null(lnms)) { + largs = stats::setNames(largs, c("", "x")) + } else if (length(largs)>=2 && lnms[2] == "") { + lnms[2] = "x" + largs = stats::setNames(largs, lnms) + } else { + largs[["x"]] = "right!" + } } + # Finally, combine with any pre-existing legend args (e.g., title from the by label) + legend_args = utils::modifyList(legend_args, largs, keep.null = TRUE) } - # Finally, combine with any pre-existing legend args (e.g., title from the by label) - legend_args = utils::modifyList(legend_args, largs, keep.null = TRUE) } ## Use `!exists` rather than `is.null` for title in case user specified no title diff --git a/R/tinyplot.R b/R/tinyplot.R index 00be9975..1b44e2d6 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -549,8 +549,15 @@ tinyplot.default = function( } ## Another catch for bespoke legend position (if originally passed via the formula method) if (!is.null(fargs[["legend"]]) && !is.null(fargs[["legend_args"]])) { - if (names(fargs[["legend"]])[1] == "") names(fargs[["legend"]])[1] = "x" - fargs[["legend_args"]] = utils::modifyList(fargs[["legend"]], fargs[["legend_args"]]) + if (is.atomic(fargs[["legend"]])) { + fargs[["legend"]] = list(x = fargs[["legend"]]) + } else if (!is.list(fargs[["legend"]])) { + fargs[["legend"]] = as.list(fargs[["legend"]]) + } + if (is.null(names(fargs[["legend"]])[1]) || names(fargs[["legend"]])[1] == "") { + names(fargs[["legend"]])[1] = "x" + } + fargs[["legend_args"]] = modifyList(fargs[["legend"]], fargs[["legend_args"]]) fargs[["legend"]] = NULL } fargs$y = fargs$ymin = fargs$ymax = fargs$ylab = fargs$xlab = NULL diff --git a/inst/tinytest/_tinysnapshot/density_legend_bottom.svg b/inst/tinytest/_tinysnapshot/density_legend_bottom.svg new file mode 100644 index 00000000..97553741 --- /dev/null +++ b/inst/tinytest/_tinysnapshot/density_legend_bottom.svg @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + +am +0 +1 + + + + + + + +N = [19, 13] Joint Bandwidth = 2.453 +Density + + + + + + + +10 +20 +30 +40 + + + + + + +0.00 +0.02 +0.04 +0.06 +0.08 + + + + + + + + + + + + diff --git a/inst/tinytest/test-density.R b/inst/tinytest/test-density.R index 8e587de8..da5e28ac 100644 --- a/inst/tinytest/test-density.R +++ b/inst/tinytest/test-density.R @@ -54,3 +54,10 @@ expect_snapshot_plot(f1a, label = "density_type_fill") expect_snapshot_plot(f1b, label = "density_type_fill") expect_snapshot_plot(f2a, label = "density_type_fill") expect_snapshot_plot(f2b, label = "density_type_fill") + +# Some extra tests for bespoke legend placement +f1 = function() with(mtcars, tinyplot(mpg, by = am, type = "density", legend = "bottom!")) +f2 = function() with(mtcars, tinyplot(mpg, by = am, type = "density", legend = list(x = "bottom!"))) +expect_snapshot_plot(f1, label = "density_legend_bottom") +expect_snapshot_plot(f2, label = "density_legend_bottom") +