diff --git a/NAMESPACE b/NAMESPACE index fdfa62df..f43e8d96 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/init.R b/R/init.R index 9a616f29..d458c3f8 100644 --- a/R/init.R +++ b/R/init.R @@ -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 diff --git a/R/plot.R b/R/plot.R index d331fee3..a4ffe765 100644 --- a/R/plot.R +++ b/R/plot.R @@ -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) }