你可以
设置整体字体
首先,根据系统,您需要检查哪些字体可用。当我在 Windows 上运行时,我正在使用以下内容:
install.packages("extrafont")
library(extrafont)
windowsFonts() # check which fonts are available
该theme_set
函数允许您指定 ggplot 的整体主题。因此theme_set(theme_minimal(base_family = "Times New Roman"))
,您可以定义绘图的字体。
使标签继承字体
要使标签继承此文本,我们需要使用两件事:
update_geom_defaults
允许您更新 ggplot 中未来绘图的几何对象样式:http: //ggplot2.tidyverse.org/reference/update_defaults.html
theme_get()$text$family
提取当前全局 ggplot 主题的字体。
通过结合这两者,标签样式可以更新如下:
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
结果
theme_set(theme_minimal(base_family = "Times New Roman"))
# Change the settings
update_geom_defaults("text", list(colour = "grey20", family = theme_get()$text$family))
# Basic Plot
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text()
# works with ggrepel
update_geom_defaults("text_repel", list(colour = "grey20", family = theme_get()$text$family))
library(ggrepel)
ggplot(mtcars, aes(x = mpg,
y = hp,
label = row.names(mtcars))) +
geom_point() +
geom_text_repel()