Item 1

Questão 1

clock <- c(0.1, 0.5, 1, 1.5, 2, 2.5, 3)
MRT_1F <-c(517.1468515630205, 85.13094142168089, 30.333207896694553,
           12.694776264558937, 3.3041601673945418, 1.1823111717498882,
           1.1892293502386786)

MRT_3F <-c(156.68929936163462, 11.540837783562276, 0.4512835621696538,
           0.4509797929766453, 0.4502068233039181, 0.4496185276300172,
           0.4543157082191288)

MRT_5F <-c(83.90319666471157, 0.3068151086494968, 0.30522314133037304,
           0.3072588968084928, 0.30655265997285697, 0.3055812715727718,
           0.3053297166713006)

MRT_10F <-c(29.55430642951759, 0.19832832665772515, 0.1971923924717474,
            0.19796648905716516, 0.19615594370806338, 0.2034569237883263,
            0.19617420889447737)

MRT_15F <-c(11.317736530583566, 0.167364215666193, 0.16172168266811013,
            0.16701085329580515, 0.1598052657153692, 0.1645934043532696,
            0.16216563797118075)


MRT_sem_F <-c(11.93430909937736, 0.6095414637034009,
              0.6060645101029295, 0.612167181646899, 0.6146761002685637,
              0.6096747087200697, 0.6125810476877268)


plot(x = clock, y = MRT_1F, type = "o",pch = 4,col=("black"),
     xlab = "Time between Thing requests(seconds)",
     ylab = "Response Time (sec.)"
)

lines(clock,MRT_3F, type = "o", col=("yellow"), pch =11)
lines(clock,MRT_5F , type = "o", col=("red"), pch = 1)
lines(clock,MRT_10F, type = "o", col=("blue"), pch = 2)
lines(clock,MRT_15F, type = "o", col=("purple"),pch = 5)
lines(clock,MRT_sem_F, type = "o", col=("green"),pch = 4)

legend(
  "topright",
  legend = c("1 Fog","3 Fog","5 Fog","10 Fog","15 Fog","W/o Fog"),
  col    = c("black","yellow","red", "blue", "purple","green"),
  pch    = c(4,11,1,2,5,4)
)

# --- layout 3 linhas × 2 colunas ---
par(mfrow = c(3,2), mar = c(4,4,2,1))

# 1 Fog
barplot(
  rbind(MRT_sem_F, MRT_1F),
  beside    = TRUE,
  names.arg = clock,
  log       = "y",
  col       = c("#E6E6E6","#666666"),
  xlab      = "Time between Things requests",
  ylab      = "Response time (s)"
)
legend("topright",
       fill   = c("#E6E6E6","#666666"),
       legend = c("w/o Fog","1 Fog"),
       bty    = "y")

# 3 Fogs
barplot(
  rbind(MRT_sem_F, MRT_3F),
  beside    = TRUE,
  names.arg = clock,
  log       = "y",
  col       = c("#E6E6E6","#666666"),
  xlab      = "Time between Things requests",
  ylab      = "Response time (s)"
)
legend("topright",
       fill   = c("#E6E6E6","#666666"),
       legend = c("w/o Fog","3 Fogs"),
       bty    = "y")

# 5 Fogs
barplot(
  rbind(MRT_sem_F, MRT_5F),
  beside    = TRUE,
  names.arg = clock,
  log       = "y",
  col       = c("#E6E6E6","#666666"),
  xlab      = "Time between Things requests",
  ylab      = "Response time (s)"
)
legend("topright",
       fill   = c("#E6E6E6","#666666"),
       legend = c("w/o Fog","5 Fogs"),
       bty    = "y")

# 10 Fogs
barplot(
  rbind(MRT_sem_F, MRT_10F),
  beside    = TRUE,
  names.arg = clock,
  log       = "y",
  col       = c("#E6E6E6","#666666"),
  xlab      = "Time between Things requests",
  ylab      = "Response time (s)"
)
legend("topright",
       fill   = c("#E6E6E6","#666666"),
       legend = c("w/o Fog","10 Fogs"),
       bty    = "y")

# 15 Fogs
barplot(
  rbind(MRT_sem_F, MRT_15F),
  beside    = TRUE,
  names.arg = clock,
  log       = "y",
  col       = c("#E6E6E6","#666666"),
  xlab      = "Time between Things requests",
  ylab      = "Response time (s)"
)
legend("topright",
       fill   = c("#E6E6E6","#666666"),
       legend = c("w/o Fog","15 Fogs"),
       bty    = "y")


plot.new()

par(mfrow = c(1,1))

Questão 2

# Definir dados
ratings <- c("Good", "Very Good", "Excellent")
prices  <- c("$10–19", "$20–29", "$30–39", "$40–49")

# Valores corretos (linhas = ratings, colunas = prices)
values <- matrix(
  c(
    53.8, 33.9, 2.6, 0.0,    
    43.6, 54.2, 60.5, 21.4,  
    2.6, 11.9, 36.8, 78.6     
  ),
  nrow = length(ratings),
  ncol = length(prices),
  byrow = TRUE, 
  dimnames = list(ratings, prices)
)

cols <- c("#66C2A5", "#FC8D62", "#8DA0CB")

# Dividir a janela gráfica em gráfico (80%) e legenda (20%)
layout(matrix(c(1, 2), nrow = 1), widths = c(8, 2))

par(mar = c(5, 4, 4, 1))  # Margem para o gráfico
barplot(
  values,
  beside = FALSE,
  col = cols,
  xlab = "Faixa de Preço (US$)",
  ylab = "Porcentagem (%)",
  main = "Qualidade da Refeição por Faixa de Preço",
  ylim = c(0, 100)  # Garantir escala de 0% a 100%
)

par(mar = c(0, 0,0, 1))  # Margem zerada para a legenda
plot.new()
legend(
  "topright",
  legend = ratings,
  fill = cols,
  bty = "n",
  title = "Avaliação"
)

Questão 3

maio <- subset(airquality, Month == 5)

# Converte temperatura para Celsius
maio$TempC <- (maio$Temp - 32) / 1.8

hist(
  maio$TempC,
  probability = TRUE,     
  col         = "blue",
  main        = "Histograma da Temperatura (Maio)",
  xlab        = "Temperatura (°C)",
  ylab        = "Densidade"
)

# Adiciona curva de densidade
lines(density(maio$TempC, na.rm = TRUE), col = "red", lwd = 2)

legend("topright",
       legend = "Curva de Densidade",
       col    = "red",
       lwd    = 2,
       bty    = "n")

Questão 4

sales <- read.table("https://training-course-material.com/images/8/8f/Sales.txt", header = TRUE)

total_vendas <- sum(sales$SALES)
sales$PERCENTUAL <- round((sales$SALES / total_vendas) * 100, 1)

rotulos <- paste(sales$COUNTRY, "\n", sales$PERCENTUAL, "%", sep = "")

# Definir cores para cada país
cores <- rainbow(nrow(sales))

# Criar gráfico de pizza
pie(sales$SALES,
    labels = rotulos,
    col = cores,
    main = "Distribuição Percentual das Vendas por País",
    clockwise = TRUE)

legend("topright",
       legend = sales$COUNTRY,
       fill = cores,
       title = "Países",
       bty = "n")

Questão 5

boxplot(
  count ~ spray,          
  data = InsectSprays,    
  outline = FALSE, # Remove os outliers
  col = "yellow",         
  main = "Contagem de Insetos por Tipo de Inseticida (Sem Outliers)",
  xlab = "Tipo de Inseticida",
  ylab = "Contagem de Insetos"
)

Questão 6

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(readr)
library(stringr)
library(lubridate)
## 
## Anexando pacote: 'lubridate'
## Os seguintes objetos são mascarados por 'package:base':
## 
##     date, intersect, setdiff, union
monitoring_0.1 <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/monitoringCloudData_0.1.csv")
## Rows: 4275 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (11): totalMemory, availableMemory, usedMemory, totalSwap, freeSwap, us...
## dbl   (4): totalCpuUsage..., percentageMemory..., percentageSwap..., percent...
## dttm  (1): currentTime
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
monitoring_1 <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/monitoringCloudData_1.csv")
## Rows: 4303 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (11): totalMemory, availableMemory, usedMemory, totalSwap, freeSwap, us...
## dbl   (4): totalCpuUsage..., percentageMemory..., percentageSwap..., percent...
## dttm  (1): currentTime
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
monitoring_NONE <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/monitoringCloudData_NONE.csv")
## Rows: 4302 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (11): totalMemory, availableMemory, usedMemory, totalSwap, freeSwap, us...
## dbl   (4): totalCpuUsage..., percentageMemory..., percentageSwap..., percent...
## dttm  (1): currentTime
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
monitoring_0.5 <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/monitoringCloudData_0.5.csv",
  col_types = cols(currentTime = col_character())
)

# --- Função de processamento (hora contínua + MB) --- 
processa <- function(df) {
  df %>%
    mutate(
      hours    = as.numeric(difftime(currentTime, first(currentTime), units = "hours")),
      mem_val  = parse_number(usedMemory),
      mem_unit = str_extract(usedMemory, "[A-Za-z]+"),
      usedMB   = case_when(
        toupper(mem_unit) == "TB" ~ mem_val * 1e6,
        toupper(mem_unit) == "GB" ~ mem_val * 1024,
        toupper(mem_unit) == "MB" ~ mem_val,
        TRUE                      ~ NA_real_
      )
    )
}


d1 <- processa(monitoring_NONE)
d2 <- processa(monitoring_0.1)
d4 <- processa(monitoring_1)


d3 <- monitoring_0.5 %>%
  mutate(currentTime = ymd_hms(currentTime)) %>%
  filter(!is.na(currentTime)) %>%arrange(currentTime) %>%
  mutate(hours = as.numeric(difftime(currentTime, min(currentTime), units = "hours"))) %>%
  mutate(
    mem_val  = parse_number(usedMemory),
    mem_unit = str_extract(usedMemory, "[A-Za-z]+")
  ) %>%
  # converte tudo para MB
  mutate(
    usedMB = case_when(
      toupper(mem_unit) == "TB" ~ mem_val * 1e6,
      toupper(mem_unit) == "GB" ~ mem_val * 1024,
      toupper(mem_unit) == "MB" ~ mem_val,
      TRUE                      ~ NA_real_
    )
  )
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `currentTime = ymd_hms(currentTime)`.
## Caused by warning:
## !  194 failed to parse.
par(mfrow = c(2,2), mar = c(4,4,2,1))

# --- Gráfico 1: No Workload ---
plot(
  d1$hours, d1$usedMB,
  type = "l", lwd = 1,
  xlab = "Time (hour)", ylab = "Used Memory (MB)",
  main = "Memory Analysis (None Workload)"
)

# --- Gráfico 2: Workload 0.1 ---
plot(
  d2$hours, d2$usedMB,
  type = "l", lwd = 1,
  xlab = "Time (hour)", ylab = "Used Memory (MB)",
  main = "Memory Analysis (Workload of 0.1)"
)

# --- Gráfico 3: Workload 0.5 ---
plot(
  d3$hours, d3$usedMB,
  type  = "l",lwd   = 1,
  xlab  = "Time (hour)",
  ylab  = "Used Memory (MB)",
  main  = "Memory Analysis (Workload of 0.5)"
)

# --- Gráfico 4: Workload 1.0 ---
plot(
  d4$hours, d4$usedMB,
  type = "l", lwd = 1,
  xlab = "Time (hour)", ylab = "Used Memory (MB)",
  main = "Memory Analysis (Workload of 1.0)"
)

par(mfrow = c(1,1))

Questão 7

# pacotes
library(readr)
library(dplyr)
library(plotly)
## Carregando pacotes exigidos: ggplot2
## 
## Anexando pacote: 'plotly'
## O seguinte objeto é mascarado por 'package:ggplot2':
## 
##     last_plot
## O seguinte objeto é mascarado por 'package:stats':
## 
##     filter
## O seguinte objeto é mascarado por 'package:graphics':
## 
##     layout
# 1. leitura do CSV com locale que suporta accents/latin1
netflix <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/netflix_titles.csv",
  locale = locale(encoding = "latin1")
)
## Rows: 7787 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): show_id, type, title, director, cast, country, date_added, rating,...
## dbl  (1): release_year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2. filtra apenas itens com UM país (sem vírgula) e não NA
netflix1 <- netflix %>%
  filter(!is.na(country), !grepl(",", country))

# 3. conta e seleciona os top 10
top10 <- netflix1 %>%
  count(country, sort = TRUE) %>%
  slice_head(n = 10)

# 4. cores e plot
cores <- RColorBrewer::brewer.pal(10, "Set3")
plot_ly(
  top10,
  labels = ~country,
  values = ~n,
  type = 'pie',
  textinfo = 'label+percent',
  marker = list(colors = cores)
) %>%
  layout(
    title = "Top 10 Países com Mais Conteúdos (Origem Única)",
    showlegend = TRUE,
    legend = list(x = 1.0, y = 0.5)
  )

Questão 8

library(readr)
library(dplyr)
library(plotly)


netflix <- read_csv(
  "C:/Users/ViniciusLima/Documents/CPAD/netflix_titles.csv",locale = locale(encoding = "latin1"))
## Rows: 7787 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): show_id, type, title, director, cast, country, date_added, rating,...
## dbl  (1): release_year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
top10 <- netflix %>%
  filter(!is.na(country), !grepl(",", country)) %>%
  count(country, name = "Total de conteúdos") %>%
  arrange(desc(`Total de conteúdos`)) %>%
  slice_head(n = 10)

# Cria tabela Plotly
plot_ly(
  type = 'table',
  columnorder = 1:2,
  columnwidth = c(200, 100),
  header = list(
    values = c("<b>País</b>", "<b>Total de conteúdos</b>"),
    align  = c('center','center'),
    fill   = list(color = 'gray'),
    font   = list(color = 'white', size = 12),
    line   = list(color = 'white', width = 1)
  ),
  cells = list(
    values = list(top10$country, top10$`Total de conteúdos`),
    align  = c('center','center'),
    fill   = list(color = c('white','white')),
    font   = list(color = 'black', size = 11),
    line   = list(color = 'gray', width = 1)
  )
) %>%
  layout(margin = list(t = 20, b = 20))

Questão 9

library(readr)
library(dplyr)
library(plotly)

# 1. Leitura do CSV com encoding mais permissivo
netflix <- read_csv(
  "C:/Users/ViniciusLima/Documents/CPAD/netflix_titles.csv",
  locale = locale(encoding = "latin1")
)
## Rows: 7787 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): show_id, type, title, director, cast, country, date_added, rating,...
## dbl  (1): release_year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2. Cria variável "década" e conta por tipo
decadas_df <- netflix %>%
  filter(!is.na(release_year)) %>%
  mutate(
    decade = floor(release_year / 10) * 10
  ) %>%
  group_by(decade, type) %>%
  summarise(total = n(), .groups = "drop") %>%
  arrange(decade)

# 3. Separar em dois data.frames para as duas linhas
series_df <- decadas_df %>% filter(type == "TV Show")
filmes_df <- decadas_df %>% filter(type == "Movie")

# 4. Gráfico de linha com Plotly
plot_ly() %>%
  add_trace(
    data = series_df,
    x = ~decade,
    y = ~total,
    type = "scatter",
    mode = "lines+markers",
    name = "Séries",
    line = list(color = "blue"),
    marker = list(symbol = "circle")
  ) %>%
  add_trace(
    data = filmes_df,
    x = ~decade,
    y = ~total,
    type = "scatter",
    mode = "lines+markers",
    name = "Filmes",
    line = list(color = "yellow"),
    marker = list(symbol = "diamond")
  ) %>%
  layout(
    title = "Quantidade de Conteúdos por Década (Netflix 2019)",
    xaxis = list(title = "Década", tickvals = unique(decadas_df$decade)),
    yaxis = list(title = "Quantidade de Conteúdo"),
    legend = list(title = list(text = "<b>Tipo</b>"))
  )

Questão 10

library(readr)
library(dplyr)
library(plotly)

netflix <- read_csv("C:/Users/ViniciusLima/Documents/CPAD/netflix_titles.csv",locale = locale(encoding = "latin1"))
## Rows: 7787 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): show_id, type, title, director, cast, country, date_added, rating,...
## dbl  (1): release_year
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# 2. Filtra filmes de 2000 a 2010
df_filmes <- netflix %>%
  filter(type == "Movie",release_year >= 2000, release_year <= 2010
  )

# 3. Extrai apenas o primeiro gênero listado
df_filmes <- df_filmes %>%mutate(
    primeiro_genero = sapply(strsplit(listed_in, ","), `[`, 1) %>% trimws()
  ) %>%filter(primeiro_genero %in% c("Dramas", "Action & Adventure", "Comedies"))

# 4. Conta quantos filmes de cada gênero por ano
counts <- df_filmes %>% count(release_year, primeiro_genero, name = "qtde")


plot_ly( data = counts,
  x = ~release_year,
  y = ~qtde,
  color = ~primeiro_genero,
  colors = c("Dramas" = "#1f77b4",          
             "Action & Adventure" = "#ff7f0e", 
             "Comedies" = "#2ca02c"),       
  type = "bar"
) %>%
  layout(
    barmode = "group",
    title   = "Filmes por Gênero (2000–2010)",
    xaxis   = list(title = "Ano"),
    yaxis   = list(title = "Quantidade de Filmes"),
    legend  = list(title = list(text = "<b>Gênero</b>"))
  )