fmtkhông có gì để làm với khoảng cách của các mục huyền thoại. Đối với một mô tả chi tiết fmtxin vui lòng xem Sử dụng C-style Chuỗi Formatting Commands . Chỉ cần dán đoạn mã sau vào Rbảng điều khiển của bạn để xem sự khác biệt (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()gọi legend()nội bộ. Để giảm khoảng cách ngang giữa các mục chú thích, bạn nên thay đổi text.widththam số của legend()hàm. Thật không may choro.legend, không cung cấp một tham số để đặt text.widthbên ngoài thay vì tính toán bên trong. Tôi đã thêm một space_reductiontham số cho choro.legendvà sửa đổi một chút chức năng ban đầu như sau:
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)
}
Lưu đoạn mã này trong tệp tập lệnh R và sourcenó. Một đoạn mã có thể tái tạo sẽ như sau:
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)
Dần dần giảm / tăng space_reductiontham số để đạt được kết quả mong muốn.
