R:sf的颜色键的大小

时间:2018-04-23 01:19:29

标签: r plot sf color-key

有一种方法可以调整height的colorkey图例的widthplot.sf参数,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 ?

plot-sf

library(sp)
nc_sp <- as(nc_sf, 'Spatial')
spplot(nc_sp, "AREA", main = "sp",
       colorkey = list(space = "bottom",
                       height = 0.5, width = 1))

sp-plot

编辑

我在第203-211行和

处尝试原始代码plot.R周围的事情
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
  )    

更改widths参数并重新构建包。但是不能工作。

可能是函数.get_layout周围的东西?

重新编辑

我的笨拙解决方案:通过一些调整重建原始功能

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)

my_plot.sf

1 个答案:

答案 0 :(得分:0)

0.6-3,提交给CRAN,有这样的设施。