MATERI PRAKTIKUM

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
netflix = read.csv("netflix_titles.csv")
custom_col = c("navy", "blue")
ggplot2::ggplot(netflix, aes(y = "", fill = factor(type))) +
  geom_bar(width = 1) +
  theme(axis.line = element_blank(), plot.title = element_text(hjust = 0.5, size = 22)) +
  coord_polar(start = 0) +
  labs(fill = "class", x = NULL, y = NULL, title = "Pie Chart of Netflix Shows") +
  scale_fill_manual(values = custom_col)

par(c(2, 1))
## Warning in par(c(2, 1)): argument 1 does not name a graphical parameter
## NULL
ggplot(netflix, aes(x = "", fill = factor(type))) + geom_bar()

install.packages("tidyr")
## Installing package into 'C:/Users/ASUS/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'tidyr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'tidyr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\ASUS\AppData\Local\R\win-library\4.3\00LOCK\tidyr\libs\x64\tidyr.dll
## to C:\Users\ASUS\AppData\Local\R\win-library\4.3\tidyr\libs\x64\tidyr.dll:
## Permission denied
## Warning: restored 'tidyr'
## 
## The downloaded binary packages are in
##  C:\Users\ASUS\AppData\Local\Temp\RtmpwLsTgl\downloaded_packages
install.packages("forcats")
## Installing package into 'C:/Users/ASUS/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'forcats' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\ASUS\AppData\Local\Temp\RtmpwLsTgl\downloaded_packages
install.packages("modelr")
## Installing package into 'C:/Users/ASUS/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'modelr' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\ASUS\AppData\Local\Temp\RtmpwLsTgl\downloaded_packages
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
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.3
library(stringr)
## Warning: package 'stringr' was built under R version 4.3.3
library(forcats)
## Warning: package 'forcats' was built under R version 4.3.3
library(modelr)
## Warning: package 'modelr' was built under R version 4.3.3
data = read.csv("datavde.csv")
head(data)
##   PassengerId Survived Pclass
## 1           1        0      3
## 2           2        1      1
## 3           3        1      3
## 4           4        1      1
## 5           5        0      3
## 6           6        0      3
##                                                  Name    Sex Age SibSp Parch
## 1                             Braund, Mr. Owen Harris   male  22     1     0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1     0
## 3                              Heikkinen, Miss. Laina female  26     0     0
## 4        Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1     0
## 5                            Allen, Mr. William Henry   male  35     0     0
## 6                                    Moran, Mr. James   male  NA     0     0
##             Ticket    Fare Cabin Embarked
## 1        A/5 21171  7.2500              S
## 2         PC 17599 71.2833   C85        C
## 3 STON/O2. 3101282  7.9250              S
## 4           113803 53.1000  C123        S
## 5           373450  8.0500              S
## 6           330877  8.4583              Q

STUDY CASE

data = data %>% mutate(
  Survived = factor(Survived),
  Pclass = factor(Pclass),
  Embarked = factor(Embarked),
  Sex = factor(Sex)
)
head(data)
##   PassengerId Survived Pclass
## 1           1        0      3
## 2           2        1      1
## 3           3        1      3
## 4           4        1      1
## 5           5        0      3
## 6           6        0      3
##                                                  Name    Sex Age SibSp Parch
## 1                             Braund, Mr. Owen Harris   male  22     1     0
## 2 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female  38     1     0
## 3                              Heikkinen, Miss. Laina female  26     0     0
## 4        Futrelle, Mrs. Jacques Heath (Lily May Peel) female  35     1     0
## 5                            Allen, Mr. William Henry   male  35     0     0
## 6                                    Moran, Mr. James   male  NA     0     0
##             Ticket    Fare Cabin Embarked
## 1        A/5 21171  7.2500              S
## 2         PC 17599 71.2833   C85        C
## 3 STON/O2. 3101282  7.9250              S
## 4           113803 53.1000  C123        S
## 5           373450  8.0500              S
## 6           330877  8.4583              Q
summary(data)
##   PassengerId    Survived Pclass      Name               Sex     
##  Min.   :  1.0   0:549    1:216   Length:891         female:314  
##  1st Qu.:223.5   1:342    2:184   Class :character   male  :577  
##  Median :446.0            3:491   Mode  :character               
##  Mean   :446.0                                                   
##  3rd Qu.:668.5                                                   
##  Max.   :891.0                                                   
##                                                                  
##       Age            SibSp           Parch           Ticket         
##  Min.   : 0.42   Min.   :0.000   Min.   :0.0000   Length:891        
##  1st Qu.:20.12   1st Qu.:0.000   1st Qu.:0.0000   Class :character  
##  Median :28.00   Median :0.000   Median :0.0000   Mode  :character  
##  Mean   :29.70   Mean   :0.523   Mean   :0.3816                     
##  3rd Qu.:38.00   3rd Qu.:1.000   3rd Qu.:0.0000                     
##  Max.   :80.00   Max.   :8.000   Max.   :6.0000                     
##  NA's   :177                                                        
##       Fare           Cabin           Embarked
##  Min.   :  0.00   Length:891          :  2   
##  1st Qu.:  7.91   Class :character   C:168   
##  Median : 14.45   Mode  :character   Q: 77   
##  Mean   : 32.20                      S:644   
##  3rd Qu.: 31.00                              
##  Max.   :512.33                              
## 
custom_col = c("#ADBC9F", "#436850", "#12372A", "#ACE1AF", "#E0FBE2", "#799351")
ggplot2::ggplot(data, aes(y = "", fill = factor(Pclass))) +
  geom_bar(width = 1) +
  theme(axis.line = element_blank(), plot.title = element_text(hjust = 0.5, size = 22)) +
  coord_polar(start = 0) +
  labs(fill = "class", x = NULL, y = NULL, title = "Pclass") +
  scale_fill_manual(values = custom_col)

ggplot2::ggplot(data) +
  geom_freqpoly(mapping = aes(x = Age, color = Survived), binwidth = 1) +
  theme(legend.position = "right")
## Warning: Removed 177 rows containing non-finite outside the scale range
## (`stat_bin()`).

p_sex = ggplot(data) +
  geom_bar(mapping = aes(x = Sex, fill = Survived), position = "dodge") +
  scale_fill_manual(values = custom_col)

p_sex

p_class = ggplot(data, mapping = aes(x = Pclass, fill = Survived)) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'Pclass') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "none")

p_class

ggplot2::ggplot(data) +
  geom_freqpoly(mapping = aes(x = Fare, color = Survived), binwidth = 1) +
  theme(legend.position = "right")

p_age = ggplot(data) +
  geom_freqpoly(mapping = aes(x = Age, color = as.factor(Survived)), binwidth = 1) +
  scale_color_manual(values = custom_col) +
  theme(legend.position = "right")

p_sex = ggplot(data, mapping = aes(x = Sex, fill = as.factor(Survived))) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'Sex') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "right")

p_class = ggplot(data, mapping = aes(x = Pclass, fill = Survived, colour = Survived)) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'Pclass') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "none")

p_emb = ggplot(data, aes(Embarked, fill = Survived)) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'Embarked') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "none")

p_sib = ggplot(data, aes(SibSp, fill = Survived)) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'SibSp') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "none")

p_par = ggplot(data, aes(Parch, fill = Survived)) +
  geom_bar(stat = 'count', position = 'fill') +
  labs(x = 'Parch') +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "none")

p_fare = ggplot(data) +
  geom_freqpoly(mapping = aes(Fare, color = Survived), binwidth = 0.05) +
  scale_x_log10() +
  scale_color_manual(values = custom_col) +
  theme(legend.position = "none")

p_age
## Warning: Removed 177 rows containing non-finite outside the scale range
## (`stat_bin()`).

p_sex

p_fare
## Warning in scale_x_log10(): log-10 transformation introduced infinite values.
## Warning: Removed 15 rows containing non-finite outside the scale range
## (`stat_bin()`).

p_class

p_emb

p_sib

p_par

library(ggplot2)
data$AgeCategory <- cut(data$Age, 
                        breaks = c(-Inf, 18, 35, 50, Inf), 
                        labels = c("Child", "Young Adult", "Middle Age", "Senior"))

ggplot(data, mapping = aes(x = Pclass, fill = AgeCategory)) +
  geom_bar(stat = 'count') +
  scale_fill_manual(values = custom_col, name = "Age Category") +
  labs(x = "Pclass", y = "Count") +
  theme(legend.position = "right")

ggplot(data, aes(x = Age, y = Fare, color = Pclass)) +
  geom_point(alpha = 0.7) +
  labs(x = "Age", y = "Fare", title = "Scatter Plot of Fare vs Age by Pclass") +
  scale_color_manual(values = custom_col, name = "Pclass") +
  theme(legend.position = "right")
## Warning: Removed 177 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(data, aes(x = Pclass, y = Age, fill = Survived)) +
  geom_boxplot() +
  labs(x = "Pclass", y = "Age", title = "Boxplot of Age by Pclass and Survival Status") +
  scale_fill_manual(values = custom_col, name = "Survived") +
  theme(legend.position = "right")
## Warning: Removed 177 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

ggplot(data, aes(x = Age, color = Pclass)) +
  geom_freqpoly(binwidth = 1) +
  labs(x = "Age", y = "Count", title = "Frequency Polygon of Age by Pclass") +
  scale_color_manual(values = custom_col, name = "Pclass") +
  theme(legend.position = "right")
## Warning: Removed 177 rows containing non-finite outside the scale range
## (`stat_bin()`).