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() ,然後將正確顯示圖。