Exercício 12 - Visualização de Dados - R

Questões de 1 a 5 foram feitas com o R tradicional e as questões de 6 a 10 utilizaram o Plotly.

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,
     cex=2, col="black",
     xlab= "Time between Things requests (s)",
     ylab= "Response Time (s)",
     cex.lab=1.0, cex.axis=1,
     cex.main=2,
     xlim = c(0,3),
     ylim = c(0,600)
     )
lines(x= clock,y = MRT_3F, type ="o", pch=0, col = "yellow")
lines(x= clock,y = MRT_5F, type ="o",pch=1, col = "red")
lines(x= clock,y = MRT_10F, type ="o",pch=2, col = "blue")
lines(x= clock,y = MRT_15F, type ="o",pch=5, col = "purple")
lines(x= clock,y = MRT_sem_F, type ="o", pch=6, col = "green")

legend("topright",
     col= c("black","yellow","red","blue","purple","green"),
     legend = c("1 Fog","3 Fogs","5 Fogs","10 Fogs","15 Fogs","w/o Fog"),
     pch = c(4, 0, 1, 2, 5, 6),
     lty = 1,
     bty = "n",
     x.intersp =0.5, y.intersp = 0.5)

#grafico em barras
par(mfrow = c(2, 3))

cores <- c("#E6E6E6", "#666666")  # cinza claro e cinza escuro

barplot(rbind(MRT_sem_F, MRT_1F), beside = TRUE, col = cores, log = "y",
        names.arg = clock, xlab = "Time between Things requests",
        ylab = "Response time (s)", main = "1 Fog", ylim=c(0.1,600))
legend("topright", legend = c("w/o Fog", "1 Fog"),   x.intersp =0.5, y.intersp = 0.5, fill = cores)


barplot(rbind(MRT_sem_F, MRT_3F), beside = TRUE, col = cores, log = "y",
        names.arg = clock, xlab = "Time between Things requests",
        ylab = "Response time (s)", main = "3 Fog")
legend("topright", legend = c("w/o Fog", "3 Fog"),  x.intersp =0.5, y.intersp = 0.5, fill = cores)


barplot(rbind(MRT_sem_F, MRT_5F), beside = TRUE, col = cores, log = "y",
        names.arg = clock, xlab = "Time between Things requests",
        ylab = "Response time (s)", main = "5 Fog")
legend("topright", legend = c("w/o Fog", "5 Fog"), x.intersp =0.5, y.intersp = 0.5, fill = cores)


barplot(rbind(MRT_sem_F, MRT_10F), beside = TRUE, col = cores, log = "y",
        names.arg = clock, xlab = "Time between Things requests",
        ylab = "Response time (s)", main = "10 Fog")
legend("topright", legend = c("w/o Fog", "10 Fog"),  x.intersp =0.5, y.intersp = 0.5,fill = cores)


barplot(rbind(MRT_sem_F, MRT_15F), beside = TRUE, col = cores, log = "y",
        names.arg = clock, xlab = "Time between Things requests",
        ylab = "Response time (s)", main = "15 Fog")
legend("topright", legend = c("w/o Fog", "15 Fog"),  x.intersp =0.5, y.intersp = 0.5, fill = cores)

Questão 2

# Dados da tabela (valores em porcentagem)
MealPrice <- c("$10–19", "$20–29", "$30–39", "$40–49")
Qual.Rating <- c("Good", "Very Good", "Excellent")
Good <- c(53.8, 33.9,2.6, 0)
VeryGood <- c(43.6, 54.2, 60.5, 21.4)
Excellent <-c(2.6, 11.9, 36.8, 78.6)

# Criando a matriz de dados (linhas = qualidade, colunas = faixa de preço)
dados <- rbind(Good, VeryGood, Excellent)

# Nomeando as colunas (faixas de preço)
colnames(dados) <- MealPrice

# Transpor para empilhar por faixa de preço
dados_t <- t(dados)

# Cores
cores <- c("orange", "skyblue", "seagreen")

# Gráfico
barplot(dados, beside = FALSE, col = cores,
        main = "Meal Price Range Quality Distribution",
        xlab = "Meal Price",
        ylab = "Quality Rating",
        legend.text = TRUE,
        args.legend = list(title = "Quality", x = "topright"))

Questão 3

# Dataset airquality já disponível no R
data("airquality")

# Filtrar apenas o mês de maio
maio <- subset(airquality, Month == 5)

# Converter temperatura de Fahrenheit para Celsius
temp_celsius <- (maio$Temp - 32) / 1.8

# Histograma com curva de densidade
hist(temp_celsius, breaks = 10, col = "lightblue", prob = TRUE,
     main = "Histograma das Temperaturas em Maio (°C)",
     xlab = "Temperatura (°C)",
     ylab = "Densidade")

# Adicionar curva de densidade
lines(density(temp_celsius, na.rm = TRUE), col = "darkblue", lwd = 2)

Questão 4

# Carregar dados
sales <- read.table("https://training-course-material.com/images/8/8f/Sales.txt", header = TRUE)
str(sales)
## 'data.frame':    6 obs. of  2 variables:
##  $ COUNTRY: chr  "US" "UK" "France" "Poland" ...
##  $ SALES  : int  340 290 510 820 120 780
#proporções
pct <- round(sales$SALES/sum(sales$SALES)*100)
Ibis <- paste(sales$COUNTRY,pct)
Ibis <- paste(Ibis,"%",sep=" ")

# Criar gráfico de pizza
pie(pct,labels = Ibis, main= "Vendas no mundo", col = rainbow(6))

legend("topleft", legend = c(sales$COUNTRY), fill = rainbow(6))

Questão 5

# Questão 5
InsectSprays
##    count spray
## 1     10     A
## 2      7     A
## 3     20     A
## 4     14     A
## 5     14     A
## 6     12     A
## 7     10     A
## 8     23     A
## 9     17     A
## 10    20     A
## 11    14     A
## 12    13     A
## 13    11     B
## 14    17     B
## 15    21     B
## 16    11     B
## 17    16     B
## 18    14     B
## 19    17     B
## 20    17     B
## 21    19     B
## 22    21     B
## 23     7     B
## 24    13     B
## 25     0     C
## 26     1     C
## 27     7     C
## 28     2     C
## 29     3     C
## 30     1     C
## 31     2     C
## 32     1     C
## 33     3     C
## 34     0     C
## 35     1     C
## 36     4     C
## 37     3     D
## 38     5     D
## 39    12     D
## 40     6     D
## 41     4     D
## 42     3     D
## 43     5     D
## 44     5     D
## 45     5     D
## 46     5     D
## 47     2     D
## 48     4     D
## 49     3     E
## 50     5     E
## 51     3     E
## 52     5     E
## 53     3     E
## 54     6     E
## 55     1     E
## 56     1     E
## 57     3     E
## 58     2     E
## 59     6     E
## 60     4     E
## 61    11     F
## 62     9     F
## 63    15     F
## 64    22     F
## 65    15     F
## 66    16     F
## 67    13     F
## 68    10     F
## 69    26     F
## 70    26     F
## 71    24     F
## 72    13     F
str(InsectSprays)
## 'data.frame':    72 obs. of  2 variables:
##  $ count: num  10 7 20 14 14 12 10 23 17 20 ...
##  $ spray: Factor w/ 6 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...
head(InsectSprays)
##   count spray
## 1    10     A
## 2     7     A
## 3    20     A
## 4    14     A
## 5    14     A
## 6    12     A
boxplot(count ~ spray, data=InsectSprays,
        xlab = "Type of spray",
        ylab = "Insect count", 
        main = "InsectSprays data",
        outline=FALSE)

Questão 6

#diretório
setwd(  "A:/UFRPE/RCAD/monitoringCloudData")
Data0.1 <- read.csv("monitoringCloudData_0.1.csv") 
Data0.5 <- read.csv("monitoringCloudData_0.5.csv")
Data1.0 <- read.csv("monitoringCloudData_1.csv")
DataN <- read.csv("monitoringCloudData_NONE.csv")

head(Data0.1)
##                  currentTime totalCpuUsage... totalMemory availableMemory
## 1 2020-01-28 19:05:24.696535                0      3.62GB          3.16GB
## 2 2020-01-28 19:06:24.960884                0      3.62GB          3.16GB
## 3 2020-01-28 19:07:25.178265                0      3.62GB          3.16GB
## 4 2020-01-28 19:08:25.422138                0      3.62GB          3.16GB
## 5 2020-01-28 19:09:25.684460                0      3.62GB          3.17GB
## 6 2020-01-28 19:10:25.946943                0      3.62GB          3.16GB
##   usedMemory percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1   247.09MB                12.5     0.00B    0.00B    0.00B                 0
## 2   247.34MB                12.5     0.00B    0.00B    0.00B                 0
## 3   247.29MB                12.5     0.00B    0.00B    0.00B                 0
## 4   247.02MB                12.5     0.00B    0.00B    0.00B                 0
## 5   245.26MB                12.5     0.00B    0.00B    0.00B                 0
## 6   247.49MB                12.5     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.33GB        3.94GB                   57.5
## 2         9.78GB        5.33GB        3.94GB                   57.5
## 3         9.78GB        5.33GB        3.94GB                   57.5
## 4         9.78GB        5.33GB        3.94GB                   57.5
## 5         9.78GB        5.33GB        3.94GB                   57.5
## 6         9.78GB        5.33GB        3.94GB                   57.5
##   total_IO_read total_IO_write
## 1      291.32MB         6.74MB
## 2      291.32MB         7.05MB
## 3      291.32MB         7.25MB
## 4      291.32MB         7.47MB
## 5      291.32MB         7.71MB
## 6      291.32MB         7.96MB
head(Data0.5)
##                  currentTime totalCpuUsage... totalMemory availableMemory
## 1 2020-01-28 15:14:55.878829              0.0      3.62GB          3.15GB
## 2 2020-01-28 15:15:56.127157              0.0      3.62GB          3.15GB
## 3 2020-01-28 15:16:56.385147              9.1      3.62GB          3.15GB
## 4 2020-01-28 15:17:56.647501              0.0      3.62GB          3.15GB
## 5 2020-01-28 15:18:56.910265              0.0      3.62GB          3.15GB
## 6 2020-01-28 15:19:57.172668              0.0      3.62GB          3.14GB
##   usedMemory percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1   257.46MB                12.8     0.00B    0.00B    0.00B                 0
## 2   261.70MB                12.9     0.00B    0.00B    0.00B                 0
## 3   265.79MB                13.0     0.00B    0.00B    0.00B                 0
## 4   266.47MB                13.1     0.00B    0.00B    0.00B                 0
## 5   266.17MB                13.0     0.00B    0.00B    0.00B                 0
## 6   269.06MB                13.1     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.34GB        3.93GB                   57.6
## 2         9.78GB        5.34GB        3.93GB                   57.6
## 3         9.78GB        5.34GB        3.93GB                   57.6
## 4         9.78GB        5.34GB        3.93GB                   57.6
## 5         9.78GB        5.34GB        3.93GB                   57.6
## 6         9.78GB        5.34GB        3.93GB                   57.6
##   total_IO_read total_IO_write
## 1      292.50MB        15.57MB
## 2      292.50MB        15.79MB
## 3      292.50MB        16.07MB
## 4      292.50MB        16.38MB
## 5      292.50MB        16.46MB
## 6      292.50MB        16.70MB
head(Data1.0)
##                  currentTime totalCpuUsage... totalMemory availableMemory
## 1 2020-01-28 21:16:18.220357                0      3.62GB          3.17GB
## 2 2020-01-28 21:17:18.482927                0      3.62GB          3.17GB
## 3 2020-01-28 21:18:18.700329                0      3.62GB          3.17GB
## 4 2020-01-28 21:19:18.963742                0      3.62GB          3.17GB
## 5 2020-01-28 21:20:19.212354                0      3.62GB          3.17GB
## 6 2020-01-28 21:21:19.474764                0      3.62GB          3.17GB
##   usedMemory percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1   243.19MB                12.4     0.00B    0.00B    0.00B                 0
## 2   242.52MB                12.4     0.00B    0.00B    0.00B                 0
## 3   241.74MB                12.4     0.00B    0.00B    0.00B                 0
## 4   241.23MB                12.4     0.00B    0.00B    0.00B                 0
## 5   243.61MB                12.4     0.00B    0.00B    0.00B                 0
## 6   242.70MB                12.4     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.39GB        3.88GB                   58.1
## 2         9.78GB        5.39GB        3.88GB                   58.1
## 3         9.78GB        5.39GB        3.88GB                   58.1
## 4         9.78GB        5.39GB        3.88GB                   58.1
## 5         9.78GB        5.39GB        3.88GB                   58.1
## 6         9.78GB        5.39GB        3.88GB                   58.1
##   total_IO_read total_IO_write
## 1      292.33MB        14.85MB
## 2      292.33MB        15.06MB
## 3      292.33MB        15.31MB
## 4      292.33MB        15.51MB
## 5      292.33MB        15.73MB
## 6      292.33MB        15.96MB
head(DataN)
##                  currentTime totalCpuUsage... totalMemory availableMemory
## 1 2020-01-28 15:37:52.452600              0.0      3.62GB          3.35GB
## 2 2020-01-28 15:38:52.718592              9.1      3.62GB          3.35GB
## 3 2020-01-28 15:39:52.981509              0.0      3.62GB          3.35GB
## 4 2020-01-28 15:40:53.245219              0.0      3.62GB          3.35GB
## 5 2020-01-28 15:41:53.509300              0.0      3.62GB          3.35GB
## 6 2020-01-28 15:42:53.773309              0.0      3.62GB          3.35GB
##   usedMemory percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1   102.94MB                 7.4     0.00B    0.00B    0.00B                 0
## 2   103.36MB                 7.4     0.00B    0.00B    0.00B                 0
## 3   100.23MB                 7.3     0.00B    0.00B    0.00B                 0
## 4   100.19MB                 7.3     0.00B    0.00B    0.00B                 0
## 5   100.12MB                 7.3     0.00B    0.00B    0.00B                 0
## 6   100.01MB                 7.3     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.28GB        3.98GB                     57
## 2         9.78GB        5.28GB        3.98GB                     57
## 3         9.78GB        5.28GB        3.98GB                     57
## 4         9.78GB        5.28GB        3.98GB                     57
## 5         9.78GB        5.28GB        3.98GB                     57
## 6         9.78GB        5.28GB        3.98GB                     57
##   total_IO_read total_IO_write
## 1       84.64MB         1.65MB
## 2       84.65MB         2.10MB
## 3       84.65MB         2.26MB
## 4       84.71MB         2.77MB
## 5       84.71MB         2.85MB
## 6       84.71MB         2.96MB
# é necessário ajustar a coluna “currentTime” para que o tempo fique contínuo 
#e a coluna “usedMemory” para que todas as informações fiquem megabytes.

# Converter para data-hora
Data0.1$currentTime <- as.POSIXct(Data0.1$currentTime)
Data0.5$currentTime <- as.POSIXct(Data0.5$currentTime)
Data1.0$currentTime <- as.POSIXct(Data1.0$currentTime)
DataN$currentTime  <- as.POSIXct(DataN$currentTime)

head(Data0.1$currentTime)
## [1] "2020-01-28 19:05:24 -03" "2020-01-28 19:06:24 -03"
## [3] "2020-01-28 19:07:25 -03" "2020-01-28 19:08:25 -03"
## [5] "2020-01-28 19:09:25 -03" "2020-01-28 19:10:25 -03"
head(Data0.5)
##   currentTime totalCpuUsage... totalMemory availableMemory usedMemory
## 1  2020-01-28              0.0      3.62GB          3.15GB   257.46MB
## 2  2020-01-28              0.0      3.62GB          3.15GB   261.70MB
## 3  2020-01-28              9.1      3.62GB          3.15GB   265.79MB
## 4  2020-01-28              0.0      3.62GB          3.15GB   266.47MB
## 5  2020-01-28              0.0      3.62GB          3.15GB   266.17MB
## 6  2020-01-28              0.0      3.62GB          3.14GB   269.06MB
##   percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1                12.8     0.00B    0.00B    0.00B                 0
## 2                12.9     0.00B    0.00B    0.00B                 0
## 3                13.0     0.00B    0.00B    0.00B                 0
## 4                13.1     0.00B    0.00B    0.00B                 0
## 5                13.0     0.00B    0.00B    0.00B                 0
## 6                13.1     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.34GB        3.93GB                   57.6
## 2         9.78GB        5.34GB        3.93GB                   57.6
## 3         9.78GB        5.34GB        3.93GB                   57.6
## 4         9.78GB        5.34GB        3.93GB                   57.6
## 5         9.78GB        5.34GB        3.93GB                   57.6
## 6         9.78GB        5.34GB        3.93GB                   57.6
##   total_IO_read total_IO_write
## 1      292.50MB        15.57MB
## 2      292.50MB        15.79MB
## 3      292.50MB        16.07MB
## 4      292.50MB        16.38MB
## 5      292.50MB        16.46MB
## 6      292.50MB        16.70MB
head(Data1.0)
##           currentTime totalCpuUsage... totalMemory availableMemory usedMemory
## 1 2020-01-28 21:16:18                0      3.62GB          3.17GB   243.19MB
## 2 2020-01-28 21:17:18                0      3.62GB          3.17GB   242.52MB
## 3 2020-01-28 21:18:18                0      3.62GB          3.17GB   241.74MB
## 4 2020-01-28 21:19:18                0      3.62GB          3.17GB   241.23MB
## 5 2020-01-28 21:20:19                0      3.62GB          3.17GB   243.61MB
## 6 2020-01-28 21:21:19                0      3.62GB          3.17GB   242.70MB
##   percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1                12.4     0.00B    0.00B    0.00B                 0
## 2                12.4     0.00B    0.00B    0.00B                 0
## 3                12.4     0.00B    0.00B    0.00B                 0
## 4                12.4     0.00B    0.00B    0.00B                 0
## 5                12.4     0.00B    0.00B    0.00B                 0
## 6                12.4     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.39GB        3.88GB                   58.1
## 2         9.78GB        5.39GB        3.88GB                   58.1
## 3         9.78GB        5.39GB        3.88GB                   58.1
## 4         9.78GB        5.39GB        3.88GB                   58.1
## 5         9.78GB        5.39GB        3.88GB                   58.1
## 6         9.78GB        5.39GB        3.88GB                   58.1
##   total_IO_read total_IO_write
## 1      292.33MB        14.85MB
## 2      292.33MB        15.06MB
## 3      292.33MB        15.31MB
## 4      292.33MB        15.51MB
## 5      292.33MB        15.73MB
## 6      292.33MB        15.96MB
head(DataN)
##           currentTime totalCpuUsage... totalMemory availableMemory usedMemory
## 1 2020-01-28 15:37:52              0.0      3.62GB          3.35GB   102.94MB
## 2 2020-01-28 15:38:52              9.1      3.62GB          3.35GB   103.36MB
## 3 2020-01-28 15:39:52              0.0      3.62GB          3.35GB   100.23MB
## 4 2020-01-28 15:40:53              0.0      3.62GB          3.35GB   100.19MB
## 5 2020-01-28 15:41:53              0.0      3.62GB          3.35GB   100.12MB
## 6 2020-01-28 15:42:53              0.0      3.62GB          3.35GB   100.01MB
##   percentageMemory... totalSwap freeSwap usedSwap percentageSwap...
## 1                 7.4     0.00B    0.00B    0.00B                 0
## 2                 7.4     0.00B    0.00B    0.00B                 0
## 3                 7.3     0.00B    0.00B    0.00B                 0
## 4                 7.3     0.00B    0.00B    0.00B                 0
## 5                 7.3     0.00B    0.00B    0.00B                 0
## 6                 7.3     0.00B    0.00B    0.00B                 0
##   totalPartition usedPartition freePartition percentagePartition...
## 1         9.78GB        5.28GB        3.98GB                     57
## 2         9.78GB        5.28GB        3.98GB                     57
## 3         9.78GB        5.28GB        3.98GB                     57
## 4         9.78GB        5.28GB        3.98GB                     57
## 5         9.78GB        5.28GB        3.98GB                     57
## 6         9.78GB        5.28GB        3.98GB                     57
##   total_IO_read total_IO_write
## 1       84.64MB         1.65MB
## 2       84.65MB         2.10MB
## 3       84.65MB         2.26MB
## 4       84.71MB         2.77MB
## 5       84.71MB         2.85MB
## 6       84.71MB         2.96MB
convert_to_mb <- function(x) {
  x <- gsub(" ", "", x)  # remove espaços
  num <- as.numeric(sub("[A-Za-z]+", "", x))
  unit <- toupper(gsub("[0-9.]", "", x))
  ifelse(unit == "GB", num * 1024, num)
}

# Função para converter qualquer unidade (TB, GB, MB) para MB
convert_to_mb <- function(x) {
  x <- gsub(" ", "", x)  # remove espaços
  num <- as.numeric(sub("[A-Za-z]+", "", x))
  unit <- toupper(gsub("[0-9.]", "", x))
  
  converted <- ifelse(unit == "TB", num * 1e6,
                      ifelse(unit == "GB", num * 1024,
                             ifelse(unit == "MB", num, NA)))
  return(converted)
}
Data0.1$usedMemory_MB <- convert_to_mb(Data0.1$usedMemory)
Data0.5$usedMemory_MB <- convert_to_mb(Data0.5$usedMemory)
Data1.0$usedMemory_MB <- convert_to_mb(Data1.0$usedMemory)
DataN$usedMemory_MB   <- convert_to_mb(DataN$usedMemory)

# Converter para POSIXct
Data0.1$currentTime <- as.POSIXct(Data0.1$currentTime)
Data0.5$currentTime <- as.POSIXct(Data0.5$currentTime)
Data1.0$currentTime <- as.POSIXct(Data1.0$currentTime)
DataN$currentTime  <- as.POSIXct(DataN$currentTime)

# Calcular tempo decorrido em horas (a partir da primeira observação)
Data0.1$time_hr <- as.numeric(difftime(Data0.1$currentTime, Data0.1$currentTime[1], units = "hours"))
Data0.5$time_hr <- as.numeric(difftime(Data0.5$currentTime, Data0.5$currentTime[1], units = "hours"))
Data1.0$time_hr <- as.numeric(difftime(Data1.0$currentTime, Data1.0$currentTime[1], units = "hours"))
DataN$time_hr   <- as.numeric(difftime(DataN$currentTime,  DataN$currentTime[1],  units = "hours"))

# Definir layout: 2 linhas, 2 colunas
layout(matrix(1:4, nrow = 2, ncol = 2))

# Gráfico 1
plot(Data0.1$time_hr, Data0.1$usedMemory_MB, type = "l", col = "blue",
     xlab = "Time (hour)", ylab = "Used Memory (MB)",
     main = "Memory Analysis (Workload of 0.1)")

# Gráfico 2
plot(Data0.5$time_hr, Data0.5$usedMemory_MB, type = "l", col = "green",
     xlab = "Time (hour)", ylab = "Used Memory (MB)",
     main = "Memory Analysis (Workload of 0.5)")

# Gráfico 3
plot(Data1.0$time_hr, Data1.0$usedMemory_MB, type = "l", col = "red",
     xlab = "Time (hour)", ylab = "Used Memory (MB)",
     main = "Memory Analysis (Workload of 1.0)")

# Gráfico 4
plot(DataN$time_hr, DataN$usedMemory_MB, type = "l", col = "purple",
     xlab = "Time (hour)", ylab = "Used Memory (MB)",
     main = "Memory Analysis (None Workload)")

Questão 7

library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.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
library(plotly)
## Warning: package 'plotly' was built under R version 4.2.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.3
## 
## 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
# diretório
setwd(  "A:/UFRPE/RCAD")
netflix <- read.csv("netflix_titles.csv")
head(netflix)
##   show_id    type title          director
## 1      s1 TV Show    3%                  
## 2      s2   Movie  7:19 Jorge Michel Grau
## 3      s3   Movie 23:59      Gilbert Chan
## 4      s4   Movie     9       Shane Acker
## 5      s5   Movie    21    Robert Luketic
## 6      s6 TV Show    46       Serdar Akar
##                                                                                                                                                                         cast
## 1 João Miguel, Bianca Comparato, Michel Gomes, Rodolfo Valente, Vaneza Oliveira, Rafael Lozano, Viviane Porto, Mel Fronckowiak, Sergio Mamberti, Zezé Motta, Celso Frateschi
## 2                                                                                   Demián Bichir, Héctor Bonilla, Oscar Serrano, Azalia Ortiz, Octavio Michel, Carmen Beato
## 3                                                               Tedd Chan, Stella Chung, Henley Hii, Lawrence Koh, Tommy Kuan, Josh Lai, Mark Lee, Susan Leong, Benjamin Lim
## 4                            Elijah Wood, John C. Reilly, Jennifer Connelly, Christopher Plummer, Crispin Glover, Martin Landau, Fred Tatasciore, Alan Oppenheimer, Tom Kane
## 5            Jim Sturgess, Kevin Spacey, Kate Bosworth, Aaron Yoo, Liza Lapira, Jacob Pitts, Laurence Fishburne, Jack McGee, Josh Gad, Sam Golzari, Helen Carey, Jack Gilpin
## 6                            Erdal Beşikçioğlu, Yasemin Allen, Melis Birkan, Saygın Soysal, Berkan Şal, Metin Belgin, Ayça Eren, Selin Uludoğan, Özay Fecht, Suna Yıldızoğlu
##         country        date_added release_year rating  duration
## 1        Brazil   August 14, 2020         2020  TV-MA 4 Seasons
## 2        Mexico December 23, 2016         2016  TV-MA    93 min
## 3     Singapore December 20, 2018         2011      R    78 min
## 4 United States November 16, 2017         2009  PG-13    80 min
## 5 United States   January 1, 2020         2008  PG-13   123 min
## 6        Turkey      July 1, 2017         2016  TV-MA  1 Season
##                                                  listed_in
## 1   International TV Shows, TV Dramas, TV Sci-Fi & Fantasy
## 2                             Dramas, International Movies
## 3                      Horror Movies, International Movies
## 4 Action & Adventure, Independent Movies, Sci-Fi & Fantasy
## 5                                                   Dramas
## 6          International TV Shows, TV Dramas, TV Mysteries
##                                                                                                                                             description
## 1              In a future where the elite inhabit an island paradise far from the crowded slums, you get one chance to join the 3% saved from squalor.
## 2  After a devastating earthquake hits Mexico City, trapped survivors from all walks of life wait to be rescued while trying desperately to stay alive.
## 3 When an army recruit is found dead, his fellow soldiers are forced to confront a terrifying secret that's haunting their jungle island training camp.
## 4     In a postapocalyptic world, rag-doll robots hide in fear from dangerous machines out to exterminate them, until a brave newcomer joins the group.
## 5       A brilliant group of students become card-counting experts with the intent of swindling millions out of Las Vegas casinos by playing blackjack.
## 6 A genetics professor experiments with a treatment for his comatose sister that blends medical and shamanic cures, but unlocks a shocking side effect.
# Top países com mais conteúdo em 2019 
netflix_clean <- netflix %>%
  filter(country != "", !is.na(country)) %>%         
  filter(!grepl(",", country))                       


top_countries <- netflix_clean %>%
  group_by(country) %>%
  summarise(total = n()) %>%
  arrange(desc(total)) %>%
  slice_head(n = 10)  

str(top_countries)
## tibble [10 × 2] (S3: tbl_df/tbl/data.frame)
##  $ country: chr [1:10] "United States" "India" "United Kingdom" "Japan" ...
##  $ total  : int [1:10] 2555 923 397 226 183 177 134 115 101 100
plot_ly(top_countries, labels = ~country, values = ~total, type = 'pie') %>%
  layout(title = 'Top 10 países com mais conteúdos na Netflix (2019)',
         legend = list(orientation = 'h', x = 0.1, y = -0.2))

Questão 8

plot_ly(
  type = 'table',
  header = list(
    values = c("País", "Total de conteúdos"),
    align = c("center", "center"),
    fill = list(color = "gray"),
    font = list(color = "white", size = 14)
  ),
  cells = list(
    values = list(top_countries$country, top_countries$total),
    align = "center"
  )
)

Questão 9

netflix_decade <- netflix %>%
  filter(!is.na(release_year), release_year >= 1900) %>%  
  mutate(decade = floor(release_year / 10) * 10)          
conteudo_por_decada <- netflix_decade %>%
  group_by(decade, type) %>%
  summarise(total = n(), .groups = "drop")
series <- conteudo_por_decada %>% filter(type == "TV Show")
filmes <- conteudo_por_decada %>% filter(type == "Movie")
library(plotly)

plot_ly() %>%
  add_trace(data = series,
            x = ~decade,
            y = ~total,
            type = 'scatter',
            mode = 'lines+markers',
            name = 'Séries',
            line = list(color = 'blue')) %>%
  add_trace(data = filmes,
            x = ~decade,
            y = ~total,
            type = 'scatter',
            mode = 'lines+markers',
            name = 'Filmes',
            line = list(color = 'yellow')) %>%
  layout(title = "Quantidade de Conteúdos por Década na Netflix",
         xaxis = list(title = "Década"),
         yaxis = list(title = "Quantidade de Conteúdo"),
         legend = list(x = 0.1, y = 1))

Questão 10

# Carregar pacotes
library(dplyr)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.2.3
library(plotly)

# Carregar os dados
netflix <- read.csv("netflix_titles.csv")

# Preparar os dados: só filmes, anos de 2000 a 2010, e extrair o primeiro gênero
netflix_filmes <- netflix %>%
  filter(type == "Movie",
         !is.na(release_year),
         release_year >= 2000 & release_year <= 2010,
         !is.na(listed_in)) %>%
  mutate(first_genre = sub(",.*", "", listed_in))  # pegar o primeiro gênero

# Filtrar apenas os gêneros desejados
generos_desejados <- c("Dramas", "Action & Adventure", "Comedies")

# Contar por ano e gênero
dados_genero <- netflix_filmes %>%
  filter(first_genre %in% generos_desejados) %>%
  group_by(release_year, first_genre) %>%
  summarise(total = n(), .groups = "drop")

# Expandir todos os anos e gêneros (mesmo se total for 0)
todos_anos <- 2000:2010
dados_completos <- expand.grid(release_year = todos_anos, first_genre = generos_desejados) %>%
  left_join(dados_genero, by = c("release_year", "first_genre")) %>%
  mutate(total = ifelse(is.na(total), 0, total))  # preencher zeros

# Reestruturar para colunas por gênero
dados_genero_wide <- dados_completos %>%
  pivot_wider(names_from = first_genre, values_from = total)


# Criar gráfico de barras lado a lado com Plotly
plot_ly(dados_genero_wide, x = ~release_year) %>%
  add_trace(y = ~`Dramas`, name = 'Dramas', type = 'bar', marker = list(color = 'blue')) %>%
  add_trace(y = ~`Action & Adventure`, name = 'Action & Adventure', type = 'bar', marker = list(color = 'orange')) %>%
  add_trace(y = ~`Comedies`, name = 'Comedies', type = 'bar', marker = list(color = 'green')) %>%
  layout(
    barmode = 'group',
    title = 'Quantidade de Filmes por Gênero (2000–2010)',
    xaxis = list(title = 'Ano'),
    yaxis = list(title = 'Quantidade de Filmes')
  )