Load your data for titanic train data Book for ggplot2 - ggplot2 by Hadley Wickham
#Set the platform
setwd("D:/Data/ScienceDojo/Kaggle Submission")
The working directory was changed to D:/Data/ScienceDojo/Kaggle Submission inside a notebook chunk. The working directory will be reset when the chunk is finished running. Use the knitr root.dir option in the setup chunk to change the working directory for notebook chunks.
getwd()
[1] "D:/Data/ScienceDojo/Kaggle Submission"
library(ggplot2)
titanic <- read.csv("full_train_titanic_dataset.csv")
#levels(titanic$Survived) <- c("Dead", "Survived")
#levels(titanic$Embarked) <- c("Unknown", "Cherbourg", "Queenstown", "Southampton")
titanic$Pclass <- as.factor(titanic$Pclass)
titanic$SibSp <- as.factor(titanic$SibSp)
titanic$Sex <- as.factor(titanic$Sex)
titanic$Survived <- as.factor(titanic$Survived)
titanic$Embarked <- as.factor(titanic$Embarked)
Plot survival rate
ggplot(data = titanic, aes(x = Survived)) + theme_bw() + geom_bar() + labs(y = "Passenger Count",title = "Titanic Survival Rate")

prop.table(table(titanic$Survived))
0 1
0.6161616 0.3838384
What was the survival rate by Gender?
ggplot(data = titanic, aes(x = Sex, fill = Survived)) + theme_bw() + geom_bar() + labs(y = "Passenger Count",title = "Titanic Survival Rate by Sex")

What was the survival rate by PClass?
ggplot(data = titanic, aes(x = Pclass, fill = Survived)) + theme_bw() + geom_bar() + labs(y = "Passenger Count",title = "Titanic Survival Rate by PClass")

What was the survival rate by class of ticket and gender?
ggplot(data = titanic, aes(x = Sex, fill = Survived)) + theme_bw() + facet_wrap( ~ Pclass) +geom_bar() + labs(y = "Passenger Count",title = "Titanic Survival Rate by Pclass and Gender")

What is the distribution of Age?
ggplot(data = titanic, aes(x = Age)) + theme_bw() + geom_histogram(bandwidth=5) + labs(y = "Passenger Count", x = "Age (Distribution by 5)",title = "Titanic Age Distribution")
Ignoring unknown parameters: bandwidth

What is the survival rate by Age?
ggplot(data = titanic, aes(x = Age,fill = Survived)) + theme_bw() + geom_histogram(bandwidth=5) + labs(y = "Passenger Count", x = "Age (Distribution by 5)",title = "Titanic Age Distribution with Survival")
Ignoring unknown parameters: bandwidth

Wiskeplot visualization for Survival by Age
ggplot(data = titanic, aes(x = Survived, y = Age)) + theme_bw() + geom_boxplot() + labs(y = "Age", x = "Survived",title = "Titanic Survivied by Age")

Survival Rate by Age, Pclass and Sex
ggplot(data = titanic, aes(x = Age, fill = Survived)) + theme_bw() + facet_wrap(Sex ~ Pclass) + geom_density(alpha = 0.5)+ labs(y = "Age", x = "Survived",title = "Titanic Survival by Age, Sex and Pclass")

ggplot(data = titanic, aes(x = Age, fill = Survived)) + theme_bw() + facet_wrap(Sex ~ Pclass) + geom_histogram(bandwidth = 5) + labs(y = "Age", x = "Survived",title = "Titanic Survival by Age, Sex and Pclass")
Ignoring unknown parameters: bandwidth

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
LS0tDQp0aXRsZTogIk5vdGVib29rIGZvciBLYWdnbGUgVGl0YW5pYyBIVyINCm91dHB1dDogaHRtbF9ub3RlYm9vaw0KLS0tDQpMb2FkIHlvdXIgZGF0YSBmb3IgdGl0YW5pYyB0cmFpbiBkYXRhDQpCb29rIGZvciBnZ3Bsb3QyIC0gZ2dwbG90MiBieSBIYWRsZXkgV2lja2hhbQ0KYGBge3J9DQojU2V0IHRoZSBwbGF0Zm9ybQ0KDQpzZXR3ZCgiRDovRGF0YS9TY2llbmNlRG9qby9LYWdnbGUgU3VibWlzc2lvbiIpDQpnZXR3ZCgpDQpsaWJyYXJ5KGdncGxvdDIpDQp0aXRhbmljIDwtIHJlYWQuY3N2KCJmdWxsX3RyYWluX3RpdGFuaWNfZGF0YXNldC5jc3YiKQ0KI2xldmVscyh0aXRhbmljJFN1cnZpdmVkKSA8LSBjKCJEZWFkIiwgIlN1cnZpdmVkIikNCiNsZXZlbHModGl0YW5pYyRFbWJhcmtlZCkgPC0gYygiVW5rbm93biIsICJDaGVyYm91cmciLCAiUXVlZW5zdG93biIsICJTb3V0aGFtcHRvbiIpDQp0aXRhbmljJFBjb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