Skip to Content

Fontes no ggplot2

Recentemente consegui usar fontes do windows e do google no R.

Como funciona? Tem apenas dois pacotes: * extrafont * showtext

a função extrafont::loadfonts(device=“win”) carrega todas as fontes do seu computador no R. A função fontes <- fonts() vai mostrar a lista de fontes. Vou usar o ggplot para uma demostração.

#---------------------------------------
#            windows fonts
#---------------------------------------
library(extrafont)
# Só é preciso fazer isso uma única vez 
#extrafont::font_import()

#a função extrafont::font_import() para o R importar todas as fontes disponíveis
extrafont::loadfonts(device="win")

# exemplo de uso
library(ggplot2)
p <-  ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  stat_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE) +
  labs(x = "Peso (ton.)", y = "Milhas por galão", title = "Eficiência e peso de carros",
       subtitle = "Regressão quadrática entre o peso de diferentes carros e sua eficiência energética")
p + geom_point(aes(colour = as.factor(cyl))) +
  theme(axis.text = element_text(family = "Tahoma", face = "italic"),
        axis.title = element_text(family = "Georgia"),
        legend.title = element_text(family = "Bauhaus 93"),
        legend.text = element_text(family = "Verdana"),
        plot.title = element_text(family = "Algerian", size = 20))

nomes = row.names(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)
ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_text(aes(label = nomes, family = c("xkcd", "Bauhaus 93", "Algerian")[cyl]))

fontes <- fonts()
fontes
##   [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] "Chiller"                         "Colonna MT"                     
##  [41] "Comic Sans MS"                   "Consolas"                       
##  [43] "Constantia"                      "Cooper Black"                   
##  [45] "Copperplate Gothic Bold"         "Copperplate Gothic Light"       
##  [47] "Corbel"                          "Corbel Light"                   
##  [49] "Courier New"                     "Curlz MT"                       
##  [51] "Ebrima"                          "Edwardian Script ITC"           
##  [53] "Elephant"                        "Engravers MT"                   
##  [55] "Eras Bold ITC"                   "Eras Demi ITC"                  
##  [57] "Eras Light ITC"                  "Eras Medium ITC"                
##  [59] "Felix Titling"                   "FontAwesome"                    
##  [61] "Footlight MT Light"              "Forte"                          
##  [63] "Franklin Gothic Book"            "Franklin Gothic Demi"           
##  [65] "Franklin Gothic Demi Cond"       "Franklin Gothic Heavy"          
##  [67] "Franklin Gothic Medium"          "Franklin Gothic Medium Cond"    
##  [69] "Freestyle Script"                "French Script MT"               
##  [71] "Gabriola"                        "Gadugi"                         
##  [73] "Garamond"                        "Georgia"                        
##  [75] "Gigi"                            "Gill Sans Ultra Bold"           
##  [77] "Gill Sans Ultra Bold Condensed"  "Gill Sans MT"                   
##  [79] "Gill Sans MT Condensed"          "Gill Sans MT Ext Condensed Bold"
##  [81] "Gloucester MT Extra Condensed"   "Goudy Old Style"                
##  [83] "Goudy Stout"                     "Haettenschweiler"               
##  [85] "Harlow Solid Italic"             "Harrington"                     
##  [87] "High Tower Text"                 "HoloLens MDL2 Assets"           
##  [89] "Impact"                          "Imprint MT Shadow"              
##  [91] "Informal Roman"                  "Ink Free"                       
##  [93] "Javanese Text"                   "Jokerman"                       
##  [95] "Juice ITC"                       "Kristen ITC"                    
##  [97] "Kunstler Script"                 "Wide Latin"                     
##  [99] "Leelawadee UI"                   "Leelawadee UI Semilight"        
## [101] "Lucida Bright"                   "Lucida Calligraphy"             
## [103] "Lucida Console"                  "Lucida Fax"                     
## [105] "Lucida Handwriting"              "Lucida Sans"                    
## [107] "Lucida Sans Typewriter"          "Lucida Sans Unicode"            
## [109] "Magneto"                         "Maiandra GD"                    
## [111] "Malgun Gothic"                   "Malgun Gothic Semilight"        
## [113] "Marlett"                         "Matura MT Script Capitals"      
## [115] "Microsoft Himalaya"              "Microsoft Yi Baiti"             
## [117] "Microsoft New Tai Lue"           "Microsoft PhagsPa"              
## [119] "Microsoft Sans Serif"            "Microsoft Tai Le"               
## [121] "Mistral"                         "Modern No. 20"                  
## [123] "Mongolian Baiti"                 "Monotype Corsiva"               
## [125] "MS Mincho"                       "MS Outlook"                     
## [127] "MS Reference Sans Serif"         "MS Reference Specialty"         
## [129] "MV Boli"                         "Myanmar Text"                   
## [131] "Niagara Engraved"                "Niagara Solid"                  
## [133] "Nirmala UI"                      "Nirmala UI Semilight"           
## [135] "OCR A Extended"                  "Old English Text MT"            
## [137] "Onyx"                            "Palace Script MT"               
## [139] "Palatino Linotype"               "Papyrus"                        
## [141] "Parchment"                       "Perpetua"                       
## [143] "Perpetua Titling MT"             "Playbill"                       
## [145] "Poor Richard"                    "Pristina"                       
## [147] "Rage Italic"                     "Ravie"                          
## [149] "Rockwell"                        "Rockwell Condensed"             
## [151] "Rockwell Extra Bold"             "Script MT Bold"                 
## [153] "Segoe MDL2 Assets"               "Segoe Print"                    
## [155] "Segoe Script"                    "Segoe UI"                       
## [157] "Segoe UI Light"                  "Segoe UI Semibold"              
## [159] "Segoe UI Semilight"              "Segoe UI Black"                 
## [161] "Segoe UI Emoji"                  "Segoe UI Historic"              
## [163] "Segoe UI Symbol"                 "Showcard Gothic"                
## [165] "SimSun-ExtB"                     "Snap ITC"                       
## [167] "Stencil"                         "Sylfaen"                        
## [169] "Symbol"                          "Tahoma"                         
## [171] "Tempus Sans ITC"                 "Times New Roman"                
## [173] "Trebuchet MS"                    "Tw Cen MT"                      
## [175] "Tw Cen MT Condensed"             "Tw Cen MT Condensed Extra Bold" 
## [177] "Verdana"                         "Viner Hand ITC"                 
## [179] "Vivaldi"                         "Vladimir Script"                
## [181] "dbldwrsw"                        "Warsaw"                         
## [183] "Webdings"                        "Wingdings"                      
## [185] "Wingdings 2"                     "Wingdings 3"                    
## [187] "xkcd"

a função font_add_google carrega as fontes do site https://fonts.google.com/

#-----------------------------------#            google fonts
#-----------------------------------
library(showtext)
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Gochi Hand", "gochi")
font_add_google("Schoolbell", "bell")

## Automatically use showtext to render text
showtext_auto()

hist(rnorm(1000), breaks = 30, col = "steelblue", border = "white",
     main = "", xlab = "", ylab = "")
title("Histogram of Normal Random Numbers", family = "bell", cex.main = 2)
title(ylab = "Frequency", family = "gochi", cex.lab = 2)
text(2, 70, "N = 1000", family = "bell", cex = 2.5)

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
  geom_text(aes(label = nomes, family = c("gochi", "bell", "gochi")[cyl]))