首页 > 科研绘图 > 【统计分析】R语言统计与绘图:怎么加载Windows系统字体到图形上?
2022
10-15

【统计分析】R语言统计与绘图:怎么加载Windows系统字体到图形上?

前言

作图的时候常出现一个问题,我把 windows 系统字体“ Times New Roman ”指定为图形里的字体,虽然在 RStudio 图形窗口会显示指定字体,但是在保存为 PDF 时出现问题, 出现字体类别错误,指定字体无法显示

library(readxl)

Bar <- read_excel( "Bar.xlsx", col_types = c( "text", "text", "numeric"))

library(ggplot2)

windowsFonts(myFont1 = windowsFont( "Times New Roman")) # 设定文字字体"Times New Roman"

ggplot(Bar,aes(x = group,y = value,fill = Cohort)) +

geom_bar(stat = "identity", position = "dodge", width= 0.5) + # 条形图的位置,柱子宽度

scale_fill_hue(labels = c( "组一", "组二")) +

scale_x_discrete(limits = c( "A", "B", "C", "D", "E")) + # X轴坐标名称

theme(axis.text.x = element_text(size = 10, color = "black", face = "bold", family = "myFont1")) + # X、Y坐标轴文字格式

theme(legend.text = element_text(face= "bold", family= "myFont1", colour= "black",size= 10)) + # 图例字体格式、颜色和大小

theme(legend.position = c( 0.2, 0.9), legend.background = element_blank) # 图例位置和背景

RStudio 图形窗口是这样的:

但是在输出图形时出现错误:

显示字体类别错误

指定字体无法显示

那么这个问题怎么解决呢?

需要用到 sysfonts 和 showtext 两个包。

先说下简单用法:

(*) Load the font # 第一步 加载字体

Open the graphics device # 第二步 打开图形设备

(*) Claim that you want to use showtext to render the text # 第三步 使用showtext渲染字体

Plot # 第四步 绘图命令

Close the device # 第五步 关闭图形设备

具体方法

首先安装这两个包并加载。

install.packages( "sysfonts")

install.packages( "showtext")

library(showtext)

library(sysfonts)

添加系统字体font_add( "myFont1", "timesbd.ttf") # myFont1赋予字体的名称,timesbd.ttf 为 Times New Roman粗体

font_add( "myFont2", "STXINGKA.TTF") # 同上,"STXINGKA.TTF"为华文行楷字体 中文

font.families # 使用此函数查看当前可用的字体名称

导入数据library(readxl)

Bar <- read_excel( "Bar.xlsx", col_types = c( "text", "text", "numeric"))

绘制图形

先打开图形设备,然后将绘图代码放在 showtext_begin 和 showtext_end 之间。

pdf( "out.pdf", width = 7, height = 5) # 打开图形设备

showtext_begin

library(ggplot2)

ggplot(Bar,aes(x = group,y = value,fill = Cohort)) +

geom_bar(stat = "identity", position = "dodge", width= 0.5) + # 条形图的位置,柱子宽度

scale_fill_hue(labels = c( "组一", "组二")) +

scale_x_discrete(limits = c( "A", "B", "C", "D", "E")) + # X轴刻度名称

theme(axis.text.x = element_text(size = 10, color = "black", face = "bold", family = "myFont1")) + # X、Y坐标轴文字格式

theme(legend.text = element_text(face= "bold", family= "myFont2", colour= "black",size= 10)) + # 图例字体格式、颜色和大小

theme(legend.position = c( 0.2, 0.9), legend.background = element_blank) # 图例位置和背景

showtext_end

dev.off # 关闭图形设备

输出图形:

从图上可以看出指定字体已经成功输出到图上了。

或者也可以

showtext_auto # 全局自动使用

library(ggplot2)

ggplot(Bar,aes(x = group,y = value,fill = Cohort)) +

geom_bar(stat = "identity", position = "dodge", width= 0.5) + # 条形图的位置,柱子宽度

scale_fill_hue(labels = c( "组一", "组二")) +

scale_x_discrete(limits = c( "A", "B", "C", "D", "E")) + # X轴刻度名称

theme(axis.text.x = element_text(size = 10, color = "black", face = "bold", family = "myFont1")) + # X、Y坐标轴文字格式

theme(legend.text = element_text(face= "bold", family= "myFont2", colour= "black",size= 10)) + # 图例字体格式、颜色和大小

theme(legend.position = c( 0.2, 0.9), legend.background = element_blank) # 图例位置和背景

windows # 手动打开图形设备

print(p)

ggsave( "out.png", width = 7, height = 5, dpi = 600) # PNG图形设备

showtext_auto( FALSE) # 不在需要就关闭

sysfonts包??sysfonts # 查看帮助文件

sysfonts 包是用来加载系统字体文件的包。

主要函数是 font_add 和 font.add ,这两个函数是等效的,但首选 font_add 函数。

font_add 函数支持的格式包括但不限于 TrueType 字体( .ttf , .ttc )和 OpenType 字体( .otf )。

font_add函数font_add(family, # 字符串,用来命名指定加载字体的名称,可以为任意字符串

regular, # "常规"字体的字体文件路径,必须为字符串且不能省略

bold = NULL, # “粗体”字体的字体文件路径。为NULL,函数将使用"常规"参数的值

italic = NULL, # 同上

bolditalic = NULL, # 同上

symbol = NULL) # 同上

font.add同

举例:加载constan常规斜体字体

font_add( "constan", regular = "constan.ttf", italic = "constani.ttf")

说明: regular 是字体文件的路径,如果字体在系统的标准位置(如 Windows 的 C:WindowsFonts )或当前工作目录,则可以直接输入文件名。

showtext包

在 R 中使用基本字体以外的字体并不是一件简单的事情,特别是 PDF 图形设备中,而 showtext 包就可以使我们更轻松的使用其他字体。

注意: showtext 包目前并不适用于 RStudio 的内置图形设备。建议在原始 R 控制台中运行代码。

如果想在 RStudio 中运行代码则手动打开图形设备,如 x11 和 windows 。

library(showtext) # 加载包

font_add( "myFont1", "timesbd.ttf") # 加载字体

showtext_auto # 全局自动使用showtext渲染字体

windows # 手动打开图形设备

plot

说明:此示例应适用于大多数图形设备,包括 pdf , png , post 和屏幕设备,如 windows 和 x11 。

showtext_begin 和 showtext_end 可以控制在哪一段代码间使用字体,而 showtext_auto 是全局都使用。

End

参考资料:

1. COS主站文章《showtext:字体,好玩的字体和好玩的图》作者 邱怡轩

2. sysfonts包和showtext包帮助文件

最后编辑:
作者:萌小白
一个热爱网络的青年!

发布评论

表情