Use Favorite Fonts in R

R for Killing Pneumonia

Nguyen Chi Dung

#====================================
#    Sử dụng và điều chỉnh font
#====================================

# Load thêm các phông chữ (có thể mấy chút thời gian): 
library(extrafont)
font_import()
## Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
## Continue? [y/n]
# Xem qua số lượng font chữ được bổ sung: 
library(tidyverse)
fonts() %>% length()
## [1] 302
# Các font chữ: 
my_font <- fonts()

# Một số font: 
fonts()[1:6]
## [1] "Arial Black"       "Arial"             "Arial Narrow"     
## [4] "Bahnschrift"       "Book Antiqua"      "Bookman Old Style"
# Hoặc chi tiết hơn: 
fonttable() %>% head()
##   package        afmfile                        fontfile          FullName
## 1      NA  ariblk.afm.gz  C:\\Windows\\Fonts\\ariblk.ttf       Arial Black
## 2      NA arialbi.afm.gz C:\\Windows\\Fonts\\arialbi.ttf Arial Bold Italic
## 3      NA arialbd.afm.gz C:\\Windows\\Fonts\\arialbd.ttf        Arial Bold
## 4      NA  ariali.afm.gz  C:\\Windows\\Fonts\\ariali.ttf      Arial Italic
## 5      NA   arial.afm.gz   C:\\Windows\\Fonts\\arial.ttf             Arial
## 6      NA  ARIALN.afm.gz  C:\\Windows\\Fonts\\ARIALN.TTF      Arial Narrow
##     FamilyName           FontName  Bold Italic Symbol afmsymfile
## 1  Arial Black        Arial-Black FALSE  FALSE  FALSE         NA
## 2        Arial Arial-BoldItalicMT  TRUE   TRUE  FALSE         NA
## 3        Arial       Arial-BoldMT  TRUE  FALSE  FALSE         NA
## 4        Arial     Arial-ItalicMT FALSE   TRUE  FALSE         NA
## 5        Arial            ArialMT FALSE  FALSE  FALSE         NA
## 6 Arial Narrow        ArialNarrow FALSE  FALSE  FALSE         NA
#===================================
#  Sử dụng font sau khi đã cài đặt
#===================================


# Nếu sử dụng hệ điều hành Windown cần đăng kí sử dụng font: 
extrafont::loadfonts(device = "win")

# Nêu tên trực tiếp font muốn sử dụng: 
theme_set(theme_minimal())

mtcars %>% 
  ggplot(aes(wt, mpg)) + 
  geom_point() + 
  # Chỉ định font cho tiêu đề chính và cả hai trục: 
  labs(title = "Font Used for This Title: Comic Sans MS") + 
  # Chỉ định font cho Annotation: 
  theme(text = element_text(family = "Comic Sans MS", size = 14)) + 
  annotate("text", 
           x = 4, 
           y = 30, 
           label = "Font Used for This Message: Garamond",
           family = "Garamond", 
           color = "#DC5B44", 
           size = 8)

# Hoặc kiểu khác: 

exp_text <- "italic(y == frac(1, sqrt(2*pi))*e^{-frac(x^2, 2)})"

mtcars %>% 
  ggplot(aes(wt, mpg)) + 
  geom_point() + 
  # Chỉ định font cho tiêu đề chính và cả hai trục: 
  labs(title = "Font Used for This Title: Arial Black") + 
  # Chỉ định font cho Annotation: 
  theme(text = element_text(family = "Arial Black", size = 14, color = "gray35")) + 
  annotate("text", 
           x = 4, 
           y = 30, 
           label = "Font Used for This Message: Garamond",
           family = "Garamond", 
           color = "#DC5B44", 
           size = 8) + 
  # Hiển thị một công thức toán học bằng font CM Roman: 
  annotate("text", 
           x = 4.5,
           y = 22, 
           label = exp_text, 
           parse = TRUE, 
           family = "CM Roman", 
           size = 8, 
           color = "purple")

# Như đã nói chúng ta có 273 font có thể sử dụng. Ví dụ 
# là font thứ 72 là Times New Roman quen thuộc: 

my_font[72]
## [1] "Times New Roman"
# Sử dụng font Times New Roman: 

mtcars %>% 
  ggplot(aes(wt, mpg)) + 
  geom_point() + 
  labs(title = "Font Used for This Title: Times New Roman") + 
  theme(text = element_text(family = my_font[72], size = 14, 
                            color = "gray30", face = "bold")) + 
  annotate("text", 
           x = 4, 
           y = 30, 
           label = "Font Used for This Message: Garamond",
           family = "Garamond", 
           color = "#DC5B44", 
           size = 8) + 
  annotate("text", 
           x = 4.5,
           y = 22, 
           label = exp_text, 
           parse = TRUE, 
           family = "CM Roman", 
           size = 8, 
           color = "purple")

# Một số font khác: 

mtcars %>% 
  ggplot(aes(wt, mpg)) + 
  geom_point() + 
  labs(title = "Font Used for This Title: Georgia") + 
  theme(text = element_text(family = "Georgia", 
                            size = 14, color = "gray30"))

mtcars %>% 
  ggplot(aes(wt, mpg)) + 
  geom_point() + 
  labs(title = "Microsoft Sans Serif") + 
  theme(text = element_text(family = "Microsoft Sans Serif", 
                            size = 14, color = "gray30"))

Sử dụng font từ nguồn bên ngoài

  1. Trước hết download một font yêu thích, ví dụ, từ http://www.megafonts.net/view/officinasanitc-extrabold_96211.

  2. Cài đặt font mới này (đuôi là ttf).

  3. Di chuyển file vừa cài đặt đó vào: (1) thư mục font của Win, (2) thư mục hiện thời mà R đang hoạt động.

  4. Kế tiếp làm những bước như mô tả dưới đây:

# Load font này: 
font_import(pattern = "OfficinaSansITCMedium.ttf", prompt = FALSE)

# Xem font mới cài đặt: 
fonts()
##   [1] "Arial Black"                  "Arial"                       
##   [3] "Arial Narrow"                 "Bahnschrift"                 
##   [5] "Book Antiqua"                 "Bookman Old Style"           
##   [7] "Bookshelf Symbol 7"           "Calibri"                     
##   [9] "Calibri Light"                "Cambria"                     
##  [11] "Candara"                      "VNI-DOS Sample Font "        
##  [13] "Century"                      "Comic Sans MS"               
##  [15] "Consolas"                     "Constantia"                  
##  [17] "Corbel"                       "Courier New"                 
##  [19] "Vni 10 Swan Song"             "Ebrima"                      
##  [21] "Franklin Gothic Medium"       "Gabriola"                    
##  [23] "Gadugi"                       "Garamond"                    
##  [25] "VNI-Garam"                    "Georgia"                     
##  [27] "HoloLens MDL2 Assets"         "Impact"                      
##  [29] "Ink Free"                     "Javanese Text"               
##  [31] "Leelawadee"                   "Leelawadee UI"               
##  [33] "Leelawadee UI Semilight"      "VNI-Linus"                   
##  [35] "Lucida Console"               "Lucida Sans Unicode"         
##  [37] "Malgun Gothic"                "Malgun Gothic Semilight"     
##  [39] "Marlett"                      "Microsoft Himalaya"          
##  [41] "Microsoft Yi Baiti"           "Microsoft New Tai Lue"       
##  [43] "Microsoft PhagsPa"            "Microsoft Sans Serif"        
##  [45] "Microsoft Tai Le"             "Microsoft Uighur"            
##  [47] "Mongolian Baiti"              "Monotype Corsiva"            
##  [49] "MS Outlook"                   "MS Reference Sans Serif"     
##  [51] "MS Reference Specialty"       "MV Boli"                     
##  [53] "Myanmar Text"                 "Nirmala UI"                  
##  [55] "Nirmala UI Semilight"         "Palatino Linotype"           
##  [57] "Segoe MDL2 Assets"            "Segoe Print"                 
##  [59] "Segoe Script"                 "Segoe UI"                    
##  [61] "Segoe UI Light"               "Segoe UI Semibold"           
##  [63] "Segoe UI Semilight"           "Segoe UI Black"              
##  [65] "Segoe UI Emoji"               "Segoe UI Historic"           
##  [67] "Segoe UI Symbol"              "SimSun-ExtB"                 
##  [69] "Sylfaen"                      "Symbol"                      
##  [71] "Tahoma"                       "Times New Roman"             
##  [73] "Trebuchet MS"                 "Verdana"                     
##  [75] "Vinhan"                       "VNI-Vivi"                    
##  [77] ".Vn3DH"                       ".VnArabia"                   
##  [79] ".VnArabiaH"                   ".VnArial"                    
##  [81] ".VnArialH"                    ".VnArial Narrow"             
##  [83] ".VnArial NarrowH"             ".VnAristoteH"                
##  [85] ".VnAristote"                  ".VnAvant"                    
##  [87] ".VnAvantH"                    ".VnBahamasB"                 
##  [89] ".VnBahamasBH"                 ".VnBlack"                    
##  [91] ".VnBlackH"                    ".VnBodoni"                   
##  [93] ".VnBodoniH"                   ".VnBook-Antiqua"             
##  [95] ".VnBook-AntiquaH"             ".VnCentury Schoolbook"       
##  [97] ".VnCentury SchoolbookH"       ".VnClarendonH"               
##  [99] ".VnClarendon"                 ".VnCommercial ScriptH"       
## [101] ".VnCommercial Script"         ".VnCooperH"                  
## [103] ".VnCooper"                    ".VnCourier New"              
## [105] ".VnCourier NewH"              ".VnCourier"                  
## [107] ".VnExoticH"                   ".VnExotic"                   
## [109] ".VnFreeH"                     ".VnFree"                     
## [111] ".VnGothicH"                   ".VnGothic"                   
## [113] ".VnHelvetInsH"                ".VnHelvetIns"                
## [115] "VNI-Allegie"                  "VNI-Aptima"                  
## [117] "VNI-Ariston"                  "VNI-Auchon"                  
## [119] "VNI-Avo"                      "VNI-Awchon"                  
## [121] "VNI-Aztek"                    "VNI-Bandit"                  
## [123] "VNI-Baybuom"                  "VNI-Bodon"                   
## [125] "VNI-Bodon-Poster"             "VNI-Book"                    
## [127] "VNI-Briquet"                  "VNI-Broad"                   
## [129] "VNI-Brush"                    "VNI-Butlong"                 
## [131] "VNI-Centur"                   "VNI-Commerce"                
## [133] "VNI-Cooper"                   "VNI-Coronet"                 
## [135] "VNI-Couri"                    "VNI-Disney"                  
## [137] "VNI-Diudang"                  "VNI-Dom"                     
## [139] "VNI-Duff"                     "VNI-Dur"                     
## [141] "VNI-Fato"                     "VNI-Franko"                  
## [143] "VNI-Free"                     "VNI-Goudy"                   
## [145] "VNI-Helve"                    "VNI-Helve-Condense"          
## [147] "VNI-Hobo"                     "VNI-Internet Mail"           
## [149] "VNI-Jamai"                    "VNI-Juni"                    
## [151] "VNI-Korin"                    "VNI-Kun"                     
## [153] "VNI-Lithos"                   "VNI-Maria"                   
## [155] "VNI-Matisse"                  "VNI-Meli"                    
## [157] "VNI-Murray"                   "VNI-Netbut"                  
## [159] "VNI-Nhatban"                  "VNI-OngDoHL"                 
## [161] "VNI-Palatin"                  "VNI-Park"                    
## [163] "VNI-Present"                  "VNI-Revue"                   
## [165] "VNI-Script"                   "VNI-Silver"                  
## [167] "VNI-Slogan"                   "VNI-Souvir"                  
## [169] "VNI-Swiss-Condense"           "VNI-Tekon"                   
## [171] "VNI-Thanhcao"                 "VNI-Thufap1"                 
## [173] "VNI-Thufap2"                  "VNI-Thufap3"                 
## [175] "VNI-Thufapfan"                "VNI-WIN Sample Font"         
## [177] "VNI-Times"                    "VNI-Top"                     
## [179] "VNI-Truck"                    "VNI-Trung Kien"              
## [181] "VNI-Tubes"                    "VNI-Univer"                  
## [183] "VNI-Vari"                     "VNI-Viettay"                 
## [185] "VNI-Whimsy"                   "VNI-Zap"                     
## [187] "Vni 07 WaterBrushROB"         "Vni 01 LinotypeZapfino one"  
## [189] "Vni 05 SpringtimeFlourish"    "VNI 22 JackieO"              
## [191] "Vni 03 LinotypeZapfino Three" "Vni 04 LinotypeZapfino four" 
## [193] "Vni 23 Qwigley"               "Vni 02 LinotypeZapfino two"  
## [195] "VNI 24 Love"                  "Vni 17 Sandy"                
## [197] "Vni 21 Scrap Cursive"         "Vni 25 Ambiance BT Swash"    
## [199] "VNI 26 Saliere"               "VNI 27 Bendigo"              
## [201] "VNI 28 Zirkon"                "Vni 13 Annabelle"            
## [203] "Vni 14 AlexBrush"             "Vni 16 Machina"              
## [205] "Vni 11 Springtime2"           "VNI 06 Springtime"           
## [207] "VNI 08 Springtime2"           "Vni 12 Alex"                 
## [209] "VNI 09 Baroque "              "Vni 29 BrushMe"              
## [211] "VNI Cambodia"                 "VNI 15 Chops"                
## [213] "VNI Greece"                   "VNI-Harrington"              
## [215] "VNI Laos"                     "Vni 18 Mandalay"             
## [217] "VNI Russia"                   "Vni 30 Shishoni Brush"       
## [219] "Vni 20 University"            "Vni 19 Walt Disney "         
## [221] ".VnKoala"                     ".VnKoalaH"                   
## [223] ".VnLincoln"                   ".VnLincolnH"                 
## [225] ".VnLinus"                     ".VnLinusH"                   
## [227] ".VnLucida sans"               ".VnMemorandum"               
## [229] ".VnMemorandumH"               ".VnMonotype corsivaH"        
## [231] ".VnMonotype corsiva"          ".VnMystical"                 
## [233] ".VnMysticalH"                 ".VnParkH"                    
## [235] ".VnPark"                      ".VnPresentH"                 
## [237] ".VnPresent"                   ".VnRevueH"                   
## [239] ".VnRevue"                     ".VnShelley Allegro"          
## [241] ".VnSouthern"                  ".VnSouthernH"                
## [243] ".VnStamp"                     ".VnTeknical"                 
## [245] ".VnTeknicalH"                 ".VnTifani HeavyH"            
## [247] ".VnTifani Heavy"              ".VnTime"                     
## [249] ".VnTimeH"                     ".VnUniverseH"                
## [251] ".VnUniverse"                  ".VnVogueH"                   
## [253] ".VnVogue"                     "Webdings"                    
## [255] "Wingdings"                    "Wingdings 2"                 
## [257] "Wingdings 3"                  "M+ 1c black"                 
## [259] "M+ 1c bold"                   "M+ 1c heavy"                 
## [261] "M+ 1c light"                  "M+ 1c medium"                
## [263] "M+ 1c regular"                "M+ 1c thin"                  
## [265] "M+ 1m bold"                   "M+ 1m light"                 
## [267] "M+ 1m medium"                 "M+ 1m regular"               
## [269] "M+ 1m thin"                   "M+ 1mn bold"                 
## [271] "M+ 1mn light"                 "M+ 1mn medium"               
## [273] "M+ 1mn regular"               "M+ 1mn thin"                 
## [275] "M+ 1p black"                  "M+ 1p bold"                  
## [277] "M+ 1p heavy"                  "M+ 1p light"                 
## [279] "M+ 1p medium"                 "M+ 1p regular"               
## [281] "M+ 1p thin"                   "M+ 2c black"                 
## [283] "M+ 2c bold"                   "M+ 2c heavy"                 
## [285] "M+ 2c light"                  "M+ 2c medium"                
## [287] "M+ 2c regular"                "M+ 2c thin"                  
## [289] "M+ 2m bold"                   "M+ 2m light"                 
## [291] "M+ 2m medium"                 "M+ 2m regular"               
## [293] "M+ 2m thin"                   "M+ 2p black"                 
## [295] "M+ 2p bold"                   "M+ 2p heavy"                 
## [297] "M+ 2p light"                  "M+ 2p medium"                
## [299] "M+ 2p regular"                "M+ 2p thin"                  
## [301] "Facebook Letter Faces"        "OfficinaSansITC"
# Tiếp tục lệnh bắt buộc sau: 
extrafont::loadfonts(device = "win")

# Sử dụng font này: 
ggplot(mtcars, aes(x = wt, y = mpg)) + 
  geom_point() +
  ggtitle("Font Used: OfficinaSansITC") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text = element_text(family = "OfficinaSansITC", size = 16))

Tham Khảo

  1. https://github.com/wch/extrafont

  2. https://github.com/yixuan/showtext

  3. https://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2

  4. https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html

  5. https://www.r-bloggers.com/using-system-fonts-in-r-graphs/

  6. https://rpubs.com/gbonafe/ggplot2-font

  7. https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/

  8. http://www.cookbook-r.com/Graphs/Fonts/

  9. https://www.r-bloggers.com/change-fonts-in-ggplot2-and-create-xkcd-style-graphs/

  10. https://nsaunders.wordpress.com/2017/09/08/infographic-style-charts-using-the-r-waffle-package/

  11. http://t-redactyl.io/blog/2016/03/creating-plots-in-r-using-ggplot2-part-9-function-plots.html

  12. https://journal.r-project.org/archive/2015/RJ-2015-008/RJ-2015-008.pdf