简体   繁体   English

tmap R-将主题图标题的字体从纯字体更改为斜体,但使图例的字体保持纯字体

[英]tmap R - change font for title of thematic plot from plain to italics but keep font of the legend plain

I would like to change the font for the main title of a thematic plot using the tmap package in R from 'plain' to italics but keep the font for the legend heading and text 'plain'. 我想使用R中tmap包将主题图的主标题的字体从'plain'更改为斜体,但保留图例标题和文本'plain'的字体。

However, when I change the argument fontface in the lm_layout() function it changes the font face of all text in the map. 然而,当我改变参数fontfacelm_layout()函数它改变了地图中的所有文本的字体。 Is it possible to only change the font face for the main title in tmap? 是否可以仅更改tmap中主要标题的字体?

My attempt at a reproducible example (which, unfortunately, changes the font face of all text in the map to italics ) is below: 我尝试一个可复制的示例(不幸的是,该示例将地图中所有文本的字体更改为斜体 )如下:

library(tmap)
data("World")

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

Edit: Martijn Tennekes, the author of the tmap package, has added 10 arguments to tm_layout (and therefore also tmap options) to allow control over this: a local fontface and fontfamily for the (map) title, main title, panel.label, legend.title and legend.text. 编辑:tmap包的作者Martijn Tennekes,向tm_layout(以及tmap选项)添加了10个参数,以便对此进行控制:(地图)标题,主标题,panel.label的本地字体和fontfamily, legend.title和legend.text。

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")

Tested for tmap_2.1-1 version. 已针对tmap_2.1-1版本进行了测试。

While the legend title in tm_polygons could be set to italic via title = expression(italic(your-text)) , the plot title doesn't seem to allow expression s. 虽然传说中的标题tm_polygons可以通过设置为斜体title = expression(italic(your-text))情节标题似乎并没有让expression秒。 One workaround would be to use the editing power of the grid package: 一种解决方法是使用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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM