Respostas

Questão 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", pch = 4, col = "black", lwd = 2,
     xlab = "Time between Things requests (seconds)",
     ylab = "Response Time (sec.)",
     ylim = c(0, 550))

lines(clock, MRT_3F, type = "o", pch = 17, col = "orange", lwd = 2)
lines(clock, MRT_5F, type = "o", pch = 15, col = "yellow", lwd = 2)
lines(clock, MRT_10F, type = "o", pch = 2, col = "blue", lwd = 2)
lines(clock, MRT_15F, type = "o", pch = 0, col = "purple", lwd = 2)
lines(clock, MRT_sem_F, type = "o", pch = 8, col = "green", lwd = 2)


legend("topright",
       legend = c("1 Fog", "3 Fogs", "5 Fogs", "10 Fogs", "15 Fogs", "w/o Fog"),
       col = c("black", "orange", "yellow", "blue", "purple", "green"),
       pch = c(4, 17, 15, 2, 0, 8),
       lty = 1,
       cex = 0.8)

par(mfrow = c(3, 2))

plot_barras <- function(y_values, title) {
  barplot(rbind(MRT_sem_F, y_values),
          beside = TRUE,
          names.arg = clock,
          col = c("#E6E6E6", "#666666"),
          log = "y",
          ylim = c(0.1, max(MRT_sem_F, y_values)*1.5),
          xlab = "Time between Things requests",
          ylab = "Response time (s)")
  legend("topright",
         inset = c(-0.1, -0.3),
         xpd = TRUE,
         legend = c("w/o Fog", title),
         fill = c("#E6E6E6", "#666666"),
         bty = "n")
}

plot_barras(MRT_1F, "1 Fog")
plot_barras(MRT_3F, "3 Fogs")
plot_barras(MRT_5F, "5 Fogs")
plot_barras(MRT_10F, "10 Fogs")
plot_barras(MRT_15F, "15 Fogs")

Questão 2

colors <- c("lightgreen", "green", "darkgreen")
good <- c(53.8, 33.9, 2.6, 0.0)
veryGood <- c(43.6, 54.2, 60.5, 21.4)
excellent <- c(2.6, 11.9, 36.8, 78.6)

data_matrix <- matrix(c(
  good,
  veryGood,
  excellent
), nrow = 3, ncol=4, byrow = TRUE)

category <- c("$10-19", "$20-29", "$30-39", "$40-49")

barplot(data_matrix, col = colors, names.arg = category,
        xlab = "Prices", ylab = "Quality Rating")


legend("topright", pch = c(15,15,15),  legend = c("good", "very good", "excellent"), col= colors)

Questão 3

data("airquality")

airquality$TempC <- (airquality$Temp - 32) / 1.8
maio <- subset(airquality, Month == 5)

hist(
  maio$TempC,
  main = "Histograma das Temperaturas em Maio (°C)",
  xlab = "temperatura (°C)",
  ylab = "frequência",
  col = "skyblue",
  border = "white",
  freq = FALSE
)

lines(density(maio$TempC, na.rm = TRUE), 
      col = "black", 
      lwd = 2)

Questão 4

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

pais <- c(sales$COUNTRY)
vendas <- c(sales$SALES)

percentage <-round(vendas/sum(vendas)*100)
labels <- paste(percentage,"%",sep="")

pie(vendas, labels, main = "Total de vendas por pais", col = rainbow(length(vendas)))
legend("topleft", legend = pais, fill = rainbow(length(vendas)))

Questão 5

data <- InsectSprays
boxplot(
  data = data,
  count ~ spray,
  col = c("yellow"),
  main = "Insect Sprays",
  xlab = "Sprays",
  ylab="Quantity",
  outline = FALSE
)

Questão 7

library(readr)
## Warning: package 'readr' was built under R version 4.3.3
library(plotly)
## Carregando pacotes exigidos: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
url <- "https://www.dropbox.com/scl/fi/vjlgt50835d6snk03add2/netflix_titles.csv?rlkey=rzrveurxlom9cjp51nbv4w1gw&raw=1"

netflix <- read_csv(url)
## 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.
netflix_country <- netflix[grep(",", netflix$country, invert = TRUE), ]

order_country <- netflix_country %>% group_by(country) %>% summarise( Content = sum(length(title), ma.rm = TRUE )) %>% arrange(desc(Content))
order_country <- slice(filter(order_country, !(is.na(country))), 1:10)

graph7 <- plot_ly(
  order_country,
  labels=~country,
  values=~Content,
  type = 'pie'
  )
graph7

Questão 8

graph8 <- plot_ly(
  type = 'table',
  header = list(
    values = c("País", "Total de conteúdos"),
    align = c("center", "center"),
    line = list(width = 1, color = "black"),
    fill = list(color = "grey"),
    font = list(color = "white")
    ),
  cells = list(
    values = rbind(order_country$country, order_country$Content),
    align = c("center", "center"),
    line = list(color = "black", width = 1),
    font = list(color = c("black"))
    )
  )

graph8

Questão 9

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## 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
decadas <- netflix %>%
  mutate(decade = floor(release_year / 10) * 10) %>%
  group_by(decade, type) %>%
  summarise(quantity = n())
## `summarise()` has grouped output by 'decade'. You can override using the
## `.groups` argument.
series <- filter(decadas, type != "Movie")
movies <- filter(decadas, type == "Movie")

line <- plot_ly(
  data = data.frame(series),
  x= ~decade,
  y= ~quantity,
  type = "scatter",
  mode = "lines+markers",
  line = list(color = "blue"),
  name = "TV Series"
  )

line <- line %>% add_trace(
   data = data.frame(movies),
   x= ~decade,
   y= ~quantity,
   type = "scatter",
   mode = "lines+markers",
   line = list(color = "orange"),
   name = "Movies"
  )

line <- line %>% layout(
        xaxis = list(title = "Década"),
        yaxis = list(title = "Qnd.Conteúdo")
        )
line