Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ where the formatting is also better._

## Development

### New features

- Improved horizontal legend spacing, as well as multicolumn legend support. A
new example in the "Tips & tricks" vignettes demonstrates the latter.
(#446 @grantmcdermott)

### Internals

- Move `altdoc` from `Suggests` to `Config/Needs/website`.
Expand Down
39 changes: 29 additions & 10 deletions R/draw_legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ draw_legend = function(
)
legend_args[["legend"]] = lgnd_labs
}

if (isTRUE(gradient)) {
legend_args[["ncol"]] = NULL
}

#
## legend placement ----
Expand Down Expand Up @@ -329,24 +333,39 @@ draw_legend = function(
}
}

legend_args[["horiz"]] = TRUE
# enforce horizontal legend if user hasn't specified ncol arg
# (exception: gradient legends at bottom/top are always horizontal)
if (is.null(legend_args[["ncol"]]) || gradient) legend_args[["horiz"]] = TRUE

# Catch for horizontal ribbon legend spacing
if (type=="ribbon" && isTRUE(legend_args[["horiz"]])) {
} else {

legend_args[["inset"]] = 0
if (new_plot && draw) plot.new()

}

# Additional tweaks for horiz and/or multi-column legends
if (isTRUE(legend_args[["horiz"]]) || !is.null(legend_args[["ncol"]])) {
# tighter horizontal labelling
# See: https://github.com/grantmcdermott/tinyplot/issues/434
if (!gradient) {
legend_args[["text.width"]] = NA
# Add a space to all labs except the outer most right ones
nlabs = length(legend_args[["legend"]])
nidx = nlabs
if (!is.null(legend_args[["ncol"]])) nidx = tail(1:nlabs, (nlabs %/% legend_args[["ncol"]]))
legend_args[["legend"]][-nidx] = paste(legend_args[["legend"]][-nidx], " ")
}
# catch for horizontal ribbon legend spacing
if (type=="ribbon") {
if (legend_args[["pt.lwd"]] == 1) {
legend_args[["x.intersp"]] = 1
} else {
legend_args[["x.intersp"]] = 0.5
}
} else if (gradient && isTRUE(legend_args[["horiz"]])) {
} else if (gradient) {
legend_args[["x.intersp"]] = 0.5
}

} else {

legend_args[["inset"]] = 0
if (new_plot && draw) plot.new()

}

#
Expand Down
8 changes: 4 additions & 4 deletions inst/tinytest/_tinysnapshot/density_legend_bottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions inst/tinytest/_tinysnapshot/legend_default_legend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions inst/tinytest/_tinysnapshot/legend_formula_legend.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions inst/tinytest/_tinysnapshot/legend_keyword_default.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions inst/tinytest/_tinysnapshot/legend_keyword_formula.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions inst/tinytest/_tinysnapshot/legend_keyword_outerbottom.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions inst/tinytest/_tinysnapshot/legend_keyword_outertop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading