fmtno tiene nada que ver con el espaciado de elementos de leyenda. Para una descripción detallada de fmtconsulte Uso de estilo secuencia de C Comandos de formatos . Simplemente pegue el siguiente fragmento de código en su Rconsola para ver las diferencias (pi ~ 3.14):
sprintf("%f", pi)
sprintf("%.3f", pi)
sprintf("%1.0f", pi)
sprintf("%5.1f", pi)
sprintf("%05.1f", pi)
sprintf("%+f", pi)
sprintf("% f", pi)
sprintf("%-10f", pi) # left justified
sprintf("%e", pi)
sprintf("%E", pi)
sprintf("%g", pi)
sprintf("%g", 1e6 * pi) # -> exponential
sprintf("%.9g", 1e6 * pi) # -> "fixed"
sprintf("%G", 1e-6 * pi)
choro.legend()Llamadas legend()internas. Para reducir el espacio horizontal entre los elementos de la leyenda, debe cambiar el text.widthparámetro de la legend()función. Desafortunadamente choro.legend, no proporciona un parámetro para establecer text.widthexternamente, sino que lo calcula internamente. Agregué un space_reductionparámetro choro.legendy modifiqué ligeramente la función original de la siguiente manera:
choro.legend <- function (px, py, sh, under = "under", over = "over", between = "to",
fmt = "%g", cex = 1, space_reduction = 0, ...)
{
x = sh$breaks
lx = length(x)
if (lx < 3)
stop("break vector too short")
res = character(lx + 1)
res[1] = paste(under, sprintf(fmt, x[1]))
for (i in 1:(lx - 1)) res[i + 1] <- paste(sprintf(fmt, x[i]),
between, sprintf(fmt, x[i + 1]))
res[lx + 1] <- paste(over, sprintf(fmt, x[lx]))
maxwidth <- max(strwidth(res)) - space_reduction
temp <- legend(x = px, y = py, legend = rep(" ", length(res)),
fill = sh$cols, text.width = maxwidth, cex = cex, ...)
text(temp$rect$left + temp$rect$w, temp$text$y, res, pos = 2,
cex = cex)
}
Guarde este fragmento en un archivo de secuencia de comandos R y source. Un fragmento de código reproducible sería el siguiente:
library(GISTools)
data(newhaven)
blocks
val <- blocks@data$POP1990
shade <- auto.shading(val)
choropleth(blocks, v= val, shade)
choro.legend(514000, 175000,shade,title='My Legend',cex=.8, bty = "n", fmt = "%0.0f",
space_reduction=4000)
Disminuya / aumente gradualmente el space_reductionparámetro para lograr el resultado deseado.
