library("dplyr")
##
## 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")
library("readr")
library("stringr")
library("forcats")
library("modelr")
library("ggplot2")
setwd("/Users/zannaquinsa")
train = read.csv("/Users/zannaquinsa/Downloads/train17mei.csv", header = T, sep = ";")
head(train)
## 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
train = train %>%
mutate(
Survived = factor(Survived),
Pclass = factor(Pclass),
Embarked = factor(Embarked),
Sex = factor(Sex)
)
train = train %>%
mutate(Title = str_extract(Name, "(?<=, )[A-Za-z ]+(?=\\.)"))
train = train %>%
mutate(Age = ifelse(is.na(Age), median(Age, na.rm = TRUE), Age))
head(train)
## 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 28 0 0
## Ticket Fare Cabin Embarked Title
## 1 A/5 21171 7.2500 S Mr
## 2 PC 17599 71.2833 C85 C Mrs
## 3 STON/O2. 3101282 7.9250 S Miss
## 4 113803 53.1000 C123 S Mrs
## 5 373450 8.0500 S Mr
## 6 330877 8.4583 Q Mr
custom_col = c("0" = "brown", "1" = "pink")
pie_chart = train %>%
count(Survived) %>%
ggplot(aes(x = "", y = n, fill = Survived)) +
geom_bar(width = 1, stat = "identity") +
coord_polar(theta = "y") +
labs(fill = "Survived") +
scale_fill_manual(values = custom_col) +
theme_void()
pie_chart
p_age = ggplot(train) +
geom_freqpoly(mapping = aes(x = Age, color = Survived), binwidth = 1) +
theme(legend.position = "right")
p_sex = ggplot(train, mapping = aes(x = Sex, fill = Survived)) +
geom_bar(stat='count', position='fill') +
labs(x = 'Sex') +
scale_fill_discrete(name="Surv")
p_class = ggplot(
train, mapping = aes(x = Pclass, fill = Survived, colour = Survived)) +
geom_bar(stat='count', position='fill') +
labs(x = 'Pclass') +
theme(legend.position = "none")
p_emb = ggplot(train, aes(Embarked, fill = Survived)) +
geom_bar(stat='count', position='fill') +
labs(x = 'Embarked') +
theme(legend.position = "none")
p_sib = ggplot(train, aes(SibSp, fill = Survived)) +
geom_bar(stat='count', position='fill') +
labs(x = 'SibSp') +
theme(legend.position = "none")
p_par = ggplot(train, aes(Parch, fill = Survived)) +
geom_bar(stat='count', position='fill') +
labs(x = 'Parch') +
theme(legend.position = "none")
p_fare = ggplot(train) +
geom_freqpoly(mapping = aes(Fare, color = Survived), binwidth = 0.05) +
scale_x_log10() +
theme(legend.position = "none")
p_age
Fig. 2
p_sex
Fig. 2
p_fare
Fig. 2
p_class
Fig. 2
p_emb
Fig. 2
p_sib
Fig. 2
p_par
Fig. 2
custom_col <- c("purple", "yellow", "blue")
sibsp_bar_chart <- ggplot(train, aes(x = factor(SibSp), fill = Survived)) +
geom_bar(position = "dodge") +
labs(x = "SibSp", y = "Count", fill = "Survived") +
ggtitle("Distribution of Siblings/Spouses aboard") +
scale_fill_manual(values = custom_col) + # Mengatur warna fill
theme_minimal()
sibsp_bar_chart
p_age_boxplot = ggplot(train, aes(x = Survived, y = Age, fill = Survived)) +
geom_boxplot() +
labs(x = 'Survived', y = 'Age') +
scale_fill_manual(values = custom_col) +
theme(legend.position = 'none')
p_age_boxplot