首页 > 解决方案 > tmap R - 将主题图标题的字体从普通更改为斜体,但保持图例的字体普通

问题描述

我想使用R中的tmap包将主题图主标题的字体从“纯”更改为斜体,但保留图例标题和文本“纯”的字体。

但是,当我更改lm_layout()函数中的参数fontface时,它​​会更改地图中所有文本的字体。是否可以只更改 tmap 中主标题的字体?

我对可重现示例的尝试(不幸的是,它将地图中所有文本的字体更改为斜体)如下:

library(tmap)
data("World")

tm_shape(World) +
tm_polygons("HPI", title = "World - HPI") +
  tm_layout(main.title = "HPI",
            main.title.position = "center",
            fontface = 3) 

编辑: tmap 包的作者 Martijn Tennekes 为 tm_layout 添加了 10 个参数(因此也添加了 tmap 选项)以允许对此进行控制:(地图)标题、主标题、panel.label 的本地字体和字体系列,图例.title 和图例.文本。

tm <- tm_shape(World) +
tm_polygons(c("HPI", "economy"), title = c("Legend 1", "Legend 2")) +
tm_layout(main.title = "Main Title",
          main.title.position = "center",
          title = c("Title 1", "Title 2"),
          panel.labels = c("Panel 1", "Panel 2"))

# global setting
tm + tm_layout(fontface = 3) 

# local setting
tm + tm_layout(main.title.fontface = 1, title.fontface = 2, panel.label.fontface = 3, legend.text.fontface = 4, legend.title.fontfamily = "serif")

标签: rtmap

解决方案


测试tmap_2.1-1版本。

虽然可以通过 将图例标题tm_polygons设置为斜体title = expression(italic(your-text)),但情节标题似乎不允许expressions。一种解决方法是使用grid包的编辑功能:

library(tmap)
library(grid)

data("World")

tm_shape(World) +
  tm_polygons("HPI", title = expression(italic(World - HPI))) + # set legend title to italic
  tm_layout(main.title = "HPI", # unfortunately, does not allow `expression`; try the `grid` hack
            main.title.position = "center") 

# Convert to gTree/list of grobs
g <- grid.grab()
View(g) # check the structure of the gTree; helps with identifying graphical elements
# Edit the fontface of the main title - from 1 (plain text) to 3 (italic); must be integer
g[["children"]][[1]][["children"]][["main_title"]][["children"]][[1]][["gp"]][["font"]] <- 3L
# Draw the edited gTree
grid.newpage(); grid.draw(g)

在此处输入图像描述


推荐阅读