这个图例的位置怎么安排

How to arrange the position of this legend

如下图所示,如何将图例放置到网格中的最后一个单元格?

我使用的代码是

psp1 <-   tm_shape(province) + 
  tm_borders(col = 'black') + 
  tm_shape(county) + 
  tm_polygons(col = 'estimate', title = 'Changes in %', style = 'fixed', palette = brewer.pal(n = 6, name = 'Spectral'), 
              breaks = c(-15, -10, -5, 0, 5, 10, 15), legend.hist = F) + 
  tm_facets('warming', ncol = 2) + 
  tm_shape(province) + 
  tm_borders(col = 'black') + 
  tm_compass(north = 0, type = 'arrow', show.labels =0, position = c('right','top')) + 
  tm_layout(legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
            fontface = 'bold',
            legend.text.size = 1.3,
            legend.width = 0.2,
            legend.title.size = 1.5,
            panel.label.size = 1.5,
            panel.label.fontface = 'bold')

数据来自here。谢谢

我的回答可能有点晚了...(在您提出请求后将近一年!)无论如何,您的问题很有趣,我希望这个答案对现在或将来有用项目,为您或其他 SO 用户(顺便说一下,感谢您让您的输入数据可访问一年 ;-))。

据我所知,使用tmap库的tm_facets()函数无法解决您的问题。所以我建议一个稍微不同的“策略”(仍然使用 tmap 库)来获得你正在寻找的东西。

分两步表达:

  1. 手动构建地图和图例...幸运的是,不是完全手动,因为我建议的解决方案使用一个自定义函数(即 make_graph()),即 运行 通过Map() 函数。

  2. 使用 R base grid 库编辑带有图例的地图镶嵌。同样,通过使用一个自定义函数(即 Maps_setup())运行 通过 Map() 函数使实现变得更容易。

所以,请在下面找到详细说明该方法的表达式。

Reprex

  • 第 1 步 - 构建地图和图例
library(sf)
library(tmap)
library(RColorBrewer)

# Import data
province <- st_read("province.shp")
county <- st_read("county.shp")


# Split the 'sf' object 'county' into a list of five 'sf' objects corresponding 
# to the five warming scenarios (i.e. the first five facets of the final figure)
county_warm_list <- split(county , f = county$warming)


# Build the function 'make_graph' to generate the maps
make_graph <- function(x,y){
  
  results <- tm_shape(x, 
                      is.master = TRUE) + 
    tm_polygons(col = 'estimate', 
                title = 'Changes in %', 
                style = 'fixed', 
                palette = brewer.pal(n = 6, name = 'Spectral'), 
                breaks = c(-15, -10, -5, 0, 5, 10, 15), 
                legend.hist = FALSE, 
                midpoint = 0) + 
    tm_shape(province) + 
    tm_borders(col = 'black') + 
    tm_compass(north = 0, 
               type = 'arrow', 
               show.labels = 0, 
               position = c(0.93, 0.87),
               size = 1.2) +
    tm_layout(legend.show = FALSE, 
              # NB: the use of the 'get_asp_ratio()' function enables
              # to optimize the size of each map inside its own facet:
              asp = tmaptools::get_asp_ratio(x), 
              panel.labels = y,
              panel.label.size = 0.8,
              panel.label.fontface = 'bold', 
              inner.margins = c(0.02, 0.02, 0.02, 0.02))
  
  return(results)
  
}


# Run the 'make_graph()' function through the list of the five 'sf' objects (i.e. 
# 'county_warm_list') to generate the maps with their respective title using 
# the 'Map()' function
map_titles <- names(county_warm_list)
Maps_list <- Map(make_graph, county_warm_list, map_titles)


# Build the legend using only the object "county"
Maps_legend <- tm_shape(county) + 
  tm_polygons(col = 'estimate', 
              title = 'Changes in %', 
              style = 'fixed', 
              palette = brewer.pal(n = 6, name = 'Spectral'), 
              breaks = c(-15, -10, -5, 0, 5, 10, 15), 
              legend.hist = FALSE, 
              midpoint = 0) + 
  tm_layout(legend.only = TRUE, 
            legend.position = c("center", "center"), 
            legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
            fontface = 'bold',
            legend.text.size = 1.3,
            legend.width = 0.2,
            legend.title.size = 1.5)



# Add the legend to 'Maps_list'
Maps_list$Legend <- Maps_legend

第一步结束时,您会得到一个包含 6 个元素(即五张地图和一个图例)的列表(即 Maps_list)。

  • 第 2 步 - 带有图例的地图马赛克布局
library(grid)

grid.newpage()

# Build the function 'Maps_setup' to set up the layout
Maps_setup <- function(x,y,z){
  
  pushViewport(viewport(layout = grid.layout(nrow = 3, ncol = 2, 
                                             widths = unit(7.32, "cm"), 
                                             heights = unit(5, "cm"))))
  
  setup <- print(x, vp = viewport(layout.pos.row = y, layout.pos.col = z))
  
  return(setup)
  
}


# Run the 'Maps_setup()' function through the six objects of 'Maps_list' (i.e. 
# 5 maps + 1 legend) to place the maps and the legend on the page using 
# the 'Map()' function

# The 'pos_row' and 'pos_col' vectors are used to indicate where to place the 
# maps as the 'Maps_setup()' function works through the list

pos_row <- rep(1:3, each = 2)
pos_col <- rep(1:2, times = 3)


Final_Results <- Map(Maps_setup, Maps_list, pos_row, pos_col)

reprex package (v2.0.1)

于 2022-01-25 创建