library(readxl)

vendas <- read_excel("rsconnect/documents/Aulas2025.Rmd/rpubs.com/rpubs/Vendas2024.xlsx")

head(vendas)
## # A tibble: 6 × 5
##   vendedor dias                venda_diaria   ano   mes
##      <dbl> <dttm>                     <dbl> <dbl> <dbl>
## 1   101101 2018-01-01 00:00:00         372.  2018     1
## 2   101101 2018-01-01 00:00:00         139.  2018     1
## 3   101101 2018-01-01 00:00:00         354.  2018     1
## 4   101101 2018-01-01 00:00:00         241.  2018     1
## 5   101101 2018-01-01 00:00:00         123.  2018     1
## 6   101101 2018-01-01 00:00:00         165.  2018     1
library(readxl)

salario <- read_excel("rsconnect/documents/Aulas2025.Rmd/rpubs.com/rpubs/Salario_base2024.xlsx")

head(salario)
## # A tibble: 6 × 7
##   `Número de Inscrição` Nome   Sobrenome   Setor       Cargo Contrato           
##                   <dbl> <chr>  <chr>       <chr>       <chr> <dttm>             
## 1                101101 Nome1  Sobrenome1  Operação    Oper… 2018-01-01 00:00:00
## 2                101102 Nome2  Sobrenome2  Operação    Oper… 2018-01-02 00:00:00
## 3                201101 Nome14 Sobrenome14 Apoio       Aux … 2018-04-01 00:00:00
## 4                301101 Nome18 Sobrenome18 Administra… Aux … 2018-04-01 00:00:00
## 5                101103 Nome3  Sobrenome3  Operação    Oper… 2018-08-01 00:00:00
## 6                101104 Nome4  Sobrenome4  Operação    Oper… 2019-10-01 00:00:00
## # ℹ 1 more variable: Salario_base <dbl>

Análise Estatística de Dados e Visualização

Análise Exploratória de Vendas

Carregar pacotes de dados

Nesta etapa os dados são carregados a partir do arquivo Excel. Em seguida os tipos de dados são ajustados e valores ausentes são removidos.

library(readxl)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(knitr)
library(kableExtra)
## 
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
vendas <- read_excel("rsconnect/documents/Aulas2025.Rmd/rpubs.com/rpubs/Vendas2024.xlsx")
vendas <- vendas %>%
mutate(
vendedor = as.factor(vendedor),
dias = as.Date(dias),
mes = as.factor(mes),
venda_diaria = as.numeric(venda_diaria)
) %>%
filter(!is.na(venda_diaria))

Visualizar os dados

A seguir exibimos estrutura, primeiras linhas e resumo estatístico.

str(vendas)
## tibble [113,202 × 5] (S3: tbl_df/tbl/data.frame)
##  $ vendedor    : Factor w/ 12 levels "101101","101102",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ dias        : Date[1:113202], format: "2018-01-01" "2018-01-01" ...
##  $ venda_diaria: num [1:113202] 372 139 354 241 123 ...
##  $ ano         : num [1:113202] 2018 2018 2018 2018 2018 ...
##  $ mes         : Factor w/ 12 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
head(vendas)
## # A tibble: 6 × 5
##   vendedor dias       venda_diaria   ano mes  
##   <fct>    <date>            <dbl> <dbl> <fct>
## 1 101101   2018-01-01         372.  2018 1    
## 2 101101   2018-01-01         139.  2018 1    
## 3 101101   2018-01-01         354.  2018 1    
## 4 101101   2018-01-01         241.  2018 1    
## 5 101101   2018-01-01         123.  2018 1    
## 6 101101   2018-01-01         165.  2018 1
summary(vendas$venda_diaria)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    9.79  133.27  254.06  260.38  373.85  838.42

Estatística por vendedor

Aqui calculamos: total, média, mediana, desvio padrão e quantidade de vendas por vendedor.

estat_vendedor <- vendas %>%
group_by(vendedor) %>%
summarise(
total_vendas = sum(venda_diaria),
media = mean(venda_diaria),
mediana = median(venda_diaria),
desvio = sd(venda_diaria),
qtd_vendas = n()
) %>%
arrange(desc(total_vendas))

kable(estat_vendedor, caption = "Estatísticas por Vendedor") %>%
kable_styling(full_width = FALSE)
Estatísticas por Vendedor
vendedor total_vendas media mediana desvio qtd_vendas
101102 4031176 276.0323 268.370 160.7581 14604
101101 3489519 253.9309 249.280 146.2669 13742
101103 3433923 268.7161 264.700 155.3434 12779
101105 2694622 258.6258 250.790 150.1411 10419
101104 2491491 237.4432 232.130 137.8471 10493
101108 2473665 271.4734 265.235 157.0374 9112
101106 2458056 245.8794 240.990 143.1502 9997
102111 2441308 244.6691 240.990 140.9007 9978
101107 2201441 250.2775 243.280 146.5876 8796
102112 1338667 289.5042 282.730 166.8494 4624
101109 1296194 304.0569 297.620 173.8729 4263
101110 1125154 256.0078 252.310 145.7166 4395

Venda mensal total

Total de vendas por mês, considerando todos os vendedores.

mensal_total <- vendas %>%
group_by(mes) %>%
summarise(total = sum(venda_diaria))

kable(mensal_total, caption = "Venda Mensal Total") %>%
kable_styling(full_width = FALSE)
Venda Mensal Total
mes total
1 2386425
2 1997638
3 2465693
4 2164830
5 2661291
6 2439277
7 2057659
8 2091707
9 2109516
10 2511229
11 3013742
12 3576212

Venda mensal por vendedor

Total de vendas por mês para cada vendedor.

mensal_vendedor <- vendas %>%
group_by(vendedor, mes) %>%
summarise(total = sum(venda_diaria), .groups = "drop")

kable(mensal_vendedor, caption = "Venda Mensal por Vendedor") %>%
kable_styling(full_width = FALSE)
Venda Mensal por Vendedor
vendedor mes total
101101 1 304996.55
101101 2 280841.29
101101 3 271108.23
101101 4 312580.18
101101 5 332379.50
101101 6 273624.83
101101 7 244295.36
101101 8 241060.96
101101 9 235423.53
101101 10 272452.76
101101 11 353683.41
101101 12 367072.11
101102 1 358325.87
101102 2 258428.16
101102 3 350061.41
101102 4 316714.02
101102 5 303263.64
101102 6 351696.00
101102 7 290731.31
101102 8 293787.95
101102 9 313033.75
101102 10 328450.46
101102 11 354579.10
101102 12 512104.04
101103 1 256950.60
101103 2 227085.20
101103 3 261548.28
101103 4 264556.80
101103 5 290407.44
101103 6 253482.21
101103 7 219043.61
101103 8 289967.85
101103 9 281298.37
101103 10 296370.12
101103 11 376967.19
101103 12 416245.38
101104 1 222662.16
101104 2 155017.30
101104 3 217127.72
101104 4 154917.52
101104 5 229778.76
101104 6 210482.34
101104 7 153517.12
101104 8 144273.45
101104 9 154356.88
101104 10 230839.02
101104 11 292080.27
101104 12 326438.93
101105 1 243601.59
101105 2 181129.39
101105 3 230925.68
101105 4 217487.04
101105 5 210134.02
101105 6 211313.24
101105 7 198280.47
101105 8 178660.18
101105 9 182837.38
101105 10 219010.56
101105 11 281821.85
101105 12 339420.87
101106 1 190747.81
101106 2 186249.53
101106 3 211870.26
101106 4 187609.33
101106 5 248299.20
101106 6 178133.89
101106 7 138348.33
101106 8 163860.42
101106 9 178171.05
101106 10 203824.08
101106 11 270247.30
101106 12 300695.05
101107 1 131991.95
101107 2 148701.20
101107 3 205178.07
101107 4 186903.56
101107 5 216176.32
101107 6 193094.22
101107 7 159875.93
101107 8 174730.10
101107 9 154164.84
101107 10 190023.95
101107 11 190755.82
101107 12 249844.79
101108 1 144378.98
101108 2 147176.34
101108 3 225444.00
101108 4 203459.68
101108 5 256836.88
101108 6 212237.53
101108 7 191760.85
101108 8 169119.74
101108 9 194768.33
101108 10 193896.73
101108 11 240693.66
101108 12 293892.62
101109 1 130292.65
101109 2 89180.06
101109 3 86114.55
101109 4 56328.50
101109 5 96427.52
101109 6 133369.88
101109 7 94089.19
101109 8 101625.88
101109 9 80056.90
101109 10 131671.38
101109 11 145915.46
101109 12 151122.44
101110 1 102221.04
101110 2 71676.60
101110 3 78463.27
101110 4 49210.58
101110 5 103094.09
101110 6 111804.99
101110 7 97608.66
101110 8 95514.34
101110 9 81701.94
101110 10 103513.47
101110 11 120435.98
101110 12 109909.32
102111 1 214266.61
102111 2 160480.82
102111 3 205464.53
102111 4 154996.70
102111 5 237483.46
102111 6 195677.70
102111 7 166520.44
102111 8 128257.65
102111 9 163233.04
102111 10 224139.70
102111 11 262223.83
102111 12 328563.49
102112 1 85988.78
102112 2 91672.44
102112 3 122386.60
102112 4 60065.69
102112 5 137010.19
102112 6 114360.31
102112 7 103587.34
102112 8 110848.82
102112 9 90469.73
102112 10 117036.86
102112 11 124337.65
102112 12 180902.99

Gráfico de barras - Total por vendedor

ggplot(estat_vendedor, aes(x=vendedor, y=total_vendas)) +
geom_bar(stat="identity") +
labs(title="Total de Vendas por Vendedor",
x="Vendedor", y="Total de Vendas") +
theme_minimal()

Gráfico de linha - Tendência mensal geral

ggplot(mensal_total, aes(x=mes, y=total, group=1)) +
geom_line() +
geom_point() +
labs(title="Tendência Mensal de Vendas",
x="Mês", y="Total de Vendas") +
theme_minimal()

Gráfico facetado - Venda mensal por vendedor

ggplot(mensal_vendedor, aes(x=mes, y=total, group=vendedor)) +
geom_line() +
geom_point() +
facet_wrap(~ vendedor, scales = "free_y") +
labs(title="Tendência Mensal por Vendedor",
x="Mês", y="Total de Vendas Mensais") +
theme_minimal()

Histograma - Venda diária

ggplot(vendas, aes(x=venda_diaria)) +
geom_histogram(bins=30) +
labs(title="Distribuição das Vendas Diárias",
x="Venda Diária", y="Frequência") +
theme_minimal()

Boxplot por mês

ggplot(vendas, aes(x=mes, y=venda_diaria)) +
geom_boxplot() +
labs(title="Distribuição das Vendas por Mês",
x="Mês", y="Venda Diária") +
theme_minimal()

Boxplot por vendedor

ggplot(vendas, aes(x=vendedor, y=venda_diaria)) +
geom_boxplot() +
labs(title="Distribuição das Vendas por Vendedor",
x="Vendedor", y="Venda Diária") +
theme_minimal()