EXERCICIO 12

Carregando bibliotecas que usarei

library(ggplot2)
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
library(dplyr)
library(readr)
library(tidyr)

Questões

Questao 1

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)


clock <- c(0.1, 0.5, 1, 1.5, 2, 2.5, 3)

plot(clock, MRT_1F, type = "o", col = "black", pch = 16, lty = 1, xlab = "Time between requests (seconds)", ylab = "Response Time (sec)")

lines(clock, MRT_3F, type = "o", col = "yellow", pch = 17, lty = 2)
lines(clock, MRT_5F, type = "o", col = "red", pch = 18, lty = 3)
lines(clock, MRT_10F, type = "o", col = "blue", pch = 19, lty = 4)
lines(clock, MRT_15F, type = "o", col = "purple", pch = 15, lty = 5)
lines(clock, MRT_sem_F, type = "o", col = "green", pch = 8, lty = 6)

legend("topright", 
       legend = c("1 Fog", "3 Fogs", "5 Fogs", "10 Fogs", "15 Fogs", "w/o Fog"), 
       col = c("black", "yellow", "red", "blue", "purple", "green"), 
       pch = c(16, 17, 18, 19, 15, 8), 
       lty = 1:6, # Tipos de linha diferentes
       title = "Categorias")

layout(matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, ncol = 2, byrow = TRUE))


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

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

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

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

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

Questao 2

dados <- data.frame(
  Quality_Rating = c("Good", "Very Good", "Excellent"),
  `10_19` = c(53.8, 43.6, 2.6),
  `20_29` = c(33.9, 54.2, 11.9),
  `30_39` = c(2.6, 60.5, 36.8),
  `40_49` = c(0.0, 21.4, 78.6)
)

barplot(as.matrix(dados[, -1]), 
        col = c("skyblue", "orange", "green"), 
        names.arg = c("10-19", "20-29", "30-39", "40-49"),
        main = "Classificação da Qualidade da Refeição por Faixa de Preço",
        xlab = "Faixa de Preço (USD)",
        ylab = "Porcentagem")
legend("topright", legend = dados$Quality_Rating, 
       fill = c("skyblue", "orange", "green"), title = "Qualidade")

Questao 3

data(airquality)
maio <-subset(airquality,Month==5)
maio$TempCelsius <- (maio$Temp-32)/1.8
hist(maio$TempCelsius,main = "Histograma das Temperaturas em Maio",
     xlab="Temperatura (Celsisus)",ylab="Frequencia",col="lightblue")

hist(maio$TempCelsius,main = "Histograma das Temperaturas em Maio com Curava de Densidade",
     xlab="Temperatura (Celsisus)",ylab="Frequencia",col="lightblue",probability=TRUE)
lines(density(maio$TempCelsius),col = "red")

Questao 4

sales <- read.table("https://training-course-material.com/images/8/8f/Sales.txt",header=TRUE)
cores <- rainbow(nrow(sales))
total_sales <- sum(sales$SALES)
sales$PERCENTAGE <- round(sales$SALES / total_sales * 100, 1)
sales
##   COUNTRY SALES PERCENTAGE
## 1      US   340       11.9
## 2      UK   290       10.1
## 3  France   510       17.8
## 4  Poland   820       28.7
## 5   Japan   120        4.2
## 6   China   780       27.3
pie(sales$SALES,
    labels = sales$PERCENTAGE,
    col = cores,
    main = "Distribuição de Vendas por País")

legend("topright", 
       legend = sales$COUNTRY,
       fill = cores,
       cex = 0.8)

Questao 5

data(InsectSprays)


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

Questao 6

Aqui vai o conteúdo da Questão 6.

Questao 7

netflix_data <- read.csv("netflix_titles.csv")
single_country_data <- netflix_data %>%
  filter(!grepl(",", country)) %>%
  filter(!is.na(country) & country != "") %>%  
  count(country) %>%  
  arrange(desc(n)) %>%  
  slice_max(order_by = n, n = 10)  


fig <- plot_ly(single_country_data, 
               labels = ~country, 
               values = ~n, 
               type = 'pie', 
               textinfo = 'label+percent', 
               insidetextorientation = 'radial')

fig <- fig %>% layout(title = 'Top 10 países com mais conteúdos na plataforma')

fig

Foram mostrados 11 países pois tem um empate

Questao 8

fig_table <- plot_ly(
  type = 'table',
  header = list(
    values = c("País", "Total de conteúdos"),
    fill = list(color = "gray"),  
    font = list(color = "white"), 
    align = "center"  
  ),
  cells = list(
    values = rbind(single_country_data$country, single_country_data$n),
    align = "center" 
  )
)

fig_table

Questao 9

netflix_data2 <- netflix_data %>%
  mutate(decade = (release_year %/% 10) * 10)  

# pegando dados de filmes
movies_data <- netflix_data2 %>%
  filter(type == "Movie") %>%
  count(decade)

# pegnado dads de series de tv
series_data <- netflix_data2 %>%
  filter(type == "TV Show") %>%
  count(decade)

fig <- plot_ly() %>%
  add_trace(data = movies_data, 
            x = ~decade, 
            y = ~n, 
            type = 'scatter', 
            mode = 'lines+markers', 
            name = 'Movies',
            line = list(color = 'orange')) %>%
  add_trace(data = series_data, 
            x = ~decade, 
            y = ~n, 
            type = 'scatter', 
            mode = 'lines+markers', 
            name = 'TV Series',
            line = list(color = 'blue')) %>%
  layout(title = 'Quantidade de Conteúdo por Década no Netflix',
         xaxis = list(title = 'Década'),
         yaxis = list(title = 'Qtd. de Conteúdo'))

fig

Questao 10

netflix_data3 <- netflix_data %>%
  filter(release_year >= 2000 & release_year <= 2010)

# Extract the first genre listed
netflix_data3$first_genre <- sapply(strsplit(netflix_data3$listed_in, ","), `[`, 1)

# Filter relevant genres
genres <- c("Dramas", "Action & Adventure", "Comedies")
netflix_data3 <- netflix_data3 %>%
  filter(first_genre %in% genres)

# Group by year and genre, then count occurrences
data <- netflix_data3 %>%
  group_by(release_year, first_genre) %>%
  summarize(count = n(), .groups = 'drop') %>%
  pivot_wider(names_from = first_genre, values_from = count, values_fill = 0)

# Create the plotly bar chart
fig <- plot_ly(data, x = ~release_year, y = ~Dramas, type = 'bar', name = 'Dramas')
fig <- fig %>% add_trace(y = ~`Action & Adventure`, name = 'Action & Adventure')
fig <- fig %>% add_trace(y = ~Comedies, name = 'Comedies')

# Layout adjustments
fig <- fig %>% layout(
  barmode = 'group',
  xaxis = list(title = "Ano de Lançamento"),
  yaxis = list(title = "Qnt. de Lançamentos"),
  title = "Quantidade de Filmes Lançados por Gênero (2000-2010)"
)

# Show the plot
fig