gpt4 book ai didi

r - 这个图例的位置怎么安排

转载 作者:行者123 更新时间:2023-12-05 06:05:35 27 4
gpt4 key购买 nike

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

我使用的代码是

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中找到.谢谢。

enter image description here

最佳答案

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

据我所知,使用tmap 库的tm_facets() 函数无法解决您的问题。因此,我建议使用一种略有不同的“策略”(仍然使用 tmap 库)来获取您要查找的内容。

它分为两个步骤:

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

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

因此,请在下面找到详细说明该方法的代表。

代表

  • 第 1 步 - 构建 map 和图例
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 个元素(即五张 map 和一个图例)的列表(即 Maps_list)。

  • 第 2 步 - 带有图例的 map 马赛克布局
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)

enter image description here

reprex package 创建于 2022-01-25 (v2.0.1)

关于r - 这个图例的位置怎么安排,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65964613/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com