Skip to Content

Gráficos Waffles no R

Passo-a-passo para gerar um infográfico com os pacotes do R, waffle e extrafont.

1. Instale os pacotes R

Você precisa do pacote waffle para criar gráficos de waffles e extrafont para usar Ícones nos gráficos.

#install.packages(c("waffle", "extrafont"))
library(waffle)
## Carregando pacotes exigidos: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.2
library(extrafont)
## Registering fonts with R

2. Instalar o Font Awesome

Os Ícones que o waffle pode usar são fornecidos pela Font Awesome.

Baixe o arquivo zip clicando no link Download em seu site. você pode querer verificar a opção paga mais tarde, mas por enquanto, basta selecionar o download gratuito.

Descompacte o arquivo e navegue até o diretório de fontes. Para instalar a fonte, use o arquivo chamado fontawesome-webfont.ttf. Para usuários do Windows, a instalação é tão simples quanto clicar duas vezes no arquivo e escolher “Instalar”.

3. Importar e registrar as fontes

No R, as fontes devem ser importadas para o banco de dados extrafont. você precisa fazer isso sempre que uma nova fonte estiver instalada que você deseja usar no R.

#font_import()

# check that Font Awesome is imported
fonts()[grep("Awesome", fonts())]
## [1] "FontAwesome"

Se você executar o comando font_import() em uma sessão, você precisa registrar as fontes com o dispositivo de sa?da R. Caso contrário, isso ocorre quando o pacote ? carregado. Se você estiver usando o sistema operacional Windows e deseja utilizar o RStudio, é necessário um argumento adicional:

# this should be fine for Mac OSX
#loadfonts()

# use this if things look odd in RStudio under Windows
loadfonts(device = "win")
## Agency FB already registered with windowsFonts().
## Algerian already registered with windowsFonts().
## Arial Black already registered with windowsFonts().
## Arial already registered with windowsFonts().
## Arial Narrow already registered with windowsFonts().
## Arial Rounded MT Bold already registered with windowsFonts().
## Arial Unicode MS already registered with windowsFonts().
## Bahnschrift already registered with windowsFonts().
## Baskerville Old Face already registered with windowsFonts().
## Bauhaus 93 already registered with windowsFonts().
## Bell MT already registered with windowsFonts().
## Berlin Sans FB already registered with windowsFonts().
## Berlin Sans FB Demi already registered with windowsFonts().
## Bernard MT Condensed already registered with windowsFonts().
## Blackadder ITC already registered with windowsFonts().
## Bodoni MT already registered with windowsFonts().
## Bodoni MT Black already registered with windowsFonts().
## Bodoni MT Condensed already registered with windowsFonts().
## Bodoni MT Poster Compressed already registered with windowsFonts().
## Book Antiqua already registered with windowsFonts().
## Bookman Old Style already registered with windowsFonts().
## Bookshelf Symbol 7 already registered with windowsFonts().
## Bradley Hand ITC already registered with windowsFonts().
## Britannic Bold already registered with windowsFonts().
## Broadway already registered with windowsFonts().
## Brush Script MT already registered with windowsFonts().
## Calibri already registered with windowsFonts().
## Calibri Light already registered with windowsFonts().
## Californian FB already registered with windowsFonts().
## Calisto MT already registered with windowsFonts().
## Cambria already registered with windowsFonts().
## Candara already registered with windowsFonts().
## Candara Light already registered with windowsFonts().
## Castellar already registered with windowsFonts().
## Centaur already registered with windowsFonts().
## Century already registered with windowsFonts().
## Century Gothic already registered with windowsFonts().
## Century Schoolbook already registered with windowsFonts().
## Chiller already registered with windowsFonts().
## Colonna MT already registered with windowsFonts().
## Comic Sans MS already registered with windowsFonts().
## Consolas already registered with windowsFonts().
## Constantia already registered with windowsFonts().
## Cooper Black already registered with windowsFonts().
## Copperplate Gothic Bold already registered with windowsFonts().
## Copperplate Gothic Light already registered with windowsFonts().
## Corbel already registered with windowsFonts().
## Corbel Light already registered with windowsFonts().
## Courier New already registered with windowsFonts().
## Curlz MT already registered with windowsFonts().
## Ebrima already registered with windowsFonts().
## Edwardian Script ITC already registered with windowsFonts().
## Elephant already registered with windowsFonts().
## Engravers MT already registered with windowsFonts().
## Eras Bold ITC already registered with windowsFonts().
## Eras Demi ITC already registered with windowsFonts().
## Eras Light ITC already registered with windowsFonts().
## Eras Medium ITC already registered with windowsFonts().
## Felix Titling already registered with windowsFonts().
## FontAwesome already registered with windowsFonts().
## Footlight MT Light already registered with windowsFonts().
## Forte already registered with windowsFonts().
## Franklin Gothic Book already registered with windowsFonts().
## Franklin Gothic Demi already registered with windowsFonts().
## Franklin Gothic Demi Cond already registered with windowsFonts().
## Franklin Gothic Heavy already registered with windowsFonts().
## Franklin Gothic Medium already registered with windowsFonts().
## Franklin Gothic Medium Cond already registered with windowsFonts().
## Freestyle Script already registered with windowsFonts().
## French Script MT already registered with windowsFonts().
## Gabriola already registered with windowsFonts().
## Gadugi already registered with windowsFonts().
## Garamond already registered with windowsFonts().
## Georgia already registered with windowsFonts().
## Gigi already registered with windowsFonts().
## Gill Sans Ultra Bold already registered with windowsFonts().
## Gill Sans Ultra Bold Condensed already registered with windowsFonts().
## Gill Sans MT already registered with windowsFonts().
## Gill Sans MT Condensed already registered with windowsFonts().
## Gill Sans MT Ext Condensed Bold already registered with windowsFonts().
## Gloucester MT Extra Condensed already registered with windowsFonts().
## Goudy Old Style already registered with windowsFonts().
## Goudy Stout already registered with windowsFonts().
## Haettenschweiler already registered with windowsFonts().
## Harlow Solid Italic already registered with windowsFonts().
## Harrington already registered with windowsFonts().
## High Tower Text already registered with windowsFonts().
## HoloLens MDL2 Assets already registered with windowsFonts().
## Impact already registered with windowsFonts().
## Imprint MT Shadow already registered with windowsFonts().
## Informal Roman already registered with windowsFonts().
## Ink Free already registered with windowsFonts().
## Javanese Text already registered with windowsFonts().
## Jokerman already registered with windowsFonts().
## Juice ITC already registered with windowsFonts().
## Kristen ITC already registered with windowsFonts().
## Kunstler Script already registered with windowsFonts().
## Wide Latin already registered with windowsFonts().
## Leelawadee UI already registered with windowsFonts().
## Leelawadee UI Semilight already registered with windowsFonts().
## Lucida Bright already registered with windowsFonts().
## Lucida Calligraphy already registered with windowsFonts().
## Lucida Console already registered with windowsFonts().
## Lucida Fax already registered with windowsFonts().
## Lucida Handwriting already registered with windowsFonts().
## Lucida Sans already registered with windowsFonts().
## Lucida Sans Typewriter already registered with windowsFonts().
## Lucida Sans Unicode already registered with windowsFonts().
## Magneto already registered with windowsFonts().
## Maiandra GD already registered with windowsFonts().
## Malgun Gothic already registered with windowsFonts().
## Malgun Gothic Semilight already registered with windowsFonts().
## Marlett already registered with windowsFonts().
## Matura MT Script Capitals already registered with windowsFonts().
## Microsoft Himalaya already registered with windowsFonts().
## Microsoft Yi Baiti already registered with windowsFonts().
## Microsoft New Tai Lue already registered with windowsFonts().
## Microsoft PhagsPa already registered with windowsFonts().
## Microsoft Sans Serif already registered with windowsFonts().
## Microsoft Tai Le already registered with windowsFonts().
## Mistral already registered with windowsFonts().
## Modern No. 20 already registered with windowsFonts().
## Mongolian Baiti already registered with windowsFonts().
## Monotype Corsiva already registered with windowsFonts().
## MS Mincho already registered with windowsFonts().
## MS Outlook already registered with windowsFonts().
## MS Reference Sans Serif already registered with windowsFonts().
## MS Reference Specialty already registered with windowsFonts().
## MV Boli already registered with windowsFonts().
## Myanmar Text already registered with windowsFonts().
## Niagara Engraved already registered with windowsFonts().
## Niagara Solid already registered with windowsFonts().
## Nirmala UI already registered with windowsFonts().
## Nirmala UI Semilight already registered with windowsFonts().
## OCR A Extended already registered with windowsFonts().
## Old English Text MT already registered with windowsFonts().
## Onyx already registered with windowsFonts().
## Palace Script MT already registered with windowsFonts().
## Palatino Linotype already registered with windowsFonts().
## Papyrus already registered with windowsFonts().
## Parchment already registered with windowsFonts().
## Perpetua already registered with windowsFonts().
## Perpetua Titling MT already registered with windowsFonts().
## Playbill already registered with windowsFonts().
## Poor Richard already registered with windowsFonts().
## Pristina already registered with windowsFonts().
## Rage Italic already registered with windowsFonts().
## Ravie already registered with windowsFonts().
## Rockwell already registered with windowsFonts().
## Rockwell Condensed already registered with windowsFonts().
## Rockwell Extra Bold already registered with windowsFonts().
## Script MT Bold already registered with windowsFonts().
## Segoe MDL2 Assets already registered with windowsFonts().
## Segoe Print already registered with windowsFonts().
## Segoe Script already registered with windowsFonts().
## Segoe UI already registered with windowsFonts().
## Segoe UI Light already registered with windowsFonts().
## Segoe UI Semibold already registered with windowsFonts().
## Segoe UI Semilight already registered with windowsFonts().
## Segoe UI Black already registered with windowsFonts().
## Segoe UI Emoji already registered with windowsFonts().
## Segoe UI Historic already registered with windowsFonts().
## Segoe UI Symbol already registered with windowsFonts().
## Showcard Gothic already registered with windowsFonts().
## SimSun-ExtB already registered with windowsFonts().
## Snap ITC already registered with windowsFonts().
## Stencil already registered with windowsFonts().
## Sylfaen already registered with windowsFonts().
## Symbol already registered with windowsFonts().
## Tahoma already registered with windowsFonts().
## Tempus Sans ITC already registered with windowsFonts().
## Times New Roman already registered with windowsFonts().
## Trebuchet MS already registered with windowsFonts().
## Tw Cen MT already registered with windowsFonts().
## Tw Cen MT Condensed already registered with windowsFonts().
## Tw Cen MT Condensed Extra Bold already registered with windowsFonts().
## Verdana already registered with windowsFonts().
## Viner Hand ITC already registered with windowsFonts().
## Vivaldi already registered with windowsFonts().
## Vladimir Script already registered with windowsFonts().
## dbldwrsw already registered with windowsFonts().
## Warsaw already registered with windowsFonts().
## Webdings already registered with windowsFonts().
## Wingdings already registered with windowsFonts().
## Wingdings 2 already registered with windowsFonts().
## Wingdings 3 already registered with windowsFonts().
## xkcd already registered with windowsFonts().

4. Criar gráficos

Agora estamos prontos para criar os waffles. Primeiro, seu gráfico básico de waffles. As cores utilizadas são do pacote RColorBrewer do tipo “Set2”.

waffle(c(50, 30, 15, 5), rows = 5, title = "Meu gráfico de Waffle")

Em seguida, você pode usar Ícones. Para isso, você precisa procurar um ícone disponível nesta página.

#waffle(c(50, 30, 15, 5), rows = 5, use_glyph = "child", glyph_size = 6, title = "Nossa! Fiz um infografico usando o R!")

você pode usar a função iron() para adicionar gráficos de waffles, que podem ser úteis nas comparações. Aqui está um exemplo comparando carros em dois países de grande rivalidade no futebol:

iron(
  waffle(c(Nao = 80, Sim = 20), rows = 5, use_glyph = "car", glyph_size = 6, 
         colors = c("#e0e585", "#197a2e"), title = "Brasil"),
  waffle(c(Nao = 70, Sim = 30), rows = 5, use_glyph = "car", glyph_size = 6,
         colors = c("#a3aabd", "#212aaf"), title = "Argentina")
)

Fonte original do artigo:

infographic-style-charts-using-the-r-waffle-package