Skip to content

Commit

Permalink
user proper key widths, stop() if there's not enough space
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Aug 18, 2023
1 parent ebf336c commit 69f127d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# version 1.0-15

* `plot.sf()`: `key.width` is sensitive to pointsize graphics parameter, `key.pos` can hold a second value in [0, 1] determining the relative position of the key in the available space
* `plot.sf()`: `key.width` is sensitive to pointsize graphics parameter, `key.pos` can hold a second value in [0, 1] determining the relative position of the key in the available space; keys with factor levels suggest a proper size if they won't fit.

* `[<-.sf` fixes the `agr` attribute when it is broken; #2211

Expand Down
18 changes: 13 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,7 @@ xy_from_r = function(r, l, o) {
..., axes = FALSE, key.width, key.length, cex.axis = par("cex.axis")) {

n = length(z)
# TODO:
ksz = as.numeric(gsub(" cm", "", key.width)) * 2
ksz = max(strwidth(z, "inches")) / par("cin")[1] # in "mar" lines
breaks = (0:n) + 0.5
offset = 0.5
if (length(key.pos) == 2) {
Expand All @@ -921,15 +920,24 @@ xy_from_r = function(r, l, o) {
ylim = xy_from_r(range(breaks), key.length, offset)
xlim = c(0, 1)
mar = c(ifelse(axes, 2.1, 1), 0, 1.2, 0)
mar[key.pos] = max(ksz - 1.3, 0.0)
mar[key.pos] = ksz
}
par(mar = mar)

poly = vector(mode="list", length(col))
for (i in seq(poly))
poly[[i]] = c(breaks[i], breaks[i+1], breaks[i+1], breaks[i])
plot(1, 1, t = "n", ylim = ylim, xlim = xlim, axes = FALSE,
xlab = "", ylab = "", xaxs = "i", yaxs = "i")

tryCatch({
plot(1, 1, t = "n", ylim = ylim, xlim = xlim, axes = FALSE,
xlab = "", ylab = "", xaxs = "i", yaxs = "i")
},
error = function(x) {
sz = max(strwidth(z, "inches")) * 2.54 + par("ps")/12 # cm
stop(paste0("key.width too small, try key.width = lcm(", signif(sz, 3), ")"), call. = FALSE)
}
)

for(i in seq_along(poly)) {
if (key.pos %in% c(1,3))
polygon(poly[[i]], c(0, 0, 1, 1), col = col[i], border = NA)
Expand Down

0 comments on commit 69f127d

Please sign in to comment.