Installing the right packages
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
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
Loading thr titanic dataset into r
titanic_data <- read.csv("Titanic-Dataset.csv")
View the first rows of the dataset
head(titanic_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
Check the structure of the data
str(titanic_data)
## 'data.frame': 891 obs. of 12 variables:
## $ PassengerId: int 1 2 3 4 5 6 7 8 9 10 ...
## $ Survived : int 0 1 1 1 0 0 0 0 1 1 ...
## $ Pclass : int 3 1 3 1 3 3 1 3 3 2 ...
## $ Name : chr "Braund, Mr. Owen Harris" "Cumings, Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen, Miss. Laina" "Futrelle, Mrs. Jacques Heath (Lily May Peel)" ...
## $ Sex : chr "male" "female" "female" "female" ...
## $ Age : num 22 38 26 35 35 NA 54 2 27 14 ...
## $ SibSp : int 1 1 0 1 0 0 0 3 0 1 ...
## $ Parch : int 0 0 0 0 0 0 0 1 2 0 ...
## $ Ticket : chr "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
## $ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
## $ Cabin : chr "" "C85" "" "C123" ...
## $ Embarked : chr "S" "C" "S" "S" ...
Removing rows with missing Embarked Values
titanic_data <- titanic_data %>%
filter(Embarked != "")
Dropping the Cabin Column
titanic_data <- titanic_data %>% select(-Cabin)
Converting relevant columns to appropiate datatypes
titanic_data <- titanic_data %>% mutate(Survived = as.factor(Survived), Pclass = as.factor(Pclass), Sex = as.factor(Sex), Embarked = as.factor(Embarked))
Looking at the Column Names
names(titanic_data)
## [1] "PassengerId" "Survived" "Pclass" "Name" "Sex"
## [6] "Age" "SibSp" "Parch" "Ticket" "Fare"
## [11] "Embarked"
Changing the Column Names to Lowercase
names(titanic_data) <- tolower(names(titanic_data))
Save my cleaned data to a new file
write.csv(titanic_data, "cleaned_titanic_data.csv", row.names = FALSE)
Data Visualisation
Installing the right packages
install.packages("tidyverse") ## Includes ggplot2
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.5
## ✔ ggplot2 3.5.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
install.packages("plotly") ## Used to make graphs interactive
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(plotly)
##
## 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
install.packages("devtools")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(devtools)
## Loading required package: usethis
Bar plot for Survived
ggplotly(
ggplot(titanic_data, aes(x = as.factor(survived), fill = as.factor(survived))) +
geom_bar(color = "black") +
scale_fill_manual(values = c("yellow", "orange")) +
xlab("Survived") +
ylab("Count") +
ggtitle("Count of Survived Passengers on the Titanic") +
theme(axis.line = element_line(color = "black"))
)
Shows a bar plot for the survived variable, shows the count of
passengers who survived and those who didnt.
Barplot for fair price
ggplotly(
ggplot(titanic_data, aes(x = sex, fill = sex)) +
geom_bar(color = "black") + # Add black borders to the bars
scale_fill_manual(values = c("blue", "pink")) +
xlab("Sex") +
ylab("Count") +
ggtitle("Male to Female Ratio on The Titanic") +
theme(axis.line = element_line(color = "black"))
)
Scatter plot Age vs Fair price
ggplotly(
ggplot(titanic_data, aes(x = age, y = fare)) +
geom_point(color = "red") +
geom_smooth(method = "lm", se = FALSE) + # Add line of best fit
xlab("Age") +
ylab("Fare") +
ggtitle("Age vs Fare Price") +
theme(axis.line = element_line(color = "black"))
)
## `geom_smooth()` using formula = 'y ~ x'
Histogram of Age
ggplotly(
ggplot(titanic_data, aes(x = age, fill = sex)) +
geom_histogram(binwidth = 5, color = "black") +
xlab("Age") +
ylab("Count") +
ggtitle("Age Distribution of Passengers by Sex") +
theme(axis.line = element_line(color = "black"))
)
Facet grid of Age vs Fare by Passenger Class and Sex
ggplotly(
ggplot(titanic_data, aes(x = age, y = fare, color = sex)) +
geom_point() +
facet_wrap(~ pclass) +
xlab("Age") +
ylab("Fare") +
ggtitle("Age vs. Fare by Passenger Class and Sex") +
theme(axis.line = element_line(color = "black"))
)
Boxplot of Age by Survived
ggplotly(ggplot(titanic_data, aes(x = as.factor(survived), y = age)) +
geom_boxplot(fill = "green") +
geom_point(data = . %>% filter(age > quantile(age, 0.75) + 1.5*IQR(age) | age < quantile(age, 0.25) - 1.5*IQR(age)), color = "red") +
xlab("Survived") +
ylab("Age") +
ggtitle("Age Distribution by Survival Status")
)
Bar Plot of Passenger Class
ggplotly(ggplot(titanic_data, aes(x = as.factor(pclass), fill = as.factor(pclass))) +
geom_bar(color = "black") +
scale_fill_manual(values = c("blue", "pink", "yellow")) +
xlab("Pclass") +
ylab("Count") +
ggtitle("Count of Passenger Class") +
theme(axis.line = element_line(color = "black"))
)
Bar Plot of Embarked
ggplotly(
ggplot(titanic_data, aes(x = embarked, fill = embarked)) +
geom_bar(color = "black") +
scale_fill_manual(values = c("blue", "pink", "yellow", "green")) +
xlab("Embarked Point") +
ylab("Count") +
ggtitle("Count of Passengers by Embarkation Point") +
theme(axis.line = element_line(color = "black"))
)