R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Aula de Programação - Fase 3

Alunos: Bruno Bemfeito & Diego Rodrigues

Profesor: Adriano Lauro

Curso: Engenharia Civil

Data: 10.12.25

Rio de Janeiro - RJ

library(dplyr)
## 
## Anexando pacote: 'dplyr'
## Os seguintes objetos são mascarados por 'package:stats':
## 
##     filter, lag
## Os seguintes objetos são mascarados por 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(readxl)
## Warning: pacote 'readxl' foi compilado no R versão 4.5.2
library(readr)
library(writexl)
## Warning: pacote 'writexl' foi compilado no R versão 4.5.2
library(openxlsx)
## Warning: pacote 'openxlsx' foi compilado no R versão 4.5.2
library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(tidyr)
## Warning: pacote 'tidyr' foi compilado no R versão 4.5.2
library(DT)
## Warning: pacote 'DT' foi compilado no R versão 4.5.2
library(lubridate)   
## Warning: pacote 'lubridate' foi compilado no R versão 4.5.2
## 
## Anexando pacote: 'lubridate'
## Os seguintes objetos são mascarados por 'package:base':
## 
##     date, intersect, setdiff, union
library(scales)
## 
## Anexando pacote: 'scales'
## O seguinte objeto é mascarado por 'package:readr':
## 
##     col_factor
library(readr)
library(readxl) 
library(writexl)
library(openxlsx)
library(forecast)
library(tidyr)
library (DT)
vendas <- read_excel("Vendas2024.xlsx")
salarios <- read_excel("Salario_base2024.xlsx")
# 1) Gráfico de barras – venda total por vendedor

estat_vendedor <- vendas %>%
  group_by(vendedor) %>%
  summarise(
    total_vendas = sum(venda_diaria),
    media_diaria = mean(venda_diaria),
    mediana_diaria = median(venda_diaria),
    desvio_padrao = sd(venda_diaria),
    n_registros = n(),
    .groups = "drop"
  ) %>%
  arrange(desc(total_vendas))

ggplot(estat_vendedor, aes(x = total_vendas, y = reorder(vendedor, total_vendas))) +
  geom_bar(stat = "identity", fill = "darkorange") +
  geom_text(aes(label = round(total_vendas,0)), hjust = -0.2) +
  labs(title = "Venda Total por Vendedor", x = "Total de Vendas", y = "Vendedor") +
  theme_light()

# 2) Gráfico de linhas – tendência geral mensal
estat_vendedor <- vendas %>%
  group_by(vendedor) %>%
  summarise(
    total_vendas = sum(venda_diaria),
    media_diaria = mean(venda_diaria),
    mediana_diaria = median(venda_diaria),
    desvio_padrao = sd(venda_diaria),
    n_registros = n(),
    .groups = "drop"
  ) %>%
  arrange(desc(total_vendas))

venda_mensal_geral <- vendas %>%
  group_by(ano, mes) %>%
  summarise(total_mensal = sum(venda_diaria), .groups = "drop") %>%
  arrange(ano, mes)

ggplot(venda_mensal_geral, aes(x = mes, y = total_mensal, color = as.factor(ano), group = ano)) +
  geom_line(linewidth = 1.2) +
  labs(
  title = "Tendencia Geral das Vendas Mensais",
  x = "Mes",
  y = "Total de Vendas"
)

# 3) Gráficos facetados – vendas mensais por vendedor
estat_vendedor <- vendas %>%
  group_by(vendedor) %>%
  summarise(
    total_vendas = sum(venda_diaria),
    media_diaria = mean(venda_diaria),
    mediana_diaria = median(venda_diaria),
    desvio_padrao = sd(venda_diaria),
    n_registros = n(),
    .groups = "drop"
  ) %>%
  arrange(desc(total_vendas))

venda_mensal_vendedor <- vendas %>%
  group_by(vendedor, ano, mes) %>%
  summarise(total_mensal = sum(venda_diaria), .groups = "drop") %>%
  unite(col = "data_mes", ano, mes, sep = "-", remove = FALSE) %>%
  mutate(data_mes = as.Date(paste0(data_mes, "-01")))
  

ggplot(venda_mensal_geral, aes(x = mes, y = total_mensal, color = as.factor(ano), group = ano)) +
  geom_line(linewidth = 1.2) +
  labs(
    title = "Tendencia Geral das Vendas Mensais",
    x = "Mes",
    y = "Total de Vendas"
  ) +
  theme_minimal()

# 4) Histograma – distribuição das vendas diárias
estat_vendedor <- vendas %>%
  group_by(vendedor) %>%
  summarise(
    total_vendas = sum(venda_diaria),
    media_diaria = mean(venda_diaria),
    mediana_diaria = median(venda_diaria),
    desvio_padrao = sd(venda_diaria),
    n_registros = n(),
    .groups = "drop"
  ) %>%
  arrange(desc(total_vendas))

ggplot(vendas, aes(x = venda_diaria)) +
  geom_histogram(binwidth = 200, fill = "skyblue", color = "black", alpha = 0.6) +
  geom_density(color = "red", linewidth = 1.2) +
  labs(
  title = "Distribuicao das Vendas Diarias",
  x = "Venda Diaria",
  y = "Frequencia"
)

# 5) Boxplot – vendas diárias por mês
estat_vendedor <- vendas %>%
  group_by(vendedor) %>%
  summarise(
    total_vendas = sum(venda_diaria),
    media_diaria = mean(venda_diaria),
    mediana_diaria = median(venda_diaria),
    desvio_padrao = sd(venda_diaria),
    n_registros = n(),
    .groups = "drop"
  ) %>%
  arrange(desc(total_vendas))

ggplot(vendas, aes(x = as.factor(mes), y = venda_diaria, fill = as.factor(mes))) +
  geom_boxplot(alpha = 0.7) +
  geom_jitter(width = 0.2, alpha = 0.4, color = "black") +
labs(
  title = "Distribuicao das Vendas Diarias por Mes",
  x = "Mes",
  y = "Venda Diaria"
)

# 6) Boxplot – vendas por vendedor (violin plot)


ggplot(venda_mensal_geral, aes(x = mes, y = total_mensal, color = as.factor(ano))) +
  geom_line() +
  stat_summary(fun = "mean", geom = "point", shape = 23, size = 3, fill = "yellow") +
  labs(
  title = "Distribuicao das Vendas Diarias",
  x = "Venda Diaria",
  y = "Frequencia"
)