Infográfico no R

Alexandre Godinho

17/01/2020

DESCRIÇÃO

Modelo de infográfico feito pelo R. Resultado anual de vendas de uma pizzaria.

VISUALIZAÇÃO

Clique para ver em tamanho maior.

Infográfico

Infográfico

CÓDIGO

library(gt)
library(arules)
library(dplyr)
library(data.table)
library(tools)
library(patchwork)
library(ggplot2)
library(ggtext)
# remotes::install_github('hrbrmstr/waffle')
library(waffle)

### DATA WRANGLING Dados
dados <- pizzaplace
`?`(pizzaplace)

# Transforma em data
dados <- dados %>% mutate(date = as.Date(date, format = "%Y-%m-%d"))
str(dados)

# Transforma em tempo
dados <- dados %>% mutate(time = as.ITime(time))
str(dados)

# Cria faixas para o tempo
x <- 1:24
y <- rep(0, 24)
for (i in 1:24) {
    y[i] <- (86400/24) * i
}
df_faixas <- as.data.frame(cbind(y, x))

dados$faixa <- cut(dados$time, breaks = c(-Inf, df_faixas$y), labels = paste(df_faixas$x - 
    1, "h00 às ", df_faixas$x, "h00", sep = ""))


# Nome das pizzas
x <- unique(dados$name)
y <- c("Hawaiian", "Classic Deluxe", x[3], "Thai Chicken", x[5], "Italian Supreme", 
    "Prosciutto and Arugula", "Barbecue Chicken", "Greek", "Spinach Supreme", "Spinach Pesto", 
    "Spicy Italian", "Italian Capocollo", "Vegan", "Green Garden", "Southwest Chicken", 
    x[17], "California Chicken", "Chicken Pesto", "Big Met", x[21], x[22], x[23], 
    x[24], "Spinach and Feta", "Italian Vegetables", x[27], "Pepper Salami", x[29], 
    "Chicken Alfredo", "Pepperoni, Mushroom, and Peppers", "Brie Carre")
y <- gsub("_", "", toTitleCase(y))
df_sabores <- as.data.frame(cbind(x, y))
colnames(df_sabores) <- c("name", "new_name")
dados <- dados %>% left_join(df_sabores)
dados$name <- dados$new_name
dados <- dados[, -9]


### INFOGRÁFICO Gráficos: waffle: Qtd por size
my_font <- "sans"
tsize <- dados %>% group_by(size) %>% summarise(qtd = round(n() * 100/nrow(dados), 
    0)) %>% arrange(desc(qtd))

gsize <- tsize %>% ggplot(aes(fill = size, values = qtd)) + geom_waffle(n_rows = 10, 
    colour = "white", flip = FALSE, show.legend = FALSE) + scale_fill_manual(values = c("#F72C25", 
    "#FD8235", "#82869B", "#F7B32B")) + labs(title = "", subtitle = "<span style='font-size:48px'>**Para cada cem unidades**</span><br>
                  **<span style='color:#F7B32B;font-size:28px'>Extra-grande</span>,
                  <span style='color:#F72C25;font-size:28px'>Grande</span>,
                  <span style='color:#FD8235;font-size:28px'>Média</span>,
                  <span style='color:#82869B;font-size:28px'>Pequena</span>.**", 
    x = "", y = "") + theme(text = element_text(family = my_font), plot.title = element_markdown(color = "#292929"), 
    plot.subtitle = element_markdown(color = "#292929"), plot.background = element_rect("#faeabe"), 
    panel.background = element_blank(), panel.grid = element_blank(), axis.text.x = element_blank(), 
    axis.ticks.x = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank())

gsize

# barra: Qtd por name
tname <- dados %>% group_by(name) %>% summarise(qtd = n()) %>% arrange(desc(qtd)) %>% 
    top_n(6)
tname$qtd <- as.numeric(tname$qtd)

gname <- tname %>% ggplot(aes(x = reorder(name, qtd), y = qtd, fill = name, label = qtd)) + 
    geom_bar(stat = "identity", show.legend = FALSE) + coord_flip(ylim = c(2000, 
    2500)) + scale_fill_manual(values = rep("#208c78", 6)) + labs(title = "", subtitle = "<span style='color:#2c6349; font-size:48px'>**Sabores mais pedidos**</span>", 
    y = "Unidades vendidas", x = "") + theme(text = element_text(family = my_font), 
    plot.title = element_markdown(color = "#292929"), plot.subtitle = element_markdown(color = "#292929"), 
    plot.background = element_rect("#faeabe"), panel.background = element_blank(), 
    panel.grid = element_blank(), axis.text.y = element_text(size = 22), axis.text.x = element_text(size = 22), 
    axis.title.x = element_text(size = 22))
gname

# ts:Vendas por dia de semana

tsemana <- dados %>% group_by(date) %>% mutate(date = weekdays(date)) %>% summarise(qtd = n())
tsemana$date <- factor(tsemana$date, levels = c("Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday"))
tsemana <- tsemana %>% arrange(date)

gsemana <- tsemana %>% ggplot(aes(x = date, y = qtd, group = 1)) + geom_line(size = 5) + 
    geom_point() + scale_y_continuous(limits = c(5000, 9000), breaks = seq(5000, 
    9000, 500)) + labs(title = "", subtitle = "<span><span style='color:#4e91fc; font-size:48px'>**8242 vendas às sextas-feiras**</span><br><br><span style='color:#6f7375; font-size:28px'>Sexta e sábado são os melhores dias.</span></span>", 
    x = "", y = "Unidades vendidas") + theme(text = element_text(family = my_font), 
    plot.title = element_markdown(color = "#292929"), plot.subtitle = element_markdown(color = "#292929"), 
    plot.background = element_rect("#faeabe"), panel.background = element_blank(), 
    axis.text.x = element_text(angle = 15, color = "#a8a179", size = 28), axis.text.y = element_text(size = 28), 
    axis.title.y = element_text(size = 22))
gsemana

# ts:Vendas por mês
tmes <- dados %>% group_by(date) %>% mutate(date = month.abb[month(date)]) %>% summarise(qtd = n())
tmes$date <- factor(tmes$date, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
tmes <- tmes %>% arrange(date)
tmes

gmes <- tmes %>% ggplot(aes(x = date, y = qtd, group = 1, color = "red")) + geom_line(size = 5, 
    show.legend = FALSE) + geom_point(show.legend = FALSE) + scale_y_continuous(limits = c(3000, 
    4500), breaks = seq(3000, 4500, 200)) + annotate("text", label = "4392", x = 7, 
    y = 4392 * 1.02, color = "#9e021e", size = 10) + annotate("rect", xmin = 8.5, 
    xmax = 10.5, ymin = 3600, ymax = 4100, alpha = 0.2) + labs(title = "", subtitle = "<span><span style='color:#9e021e ; font-size:48px'>**Julho foi melhor mês**</span><br><br><span style='color:#6f7375; font-size:28px'>Queda em Setembro e Outubro.</span></span>", 
    x = "", y = "") + theme(text = element_text(family = my_font), plot.title = element_markdown(color = "#292929"), 
    plot.subtitle = element_markdown(color = "#292929"), plot.background = element_rect("#faeabe"), 
    panel.background = element_blank(), axis.text.x = element_text(angle = 15, color = "#a8a179", 
        size = 28), axis.text.y = element_text(size = 28))
gmes

# ts:Vendas por horário
thorario <- dados %>% group_by(faixa) %>% summarise(qtd = n())
thorario

ghorario <- thorario %>% ggplot(aes(x = faixa, y = qtd, group = 1)) + geom_line(size = 5, 
    show.legend = FALSE, color = "#fcc11c") + geom_point(show.legend = FALSE, color = "#fcc11c") + 
    scale_x_discrete() + annotate("rect", xmin = 3.5, xmax = 5.5, ymin = 5500, ymax = 7500, 
    alpha = 0.2) + annotate("rect", xmin = 8.5, xmax = 10.5, ymin = 4000, ymax = 6500, 
    alpha = 0.2) + labs(title = "", subtitle = "<span><span style='color:#a8a179; font-size:48px'>**Mais vendas entre 12h00 e 14h00**</span><br><br><span style='color:#6f7375; font-size:28px'>Retomada das 17h00 às 19h00.</span></span>", 
    x = "", y = "") + theme(text = element_text(family = my_font), plot.title = element_markdown(color = "#292929"), 
    plot.subtitle = element_markdown(color = "#292929"), plot.background = element_rect("#faeabe"), 
    panel.background = element_blank(), axis.text.y = element_text(size = 22), axis.ticks.y = element_blank(), 
    axis.text.x = element_text(angle = 15, color = "#a8a179", size = 22))

ghorario


# Combinando gráficos
def_plot <- (gsize + gname)/gsemana/gmes/ghorario

# Gerando o infográfico
def_plot + plot_layout(ncol = 1) + plot_annotation(tag_levels = list(c("Tamanho", 
    "Sabores", "Dias", "Mês", "Horário")), title = "<br><span style='color:#364187; font-size: 54px'>**Vendas anual de pizzas**</span>", 
    subtitle = "<br><span style='color:#62657a; font-size: 28px'; font-size: 32px>Unidades vendidas: **49574**</span><br>
                  <span style='color:#62657a; font-size: 28px'>Média de unidades vendidas por mês: **4131**</span><br>
                  <span style='color:#62657a; font-size: 28px'>Receita total: **US$817.860,00**</span><br>", 
    caption = "Autor: Alexandre Godinho", theme = theme(plot.title = element_markdown(color = "#292929", 
        size = 22, face = "bold", family = my_font), plot.subtitle = element_markdown(color = "#292929", 
        size = 14, family = my_font), plot.caption = element_text(color = "grey50", 
        face = "bold.italic", family = my_font), plot.background = element_rect("#faeabe"), 
        panel.background = element_rect("#faeabe"))) & theme(plot.tag = element_text(size = 18))



### Salvando
ggsave("pizzas.png", width = 49, height = 70, units = "cm", dpi = 500, type = "cairo-png")