1 引言

R语言默认可以自由使用的字体只有sans(系统默认), serifmono三种,如果想要使用中文字体或者特定英文等其他字体,就需要借助第三方工具包。这里主要介绍{showtext}和{extrafont}。

2 操作程序

2.1 showtext包

优点:

  • 可以使用.ttc等格式文件
  • 可以使用ggsave保存pdf文件
  • 生成离线图片和PDF时适用

缺点:

  • 不能在Rmarkdown网页生成的图片中直接显示字体

2.1.1 安装加载相关包

#install.packages("showtext")
options(tidyverse.quiet = TRUE)
library(tidyverse) # Easily Install and Load the 'Tidyverse'
library(showtext) # Using Fonts More Easily in R Graphs
showtext_auto() 

以下为常用命令

showtext_auto() # 自动使用字体

font_families() # 必要时查看可供使用的字体

2.1.2 逐条加载字体

一般非管理员用户安装的字体在当前用户文档目录,字体目录需指定

# 增加字体所在路径
font_paths()  # 默认一般为C:/Windows/Fonts
## [1] "C:\\Windows\\Fonts"
# 可指定并添加自定义字体路径
font_path <- "C:\\Users\\ss3\\AppData\\Local\\Microsoft\\Windows\\Fonts\\" %>% 
  font_paths()
# 查看字体目录中全部支持可用的字体文件
fonts <- font_files() %>% as_tibble() # 其中包括path、file、family、face、version、ps_name
# 可以逐一加载字体
font_add("Microsoft YaHei", "msyh.ttc") # 字体文件在默认路径。
font_add("Helvetica", 
         "  C:/Users/ss3/AppData/Local/Microsoft/Windows/Fonts/Helvetica.ttf")
## 或者以自定义函数加载
path <- function(x) {
  font_path <- "C:\\Users\\ss3\\AppData\\Local\\Microsoft\\Windows\\Fonts\\"
  paste0(font_path, x)
}
font_add('Helvetica', 
         path("Helvetica.ttf"),
         bold = path("Helvetica-Black-SemiBold.ttf"),
         italic = path('Helvetica-Light-Light-Italic.ttf'))
font_add('xkcd1', path("xkcd.ttf"))
font_add("fzxbs", path("FZXBSJW.ttf"))
font_add("Georgia", "Georgia.ttf")
font_add("Helvetica Condensed", path("Helvetica Condensed.ttf"))

2.1.3 批量加载字体

### 加载常用中文字体
chinese <- font_files()%>%
  as_tibble() %>% 
  filter(str_detect(family, pattern = "^(FZ)|(Sim)|(ST)") |
           family == "WenQuanYi Micro Hei") %>% 
  distinct() %>% 
  mutate(path_file = paste0(path, "/", file))
map2(chinese$family, chinese$path_file, font_add)
 

### 加载常用英文字体
English <- font_files() %>% 
  as_tibble() %>% 
  filter(path == 'C:/Users/ss3/AppData/Local/Microsoft/Windows/Fonts' |
           family == "Georgia") %>% 
  mutate(path_file = paste0(path, "/", file))
map2(English$family, English$path_file, font_add)
font_families() 
##  [1] "sans"                            "serif"                          
##  [3] "mono"                            "wqy-microhei"                   
##  [5] "Microsoft YaHei"                 "Helvetica"                      
##  [7] "xkcd1"                           "fzxbs"                          
##  [9] "Georgia"                         "Helvetica Condensed"            
## [11] "FZHei-B01"                       "FZXiaoBiaoSong-B05S"            
## [13] "WenQuanYi Micro Hei"             "FZKai-Z03"                      
## [15] "FZKaiS-Extended(SIP)"            "FZKaiS-Extended"                
## [17] "FZShuTi"                         "FZYaoTi"                        
## [19] "SimHei"                          "SimSun"                         
## [21] "SimSun-ExtB"                     "STCaiyun"                       
## [23] "STFangsong"                      "STHupo"                         
## [25] "STKaiti"                         "STLiti"                         
## [27] "STSong"                          "STXihei"                        
## [29] "STXingkai"                       "STXinwei"                       
## [31] "STZhongsong"                     "Facebook Letter Faces"          
## [33] "Helvetica-Black-SemiBold"        "Helvetica-BoldOblique"          
## [35] "Helvetica-Condensed-Black-Se"    "Helvetica-Condensed-Light-Li"   
## [37] "Helvetica-Condensed-Light-Light" "Helvetica-Condensed-Thin"       
## [39] "Helvetica-Conth"                 "Helvetica-Light-Light-Italic"   
## [41] "Helvetica-Narrow-Bold"           "Helvetica-Narrow-BoldOblique"   
## [43] "Helvetica-Narrow"                "Helvetica-Roman-SemiB"          
## [45] "Helvetica-Narrow-Bold Lefty"     "Helvetica Narrow"               
## [47] "Helvetica-Narrow Lefty"          "HelveticaExt-Normal"            
## [49] "HelveticaExtObl-Heavy"           "HelveticaExtObl-Light"          
## [51] "HelveticaExtObl-Normal"          "HelveticaInserat-Roman-SemiB"   
## [53] "HelveticaInserat-Roman-SemiBold" "HelveticaObl-Heavy"             
## [55] "HelveticaObl-Thin"               "Montserrat Black"               
## [57] "Montserrat"                      "Montserrat ExtraBold"           
## [59] "Montserrat ExtraLight"           "Montserrat Light"               
## [61] "Montserrat Medium"               "Montserrat SemiBold"            
## [63] "Montserrat Thin"                 "Roboto Condensed"               
## [65] "Titillium Web"                   "Titillium Web Light"            
## [67] "wmpeople1"                       "xkcd"

2.1.4 字体应用演示

#case1
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars",
          subtitle = "32种汽车的燃油效率") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=10, family="Microsoft YaHei"),
        plot.title = element_text(size = 12, family="Microsoft YaHei"),
        plot.subtitle = element_text(size = 16,    family="Microsoft YaHei"))

#case2
# 字体使用演示2
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars",
          subtitle = "32种汽车的燃油效率") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size = 15, family="Montserrat Light"),
        plot.title = element_text(size = 18, family="Titillium Web"),
        plot.subtitle = element_text(size = 20, family="WenQuanYi Micro Hei"))

2.1.5 图像另存图片和pdf

ggsave("fonttest-win1.tiff", width = 7, height = 7)
ggsave("fonttest-win1.png", width = 7, height = 7)
ggsave("fonttest-win1.bmp", width = 7, height = 7)
ggsave('fonttest-win1.pdf', width = 7, height = 7)

注:目前存在以下问题,研究中……

  • Plots区有时字体不能即时更新显示(作者本人在说明文件里确认目前不支持Rstudio内置图形设备),但保存图片和pdf中显示正常。不建议生成Rmarkdown网页时使用。

  • 有时仍然会出现warnings,但plot中字体显示正常

2.2 extrafont包

优点:

  • 可以在Rmarkdown网页生成的图片中直接显示字体
  • 生成在线图片时适用

缺点:

  • 不能用ggsave保存生成Pdf文件
#install.packages('extrafont')
options(tidyverse.quiet = TRUE)
library(tidyverse)

library(extrafont)

2.2.1 导入字体

先导入字体,目前仅支持TrueType字体。这个操作在第一次运行或新安装字体后实施一次。

# 会默认把C:\windows\Fonts目录里符合格式的字体导入
font_import()
# 如果您是在非管理员帐户安排的字体,那么需要指定其安装位置。此步操作会叠加以前导入的字体,形成合集。
font_import(paths = "C:\\Users\\ss3\\AppData\\Local\\Microsoft\\Windows\\Fonts")

2.2.2 查看字体

装好字体后,可以查看字体family名称。

fonts()
##   [1] "Agency FB"                       "Algerian"                       
##   [3] "Arial Black"                     "Arial"                          
##   [5] "Arial Narrow"                    "Arial Rounded MT Bold"          
##   [7] "Arial Unicode MS"                "Bahnschrift"                    
##   [9] "Baskerville Old Face"            "Bauhaus 93"                     
##  [11] "Bell MT"                         "Berlin Sans FB"                 
##  [13] "Berlin Sans FB Demi"             "Bernard MT Condensed"           
##  [15] "Blackadder ITC"                  "Bodoni MT"                      
##  [17] "Bodoni MT Black"                 "Bodoni MT Condensed"            
##  [19] "Bodoni MT Poster Compressed"     "Book Antiqua"                   
##  [21] "Bookman Old Style"               "Bookshelf Symbol 7"             
##  [23] "Bradley Hand ITC"                "Britannic Bold"                 
##  [25] "Broadway"                        "Brush Script MT"                
##  [27] "Calibri"                         "Calibri Light"                  
##  [29] "Californian FB"                  "Calisto MT"                     
##  [31] "Cambria"                         "Candara"                        
##  [33] "Candara Light"                   "Castellar"                      
##  [35] "Centaur"                         "Century"                        
##  [37] "Century Gothic"                  "Century Schoolbook"             
##  [39] "Cera PROModern Medium"           "Chiller"                        
##  [41] "Colonna MT"                      "Comic Sans MS"                  
##  [43] "Consolas"                        "Constantia"                     
##  [45] "Cooper Black"                    "Copperplate Gothic Bold"        
##  [47] "Copperplate Gothic Light"        "Corbel"                         
##  [49] "Corbel Light"                    "Courier New"                    
##  [51] "Curlz MT"                        "DengXian"                       
##  [53] "DengXian Light"                  "Ebrima"                         
##  [55] "Edwardian Script ITC"            "Elephant"                       
##  [57] "Engravers MT"                    "Eras Bold ITC"                  
##  [59] "Eras Demi ITC"                   "Eras Light ITC"                 
##  [61] "Eras Medium ITC"                 "ESRI AMFM Electric"             
##  [63] "ESRI AMFM Gas"                   "ESRI AMFM Sewer"                
##  [65] "ESRI AMFM Water"                 "ESRI ArcPad"                    
##  [67] "ESRI Arrowhead"                  "ESRI Business"                  
##  [69] "ESRI Cartography"                "ESRI Caves 1"                   
##  [71] "ESRI Caves 2"                    "ESRI Caves 3"                   
##  [73] "ESRI Climate & Precipitation"    "ESRI Commodities"               
##  [75] "ESRI Conservation"               "ESRI Crime Analysis"            
##  [77] "ESRI Default Marker"             "ESRI Dimensioning"              
##  [79] "ESRI Elements"                   "ESRI Enviro Hazard Analysis"    
##  [81] "ESRI Enviro Hazard Incident"     "ESRI Enviro Hazard Sites"       
##  [83] "ESRI Environmental & Icons"      "ESRI ERS Infrastructures S1"    
##  [85] "ESRI ERS Operations S1"          "ESRI Fire Incident NFPA"        
##  [87] "ESRI Geology"                    "ESRI Geology AGSO 1"            
##  [89] "ESRI Geology USGS 95-525"        "ESRI Geometric Symbols"         
##  [91] "ESRI Hazardous Materials"        "ESRI Hydrants"                  
##  [93] "ESRI IGL Font16"                 "ESRI IGL Font20"                
##  [95] "ESRI IGL Font21"                 "ESRI IGL Font22"                
##  [97] "ESRI IGL Font23"                 "ESRI IGL Font24"                
##  [99] "ESRI IGL Font25"                 "ESRI Meteorological 01"         
## [101] "ESRI Mil2525C Modifiers"         "ESRI MilMod 01"                 
## [103] "ESRI MilMod 02"                  "ESRI MilRed 01"                 
## [105] "ESRI MilSym 01"                  "ESRI MilSym 02"                 
## [107] "ESRI MilSym 03"                  "ESRI MilSym 04"                 
## [109] "ESRI MilSym 05"                  "ESRI NIMA City Graphic LN"      
## [111] "ESRI NIMA City Graphic PT"       "ESRI NIMA DNC LN"               
## [113] "ESRI NIMA DNC PT"                "ESRI NIMA VMAP1&2 LN"           
## [115] "ESRI NIMA VMAP1&2 PT"            "ESRI North"                     
## [117] "ESRI Oil, Gas, & Water"          "ESRI Ordnance Survey"           
## [119] "ESRI Pipeline US 1"              "ESRI Public1"                   
## [121] "ESRI SDS 1.95 1"                 "ESRI SDS 1.95 2"                
## [123] "ESRI SDS 2.00 1"                 "ESRI SDS 2.00 2"                
## [125] "ESRI Shields"                    "ESRI Surveyor"                  
## [127] "ESRI Telecom"                    "ESRI Transportation & Civic"    
## [129] "ESRI US Forestry 1"              "ESRI US MUTCD 1"                
## [131] "ESRI US MUTCD 2"                 "ESRI US MUTCD 3"                
## [133] "ESRI Weather"                    "FangSong"                       
## [135] "Felix Titling"                   "Footlight MT Light"             
## [137] "Forte"                           "Franklin Gothic Book"           
## [139] "Franklin Gothic Demi"            "Franklin Gothic Demi Cond"      
## [141] "Franklin Gothic Heavy"           "Franklin Gothic Medium"         
## [143] "Franklin Gothic Medium Cond"     "Freestyle Script"               
## [145] "French Script MT"                "FZKaiS-Extended[SIP]"           
## [147] "FZKaiS-Extended"                 "FZKai-Z03"                      
## [149] "FZShuTi"                         "FZYaoTi"                        
## [151] "Gabriola"                        "Gadugi"                         
## [153] "Garamond"                        "Georgia"                        
## [155] "Gigi"                            "Gill Sans Ultra Bold"           
## [157] "Gill Sans Ultra Bold Condensed"  "Gill Sans MT"                   
## [159] "Gill Sans MT Condensed"          "Gill Sans MT Ext Condensed Bold"
## [161] "Gloucester MT Extra Condensed"   "Goudy Old Style"                
## [163] "Goudy Stout"                     "Haettenschweiler"               
## [165] "Harlow Solid Italic"             "Harrington"                     
## [167] "High Tower Text"                 "HoloLens MDL2 Assets"           
## [169] "Impact"                          "Imprint MT Shadow"              
## [171] "Informal Roman"                  "Ink Free"                       
## [173] "Javanese Text"                   "Jokerman"                       
## [175] "Juice ITC"                       "KaiTi"                          
## [177] "Kristen ITC"                     "Kunstler Script"                
## [179] "Wide Latin"                      "Leelawadee UI"                  
## [181] "Leelawadee UI Semilight"         "LiSu"                           
## [183] "Lucida Bright"                   "Lucida Calligraphy"             
## [185] "Lucida Console"                  "Lucida Fax"                     
## [187] "Lucida Handwriting"              "Lucida Sans"                    
## [189] "Lucida Sans Typewriter"          "Lucida Sans Unicode"            
## [191] "Magneto"                         "Maiandra GD"                    
## [193] "Malgun Gothic"                   "Malgun Gothic Semilight"        
## [195] "Marlett"                         "Matura MT Script Capitals"      
## [197] "Microsoft Himalaya"              "Microsoft Yi Baiti"             
## [199] "Microsoft New Tai Lue"           "Microsoft PhagsPa"              
## [201] "Microsoft Sans Serif"            "Microsoft Tai Le"               
## [203] "Mistral"                         "Modern No. 20"                  
## [205] "Mongolian Baiti"                 "Monotype Corsiva"               
## [207] "MS Outlook"                      "MS Reference Sans Serif"        
## [209] "MS Reference Specialty"          "MV Boli"                        
## [211] "Myanmar Text"                    "Niagara Engraved"               
## [213] "Niagara Solid"                   "Nirmala UI"                     
## [215] "Nirmala UI Semilight"            "OCR A Extended"                 
## [217] "Old English Text MT"             "Onyx"                           
## [219] "Palace Script MT"                "Palatino Linotype"              
## [221] "Papyrus"                         "Parchment"                      
## [223] "Perpetua"                        "Perpetua Titling MT"            
## [225] "Playbill"                        "Poor Richard"                   
## [227] "Pristina"                        "Rage Italic"                    
## [229] "Ravie"                           "Rockwell"                       
## [231] "Rockwell Condensed"              "Rockwell Extra Bold"            
## [233] "Script MT Bold"                  "Segoe MDL2 Assets"              
## [235] "Segoe Print"                     "Segoe Script"                   
## [237] "Segoe UI"                        "Segoe UI Light"                 
## [239] "Segoe UI Semibold"               "Segoe UI Semilight"             
## [241] "Segoe UI Black"                  "Segoe UI Emoji"                 
## [243] "Segoe UI Historic"               "Segoe UI Symbol"                
## [245] "Showcard Gothic"                 "SimHei"                         
## [247] "SimSun-ExtB"                     "Snap ITC"                       
## [249] "STCaiyun"                        "Stencil"                        
## [251] "STFangsong"                      "STHupo"                         
## [253] "STKaiti"                         "STLiti"                         
## [255] "STSong"                          "STXihei"                        
## [257] "STXingkai"                       "STXinwei"                       
## [259] "STZhongsong"                     "Sylfaen"                        
## [261] "Symbol"                          "Tahoma"                         
## [263] "Tempus Sans ITC"                 "Times New Roman"                
## [265] "Trebuchet MS"                    "Tw Cen MT"                      
## [267] "Tw Cen MT Condensed"             "Tw Cen MT Condensed Extra Bold" 
## [269] "Verdana"                         "Viner Hand ITC"                 
## [271] "Vivaldi"                         "Vladimir Script"                
## [273] "Webdings"                        "Wingdings"                      
## [275] "Wingdings 2"                     "Wingdings 3"                    
## [277] "YouYuan"                         "ZWAdobeF"                       
## [279] "Facebook Letter Faces"           "FZHei-B01"                      
## [281] "FZXiaoBiaoSong-B05S"             "Montserrat Black"               
## [283] "Montserrat"                      "Montserrat ExtraBold"           
## [285] "Montserrat ExtraLight"           "Montserrat Light"               
## [287] "Montserrat Medium"               "Montserrat SemiBold"            
## [289] "Montserrat Thin"                 "Roboto Condensed"               
## [291] "Titillium Web"                   "Titillium Web Light"            
## [293] "wmpeople1"                       "xkcd"                           
## [295] "Helvetica"                       "Helvetica-Black-SemiBold"       
## [297] "Helvetica-BoldOblique"           "Helvetica Condensed"            
## [299] "Helvetica-Condensed-Black-Se"    "Helvetica-Condensed-Light-Li"   
## [301] "Helvetica-Condensed-Light-Light" "Helvetica-Condensed-Thin"       
## [303] "Helvetica-Conth"                 "Helvetica-Light-Light-Italic"   
## [305] "Helvetica-Narrow"                "Helvetica Narrow"               
## [307] "Helvetica-Narrow-Bold"           "Helvetica-Narrow-BoldOblique"   
## [309] "Helvetica-Roman-SemiB"           "HelveticaExt-Normal"            
## [311] "HelveticaExtObl-Heavy"           "HelveticaExtObl-Light"          
## [313] "HelveticaExtObl-Normal"          "HelveticaInserat-Roman-SemiB"   
## [315] "HelveticaInserat-Roman-SemiBold" "Helvetica-Narrow-Bold Lefty"    
## [317] "Helvetica-Narrow Lefty"          "HelveticaObl-Heavy"             
## [319] "HelveticaObl-Thin"
# 建议生成字体库表格变量,内含字体family名称、位置、字体文件名等信息,以方便查询各字体family后调用
font <- fonttable()

# 字体格式示例
font %>% slice_sample(n = 10)
## # A tibble: 10 x 10
##    package afmfile fontfile FullName FamilyName FontName Bold  Italic Symbol
##    <lgl>   <chr>   <chr>    <chr>    <chr>      <chr>    <lgl> <lgl>  <lgl> 
##  1 NA      ZWAdob~ "C:\\Wi~ ZWAdobeF ZWAdobeF   ZWAdobeF FALSE FALSE  FALSE 
##  2 NA      SCRIPT~ "C:\\Wi~ Script ~ Script MT~ ScriptM~ FALSE FALSE  FALSE 
##  3 NA      esri_3~ "C:\\Wi~ ESRI Di~ ESRI Dime~ ESRIDim~ FALSE FALSE  FALSE 
##  4 NA      segmdl~ "C:\\Wi~ Segoe M~ Segoe MDL~ SegoeMD~ FALSE FALSE  FALSE 
##  5 NA      Montse~ "C:\\Us~ Montser~ Montserra~ Montser~ FALSE FALSE  FALSE 
##  6 NA      ntailu~ "C:\\Wi~ Microso~ Microsoft~ Microso~ FALSE FALSE  FALSE 
##  7 NA      Candar~ "C:\\Wi~ Candara~ Candara    Candara~ FALSE TRUE   FALSE 
##  8 NA      LSANS.~ "C:\\Wi~ Lucida ~ Lucida Sa~ LucidaS~ FALSE FALSE  FALSE 
##  9 NA      STKAIT~ "C:\\Wi~ STKaiti  STKaiti    STKaiti  FALSE FALSE  FALSE 
## 10 NA      Helvet~ "C:\\Us~ Helveti~ Helvetica  Helveti~ TRUE  FALSE  FALSE 
## # ... with 1 more variable: afmsymfile <lgl>
#常用中文字体列表
font %>% filter(str_detect(FontName, pattern = "^(FZ)|(Sim)|(ST)"))
## # A tibble: 21 x 10
##    package afmfile fontfile FullName FamilyName FontName Bold  Italic Symbol
##    <lgl>   <chr>   <chr>    <chr>    <chr>      <chr>    <lgl> <lgl>  <lgl> 
##  1 NA      FZKaiS~ "C:\\Wi~ FZKaiS-~ FZKaiS-Ex~ FZKT--G~ FALSE FALSE  FALSE 
##  2 NA      FZKaiS~ "C:\\Wi~ FZKaiS-~ FZKaiS-Ex~ FZKT--G~ FALSE FALSE  FALSE 
##  3 NA      FZKaiS~ "C:\\Wi~ FZKaiS-~ FZKaiS-Ex~ FZKT--G~ FALSE FALSE  FALSE 
##  4 NA      FZKaiS~ "C:\\Wi~ FZKaiS-~ FZKaiS-Ex~ FZKT--G~ FALSE FALSE  FALSE 
##  5 NA      FZKai_~ "C:\\Wi~ FZKai-Z~ FZKai-Z03  FZKTK--~ FALSE FALSE  FALSE 
##  6 NA      FZSTK.~ "C:\\Wi~ FZShuTi  FZShuTi    FZSTK--~ FALSE FALSE  FALSE 
##  7 NA      FZYTK.~ "C:\\Wi~ FZYaoTi  FZYaoTi    FZYTK--~ FALSE FALSE  FALSE 
##  8 NA      simhei~ "C:\\Wi~ SimHei   SimHei     SimHei   FALSE FALSE  FALSE 
##  9 NA      simsun~ "C:\\Wi~ SimSun-~ SimSun-Ex~ SimSun-~ FALSE FALSE  FALSE 
## 10 NA      STCAIY~ "C:\\Wi~ STCaiyun STCaiyun   STCaiyun FALSE FALSE  FALSE 
## # ... with 11 more rows, and 1 more variable: afmsymfile <lgl>

2.2.3 调用字体

字体导入后,使用前需要向图形设备注册字体。一般需要向win和pdf注册。

loadfonts(device = "win", quiet=TRUE)
loadfonts(device = "pdf", quiet=TRUE)

2.2.4 使用字体

library(ggplot2)
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars",
          subtitle = "32种汽车的燃油效率") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=10, family="Georgia"),
        plot.title = element_text(size = 16, family="Georgia"),
        plot.subtitle = element_text(size = 16,    family="FZXiaoBiaoSong-B05S"))

2.3 保存图像

可以用以下语句保存为图片

ggsave("fonttest-win.tiff", width = 7, height = 7)
ggsave("fonttest-win.png", width = 7, height = 7)
ggsave("fonttest-win.bmp", width = 7, height = 7)

目前仅可以通过Plots区的Export功能导出pdf,研究中……

3 字体展示

3.1 英文字体展示

sentence <- "The quick br8wn fox jumps ov57 9 lazy dog"
family <- fonttable() %>% 
  # as_tibble() %>% 
  filter(str_detect(fontfile, "C:\\\\Users\\\\ss3\\\\AppData\\\\Local\\\\Microsoft\\\\Windows\\\\Fonts")) %>% 
  select(FamilyName) %>% 
  distinct() %>% 
  filter(FamilyName != 'Helvetica Narrow') %>% 
  mutate(group = rep(1:4, each = 10) ) %>% 
  mutate(sentence = sentence) %>% 
  mutate(x = rep(1:10, 4), y = rep(1:10, 4)) %>% 
  as_tibble()

family %>% 
  ggplot(aes(x, y))+
  geom_text(aes(x = 1, y = y, label = FamilyName), family = "Helvetica", size = 3)+
  geom_text(aes(x = 7, y = y, label = sentence, family = FamilyName), size = 3.5 )+
  facet_wrap(~group)+
  scale_x_continuous(limits = c(0, 10))+
  theme_void()

3.2 中文字体展示

character <- c("12457890R语言World强大")
chinese <- fonttable()%>% 
  filter(str_detect(FontName, pattern = "^(FZ)|(Sim)|(ST)")) %>% 
  select(FamilyName) %>% 
  distinct() %>% 
  mutate(group = c(rep(1:3, each = 5), rep(4,4) )) %>% 
  mutate(sentence = character) %>% 
  mutate(x = c(rep(1:5, 3), 1:4) , y = c(rep(1:5, 3), 1:4)) %>% 
  as_tibble()

chinese %>% 
  ggplot(aes(x, y))+
  geom_text(aes(x = 3, y = y*2, label = FamilyName), family = "Helvetica", size = 5)+
  geom_text(aes(x = 8, y = y*2, label = sentence, family = FamilyName), size = 5 )+
  facet_wrap(~group)+
  scale_x_continuous(limits = c(0, 10))+
  theme_void()