我想使用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)
Run Code Online (Sandbox Code Playgroud)
编辑: tmap 包的作者 Martijn Tennekes 向 tm_layout 添加了 10 个参数(因此也添加了 tmap 选项)以允许对此进行控制:(地图)标题、主标题、panel.label 的本地字体和字体系列,图例.标题和图例.文本。
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")
Run Code Online (Sandbox Code Playgroud)
已测试tmap_2.1-1版本。
虽然图例标题tm_polygons可以通过 s 设置为斜体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)
Run Code Online (Sandbox Code Playgroud)