【问题标题】:Is there any way to customize legend histogram using tm_layout有没有办法使用 tm_layout 自定义图例直方图
【发布时间】:2021-03-11 06:34:19
【问题描述】:

请看下面我使用tmap 包绘制的地图。我没有找到任何可用于自定义直方图图例字体的参数。从下面的代码可以看出我已经设置了legend.text.fontface = 'bold'。但是,这不起作用。

psp1 <-   tm_shape(province) + 
  tm_borders(col = 'black') + 
  tm_shape(county) + 
  tm_polygons(col = '+1 °C', title = 'Changes in %', style = 'pretty', aes.palette = 'div', n=5, legend.hist = T) + 
  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")),
    legend.outside = T, legend.outside.position = 'bottom',
    legend.hist.width = 1,
    legend.hist.height = 0.5,
    legend.stack = 'horizontal',
    legend.title.fontface = 'bold',
    legend.text.fontface = 'bold')

【问题讨论】:

  • 嗨@Yabin Da,请在下面找到一种可能的方法来加粗直方图的标签......希望它仍然有用。干杯。

标签: r tmap


【解决方案1】:

非常有趣的问题。实际上,使用legend.text.fontface = 'bold' 更改直方图标签的字体似乎是不可能的

希望可以使用 base Rgrid 更改此设置,tmap 库所基于的库。

所以,请在下面找到一种可能的解决方案来满足您的要求(希望这个答案不会来得太晚,并且它仍然对您有用)

其他 Stackoverflow 用户的初步说明:要正确运行下面的 reprex,您需要首先下载 OP 在此 post 中提供的数据。

Reprex

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


setwd("Add the path to your working directory")

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


# Split the 'sf' object 'county' into a list of five 'sf' objects 
county_warm_list <- split(county , f = county$warming)

# Build the map with the legend
psp1 <- tm_shape(province) + 
  tm_borders(col = 'black') + 
  tm_shape(st_sf(county_warm_list[[3]])) +  # using the scenario +3°C
  tm_polygons(col = 'estimate', 
              title = 'Changes in %', 
              style = 'pretty', 
              aes.palette = 'div', 
              n=5, 
              legend.hist = TRUE, 
              midpoint = 0) + 
  tm_compass(north = 0, 
             type = 'arrow', 
             show.labels =0, 
             position = c('right','top')) + 
  tm_layout(legend.show = TRUE,
            legend.format = list(fun = function(x) formatC(x, digits = 1, format = "f")),
            legend.outside = TRUE, 
            legend.outside.position = 'bottom',
            legend.hist.width = 1,
            legend.hist.height = 0.5,
            legend.stack = 'horizontal',
            legend.title.fontface = 'bold',
            legend.text.fontface = 'bold')
  • 第 2 步 - 将图例中的所有标签加粗(即包括直方图中的标签)
library(grid)

# Convert the 'tmap' object psp1 into a 'grob' object ('grob' = 'grid graphical object')
psp1 <- tmap_grob(psp1)


# Find the name of the element we want to change using 'grid.list()' which
# returns a listing of 'grobs' (including gTree)
grid.ls(psp1)
#> GRID.gTree.41
#>   multiple_1
#>     BG
#>       mapBG
#>       mapElements
#>         GRID.gTree.11
#>           tm_polygons_1_2
#>         GRID.gTree.12
#>           tm_polygons_1_3
#>       GRID.rect.13
#>       meta_with_bg
#>         meta
#>           GRID.gTree.16
#>             GRID.gTree.15
#>               compass
#>                 GRID.polygon.14
#>   outside_legend                 !!!! "outside_legend" element !!!!
#>     meta_with_bg
#>       meta
#>         legend
#>           GRID.rect.39
#>           GRID.gTree.40
#>             GRID.gTree.19
#>               GRID.gTree.18
#>                 GRID.text.17
#>             GRID.gTree.23
#>               GRID.gTree.22
#>                 GRID.rect.20
#>                 GRID.text.21
#>             GRID.gTree.38
#>               GRID.gTree.37
#>                 GRID.gTree.36
#>                   GRID.gTree.25
#>                     GRID.rect.24
#>                   GRID.gTree.27
#>                     GRID.polyline.26
#>                   GRID.gTree.29
#>                     GRID.text.28
#>                   GRID.gTree.33
#>                     GRID.gTree.30
#>                       GRID.lines.31
#>                       GRID.polyline.32
#>                   GRID.gTree.35
#>                     GRID.text.34

在上面的grob 对象列表中,您可以看到一个名为"outside_legend" 的元素。因此,我们将其修改为将图例的字体加粗:

# Edit the 'outside_legend' element of the 'grob' object 'psp1' using  
# 'editGrob()' and save it in the new 'grob' object 'my_map'
my_map <- editGrob(psp1, gPath("outside_legend"), gp = gpar(fontface = "bold"))


# Draw the 'grob' object 'my_map' 
# !!!! NB: may take a few seconds to be displayed in the graphic device !!!!
grid.draw(my_map)

  • 第 3 步 - 手动或编程保存地图 (在后一种情况下,您需要安装rstudioapi 库)
rstudioapi::savePlotAsImage(
  "my_map.png",   # add the path if different of the working directory
  format = "png", # other possible formats: "jpeg", "bmp", "tiff", "emf", "svg", "eps"
  width = 670,
  height = 710
)

就是这样:-)

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

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 2022-01-08
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多