There a way to adjust the height
and width
parameters of colorkey legend of plot.sf
, as is possible in spplot
?
library(sf)
nc_sf <- st_read(system.file("shape/nc.shp", package="sf"))
plot(nc_sf["AREA"], main = "sf", key.pos = 1,
key.size = lcm(1.2)) # Only adjust the height of key (on bottom side)
# How adjust the width ?
library(sp)
nc_sp <- as(nc_sf, 'Spatial')
spplot(nc_sp, "AREA", main = "sp",
colorkey = list(space = "bottom",
height = 0.5, width = 1))
edited:
I trying something around the original code plot.R at lines 203-211 and
if (! isTRUE(dots$add) && ! is.null(key.pos) && !all(is.na(values)) &&
(is.factor(values) || length(unique(na.omit(values))) > 1) &&
length(col) > 1) { # plot key?
switch(key.pos,
layout(matrix(c(2,1), nrow = 2, ncol = 1), widths = 1, heights = c(1, key.size)), # 1 bottom
layout(matrix(c(1,2), nrow = 1, ncol = 2), widths = c(key.size, 1), heights = 1), # 2 left
layout(matrix(c(1,2), nrow = 2, ncol = 1), widths = 1, heights = c(key.size, 1)), # 3 top
layout(matrix(c(2,1), nrow = 1, ncol = 2), widths = c(1, key.size), heights = 1) # 4 right
)
changing the widths
parameter and re-building the package. But don't work.
Maybe something around the function .get_layout
?
re-edited
My clumsy solution: rebuild the original function with some adjusts
my_plot.sf <- function(x, y, ..., col = NULL, main, pal = NULL, nbreaks = 10, breaks = "pretty",
max.plot = if(is.null(n <- options("sf_max.plot")[[1]])) 9 else n,
key.pos = get_key_pos(x, ...),
key.widths = 0.1111, # new parameter
key.size = .6666, # keep "key.size" instead of "key.heights" because it's called by another functions
reset = TRUE) {
# original code of plot.sf until line 203 (show above), so I change the layout matrix
switch(key.pos,
layout(matrix(c(2,2,2,0,1,0), nrow = 2, ncol = 3, byrow = T),
widths = c((1-key.size)/2, key.size,(1-key.size)/2),
heights = c(1, key.widths)), # 1 bottom
layout(matrix(c(0,1,0,2,2,2), nrow = 3, ncol = 2, byrow = F),
widths = c(key.widths, 1),
heights = c((1-key.size)/2, key.size,(1-key.size)/2)), # 2 left
layout(matrix(c(0,1,0,2,2,2), nrow = 2, ncol = 3, byrow = T),
widths = c((1-key.size)/2, key.size,(1-key.size)/2),
heights = c(key.widths, 1)), # 3 top
layout(matrix(c(2,2,2,0,1,0), nrow = 3, ncol = 2, byrow = F),
widths = c(1, key.widths),
heights = c((1-key.size)/2, key.size,(1-key.size)/2)) # 4 right
)
# remainder of the original code
my_plot.sf
my_plot.sf(nc_sf["AREA"], main = "my_plot.sf", key.pos = 1,
key.size = .5, key.widths = .1666)
sf 0.6-3, submitted to CRAN, has the facilities for doing this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With