Skip to content

Commit

Permalink
sync kw_dflt() with that of sf
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Nov 30, 2023
1 parent d4ec6ce commit 1ee07b6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ importFrom(abind,abind)
importFrom(abind,adrop)
importFrom(abind,asub)
importFrom(classInt,classIntervals)
importFrom(grDevices,cm)
importFrom(grDevices,col2rgb)
importFrom(grDevices,dev.capabilities)
importFrom(grDevices,dev.size)
Expand Down
2 changes: 1 addition & 1 deletion R/init.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @importFrom graphics image.default image par plot title box text axis plot.new plot.window rasterImage layout lcm contour hist strwidth
#' @importFrom grDevices dev.capabilities dev.size grey rgb col2rgb
#' @importFrom grDevices dev.capabilities dev.size grey rgb col2rgb cm
#' @importFrom utils head tail setTxtProgressBar txtProgressBar packageVersion methods modifyList
#' @importFrom stats na.omit runif aggregate setNames predict quantile var complete.cases na.pass time
#' @importFrom tools file_ext
Expand Down
30 changes: 24 additions & 6 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,31 @@ make_label = function(x, i = 1) {
names(x)[i]
}

#kw_dflt = function(x, key.pos) {
# if (is.null(key.pos) || key.pos <= 0)
# lcm(0)
# else if (key.pos %in% c(2,4) && is.factor(x[[1]]))
# lcm(max(strwidth(levels(x[[1]]), "inches")) * 2.54 * 1.1 + par("ps")/12) # cm
# else
# lcm(1.8 * par("ps")/12)
#}
# copy from sf:
kw_dflt = function(x, key.pos) {
if (is.null(key.pos) || key.pos <= 0)
lcm(0)
else if (key.pos %in% c(2,4) && is.factor(x[[1]]))
lcm(max(strwidth(levels(x[[1]]), "inches")) * 2.54 * 1.1 + par("ps")/12) # cm
else
lcm(1.8 * par("ps")/12)
if (is.null(key.pos) || key.pos == 0) # no key:
return(lcm(0))

font_scale = par("ps") / 12
if (key.pos == -1)
lcm(1.8 * font_scale)
else if (key.pos %in% c(2, 4) && (is.character(x[[1]]) || is.factor(x[[1]]))) {
strings = if (is.factor(x[[1]]))
levels(x[[1]])
else
x[[1]]
lcm(cm(max(strwidth(strings, "inches"))) * 1.3 + font_scale) # cm
#lcm(cm(max(strwidth(strings, "inches"))) * 1.3) # cm
} else
lcm(1.8 * font_scale)
}


Expand Down

0 comments on commit 1ee07b6

Please sign in to comment.