showText使在R图中使用各种类型的字体(TrueType,Opentype,1,Web字体等)变得易于使用。开发此软件包的动机是,在R图中使用非标准字体(尤其是对于PDF设备)并不是一件直接的,例如,使用中文字符创建PDF时。这是因为pdf()使用的大多数标准字体不包含汉字字形,并且用户几乎无法在R中使用系统字体。
Winston Chang开发的Extrafont软件包是解决此问题的一个不错的解决方案,该解决方案主要集中在PDF Graphics设备中使用TrueType字体( .ttf )。现在, ShowText能够支持更多的字体格式和更多的图形设备,并避免使用外部软件(例如Ghostscript)。
library( showtext )
# # Loading Google fonts (https://fonts.google.com/)
font_add_google( " Gochi Hand " , " gochi " )
font_add_google( " Schoolbell " , " bell " )
font_add_google( " Covered By Your Grace " , " grace " )
font_add_google( " Rock Salt " , " rock " )
# # Automatically use showtext to render text for future devices
showtext_auto()
# # Tell showtext the resolution of the device,
# # only needed for bitmap graphics. Default is 96
showtext_opts( dpi = 96 )
set.seed( 123 )
x = rnorm( 10 )
y = 1 + x + rnorm( 10 , sd = 0.2 )
y [ 1 ] = 5
mod = lm( y ~ x )
op = par( cex.lab = 2 , cex.axis = 1.5 , cex.main = 2 )
plot( x , y , pch = 16 , col = " steelblue " ,
xlab = " X variable " , ylab = " Y variable " , family = " gochi " )
grid()
title( " Draw Plots Before You Fit A Regression " , family = " bell " )
text( - 0.5 , 4.5 , " This is the outlier " , cex = 2 , col = " steelblue " ,
family = " grace " )
abline(coef( mod ))
abline( 1 , 1 , col = " red " )
par( family = " rock " )
text( 1 , 1 , expression(paste( " True model: " , y == x + 1 )),
cex = 1.5 , col = " red " , srt = 20 )
text( 0 , 2 , expression(paste( " OLS: " , hat( y ) == 0.79 * x + 1.49 )),
cex = 1.5 , srt = 15 )
legend( " topright " , legend = c( " Truth " , " OLS " ), col = c( " red " , " black " ), lty = 1 )
par( op )在此示例中,我们首先加载一些可以通过Google字体在线获得的字体,然后通过调用showtext_auto()函数来告诉R使用ShowText渲染文本。其余部分与通常的绘图命令完全相同。
此示例应适用于大多数图形设备,包括pdf() , png() , postscript()和屏幕上的设备,例如Linux上的windows()和x11() 。
让我首先解释pdf()工作原理。
据我所知,R中的默认PDF设备不会“绘制”文本,而是实际上“描述”了PDF文件中的文本。也就是说,它不是绘制实际字形的线条和曲线,而仅嵌入有关文本的信息,例如它具有哪些字符,使用的字体等。
但是,带有声明字体的文本在不同的OS中的显示方式可能有所不同,这意味着pdf()创建的图的外观是系统依赖的。如果不幸的是,您的系统中没有声明的字体,则可能无法正确看到文本。
相比之下, ShowText软件包试图通过将文本转换为颜色填充的多边形轮廓(用于矢量图形)或栅格图像(用于位图和屏幕上的图形)来解决此问题,从而在所有平台下具有相同的外观。查看此图的人不需要安装创建图形的字体。它为图形制造商和图形查看器提供了便利。
更重要的是, ShowText可以使用系统字体文件,因此您可以在图表中使用您喜欢的字体脸显示文本,只要freetype支持它即可。请参阅下面的部分加载字体。
要使用指定的字体创建图形,您只需执行以下操作:
只有标记(*)的步骤是新的。如果要全球使用ShowText ,则可以一次调用函数showtext_auto() ,然后所有设备将自动使用ShowText呈现文本,如开头显示的示例。
如果要对代码的哪一部分使用ShowText进行更优质的控制,则功能showtext_begin()和showtext_end()将有所帮助。只有这对呼叫包含的绘制函数才会使用ShowText ,而其他呼叫则不使用。例如,要仅更改标题字体,我们可以做:
library( showtext )
font_add_google( " Schoolbell " , " bell " )
# # By default the automatic call of showtext is disabled
# # You can manually turn it off using the line below
# # showtext_auto(enable = FALSE)
# # To use showtext_begin() and showtext_end() you need to
# # explicitly open a graphics device
png( " demo.png " , 700 , 600 , res = 96 )
set.seed( 123 )
x = rnorm( 10 )
y = 1 + x + rnorm( 10 , sd = 0.2 )
y [ 1 ] = 5
mod = lm( y ~ x )
op = par( cex.lab = 1.5 , cex.axis = 1.5 , cex.main = 2 )
plot( x , y , pch = 16 , col = " steelblue " ,
xlab = " X variable " , ylab = " Y variable " )
grid()
# # Use showtext only for this part
showtext_begin()
title( " Draw Plots Before You Fit A Regression " , family = " bell " )
showtext_end()
text( - 0.5 , 4.5 , " This is the outlier " , cex = 2 , col = " steelblue " )
abline(coef( mod ))
abline( 1 , 1 , col = " red " )
text( 1 , 1 , expression(paste( " True model: " , y == x + 1 )),
cex = 1.5 , col = " red " , srt = 20 )
text( 0 , 2 , expression(paste( " OLS: " , hat( y ) == 0.79 * x + 1.49 )),
cex = 1.5 , srt = 15 )
legend( " topright " , legend = c( " Truth " , " OLS " ), col = c( " red " , " black " ), lty = 1 )
par( op )
dev.off()加载字体实际上是由软件包sysfonts完成的。
将字体加载到ShowText中的简便方法是致电font_add(family, regular) ,其中family是您分配给该字体的名称(以后您可以致电par(family = ...)在绘图中使用此字体),而regular是通往字体文件的路径。也就是说,只有知道“字体名称”是不够的,因为它们通常取决于系统。相反,字体文件是实际提供字符字形的实体。
通常,字体文件位于系统中的某些“标准”目录中(例如,在Windows上,通常是C:WindowsFonts )。您可以使用font_paths()检查当前的搜索路径或添加新路径,然后使用font_files()在搜索路径中列出可用的字体文件。 font_files()还列出了其他一些有用的信息,例如您通常用来指定字体的姓氏,以及用于不同变体的字体面。以下是显示我机器上结果的一个示例:
head(font_files())
# # path file family
# # 1 ***/.local/share/fonts Flavors-Regular.ttf Flavors
# # 2 ***/.local/share/fonts FrederickatheGreat-Regular.ttf Fredericka the Great
# # 3 ***/.local/share/fonts GandhiSerif-Bold.otf Gandhi Serif
# # 4 ***/.local/share/fonts GandhiSerif-BoldItalic.otf Gandhi Serif
# # 5 ***/.local/share/fonts GandhiSerif-Italic.otf Gandhi Serif
# # 6 ***/.local/share/fonts GandhiSerif-Regular.otf Gandhi Serif
# #
# # face version ps_name
# # 1 Regular Version 1.001 Flavors-Regular
# # 2 Regular Version 1.001 FrederickatheGreat-Regular
# # 3 Bold Version 1.001 GandhiSerif-Bold
# # 4 Bold Italic Version 1.001 GandhiSerif-BoldItalic
# # 5 Italic Version 1.001 GandhiSerif-Italic
# # 6 Regular Version 1.001 GandhiSerif-Regular下面的代码演示了如何加载和使用Windows上的系统字体:
library( showtext )
# # Add fonts that are available on Windows
font_add( " heiti " , " simhei.ttf " )
font_add( " constan " , " constan.ttf " , italic = " constani.ttf " )
library( ggplot2 )
p = ggplot( NULL , aes( x = 1 , y = 1 )) + ylim( 0.8 , 1.2 ) +
theme( axis.title = element_blank(), axis.ticks = element_blank(),
axis.text = element_blank()) +
annotate( " text " , 1 , 1.1 , family = " heiti " , size = 15 ,
label = " u 4F60 u 597D u FF0C u 4E16 u 754C " ) +
annotate( " text " , 1 , 0.9 , label = ' Chinese for "Hello, world!" ' ,
family = " constan " , fontface = " italic " , size = 12 )
# # Automatically use showtext for new devices
showtext_auto()
# # On-screen device
x11()
print( p )
dev.off()
# # PDF device
pdf( " showtext-example-3.pdf " , 7 , 4 )
print( p )
dev.off()
# # PNG device
ggsave( " showtext-example-4.png " , width = 7 , height = 4 , dpi = 96 )
# # Turn off if no longer needed
showtext_auto( FALSE )对于其他操作系统,您可能没有simhei.ttf字体文件,但是使用其他内容并没有困难。目前, font_add()支持truetype字体(*。ttf/*。ttc)和opentype字体(*。otf),只要freetype支持它,添加新的字体类型是微不足道的。
此外,例如Google字体项目(https://fonts.google.com/),还有许多免费字体可用和可访问。 SysFonts提供了一个接口,可以自动下载并通过函数font_add_google()注册这些字体,如下示例所示。
library( showtext )
font_add_google( " Lobster " , " lobster " )
showtext_auto()
plot( 1 , pch = 16 , cex = 3 )
text( 1 , 1.1 , " A fancy dot " , family = " lobster " , col = " steelblue " , cex = 3 )ShowText包括开源CJK(中文,日语和韩语)字体Wenquanyi Micro Hei。如果您只想在图中显示CJK文本,只需在绘制功能中指定wqy-microhei姓氏即可。
另一个选项是使用以下代码在本地安装Han Sans/Serif字体:
library( showtext )
font_install(source_han_serif())
font_families()
# # [1] "sans" "serif" "mono" "wqy-microhei"
# # [5] "source-han-serif-cn"有关更多详细信息,请参见?font_install和?source_han 。
Every graphics device in R implements some functions to draw specific graphical elements, eg, path() and polygon() to draw polygons, raster() to display bitmap images, text() or textUTF8() to show text, etc. What showtext does is to override their own text rendering functions and replace them by hooks provided in showtext that will further call the device's path() or raster() functions to draw the character glyphs.
仅当您调用showtext_begin()时才完成此操作,并且如果您调用showtext_end()以重新恢复原始设备功能,则不会修改图形设备。
从0.9版开始, ShowText可以与Rstudio Graphics设备(Rstudiogd)配合使用。只需在rstudio会话中调用showtext_auto() ,然后将正确显示图。