MATERI

# wrangle
library('dplyr') #data manipulation
## 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') #data manipulation
## Warning: package 'tidyr' was built under R version 4.3.3
library('readr') #data input
## Warning: package 'readr' was built under R version 4.3.3
library('stringr') #string manipulation
## Warning: package 'stringr' was built under R version 4.3.3
library('forcats') #factor manipulation
## Warning: package 'forcats' was built under R version 4.3.3
library('modelr') #factor manipulation
## Warning: package 'modelr' was built under R version 4.3.3
library('ggplot2')
## Warning: package 'ggplot2' was built under R version 4.3.3
setwd( "C:/Users/ASUS/Documents")
data = read.csv("C:/Users/ASUS/Downloads/train.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
data$Survived <- factor(data$Survived)
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    Min.   :1.000   Length:891         Length:891        
##  1st Qu.:223.5   1:342    1st Qu.:2.000   Class :character   Class :character  
##  Median :446.0            Median :3.000   Mode  :character   Mode  :character  
##  Mean   :446.0            Mean   :2.309                                        
##  3rd Qu.:668.5            3rd Qu.:3.000                                        
##  Max.   :891.0            Max.   :3.000                                        
##                                                                                
##       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         Length:891        
##  1st Qu.:  7.91   Class :character   Class :character  
##  Median : 14.45   Mode  :character   Mode  :character  
##  Mean   : 32.20                                        
##  3rd Qu.: 31.00                                        
##  Max.   :512.33                                        
## 
# Visualisasi data dengan ggplot2

# Age vs. Survival dengan warna khusus
p_age = ggplot(data) +
  geom_freqpoly(mapping = aes(x = Age, color = Survived), binwidth = 1) +
  scale_color_manual(values = c("red", "blue")) + # Warna khusus untuk Survived
  theme(legend.position = "right")

# Sex vs. Survival dengan warna khusus
p_sex = ggplot(data, mapping = aes(x = Sex, fill = Survived)) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Sex') +
  scale_fill_manual(name = "Surv", values = c("#DFD0B8","#3C5B6F")) # Warna khusus untuk Survived

# Passenger Class vs. Survival dengan warna khusus
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 = c("#E1AFD1", "#8E3E63")) + # Warna isi khusus
  scale_color_manual(values = c("#E1AFD1", "#8E3E63")) + # Warna garis khusus
  theme(legend.position = "none")

# Embarked vs. Survival dengan warna khusus
p_emb = ggplot(data, aes(Embarked, fill = Survived)) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Embarked') +
  scale_fill_manual(values = c("#E5DDC5", "#B3C8CF")) + # Warna khusus untuk Survived
  theme(legend.position = "none")

# Siblings/Spouses aboard vs. Survival dengan warna khusus
p_sib = ggplot(data, aes(SibSp, fill = Survived)) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'SibSp') +
  scale_fill_manual(values = c("brown", "pink")) + # Warna khusus untuk Survived
  theme(legend.position = "none")

# Parents/Children aboard vs. Survival dengan warna khusus
p_par = ggplot(data, aes(Parch, fill = Survived)) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Parch') +
  scale_fill_manual(values = c("darkgreen", "lightblue")) + # Warna khusus untuk Survived
  theme(legend.position = "none")

# Fare vs. Survival dengan warna khusus
p_fare = ggplot(data) +
  geom_freqpoly(mapping = aes(Fare, color = Survived), binwidth = 0.05) +
  scale_x_log10() +
  scale_color_manual(values = c("darkred", "darkblue")) + # Warna khusus untuk Survived
  theme(legend.position = "none")

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

print(p_sex)

print(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()`).

print(p_class)

print(p_emb)

print(p_sib)

print(p_par)

# Visualisasi boxplot untuk variabel Age dengan warna khusus
boxplot_age <- ggplot(data, aes(x = Survived, y = Age, fill = Survived)) +
  geom_boxplot(color = "black") + # warna garis
  labs(x = "Survived", y = "Age", title = "Boxplot of Age by Survival Status") +
  scale_fill_manual(values = c("pink", "skyblue")) + # warna khusus untuk Survived
  theme_minimal()

# Visualisasi boxplot untuk variabel Fare dengan warna khusus
boxplot_fare <- ggplot(data, aes(x = Survived, y = Fare, fill = Survived)) +
  geom_boxplot(color = "black") + # warna garis
  labs(x = "Survived", y = "Fare", title = "Boxplot of Fare by Survival Status") +
  scale_fill_manual(values = c("orange", "brown")) + # warna khusus untuk Survived
  theme_minimal()

# Visualisasi boxplot untuk variabel Pclass dengan warna khusus
boxplot_pclass <- ggplot(data, aes(x = factor(Pclass), y = Fare, fill = factor(Pclass))) +
  geom_boxplot(color = "black") + # warna garis
  labs(x = "Pclass", y = "Fare", title = "Boxplot of Fare by Passenger Class") +
  scale_fill_manual(values = c("#FDFFC2", "#EEEEEE", "#A3D8FF")) + # warna khusus untuk Pclass
  theme_minimal()

# Tampilkan boxplot
boxplot_age
## Warning: Removed 177 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

boxplot_fare

boxplot_pclass

# Histogram untuk variabel Age
hist_age <- ggplot(data, aes(x = Age)) +
  geom_histogram(fill = "#74512D", color = "white", bins = 20) +
  labs(x = "Age", y = "Frequency", title = "Histogram of Age") +
  theme_minimal()

# Histogram untuk variabel Fare
hist_fare <- ggplot(data, aes(x = Fare)) +
  geom_histogram(fill = "#006769", color = "#344C64", bins = 20) +
  labs(x = "Fare", y = "Frequency", title = "Histogram of Fare") +
  theme_minimal()

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

hist_fare

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.