knitr::opts_chunk$set(echo=TRUE)
options(repos=c(CRAN="https://cloud.r-project.org"))

##Materi 1

setwd('C:/Users/ACER/Downloads')
netflix = read.csv('netflix_titles.csv')
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
pie <- ggplot(netflix, aes(x = "", fill = factor(type))) +
geom_bar(width = 1) + theme(axis.line = element_blank(),
plot.title = element_text(hjust = 0.5, size = 22)) + labs(fill = "class",
                                                         x=NULL,
                                                         y=NULL,
                                                         title = " Pie Chart of Netflix shows ") +
  coord_polar(theta = "y",start=0)
pie

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()

ggplot(netflix, aes(y = "", fill = factor(type))) +
  geom_bar()

setwd('C:/Users/ACER/Downloads')
netflix = read.csv('netflix_titles.csv')
library(ggplot2)
custom_col <- c("red", "green", "orange")
ggplot(netflix, aes(x = "", fill = factor(type)))+ geom_bar(width=1) + theme(axis.line = element_blank(),
plot.title = element_text(hjust = 0.5, size = 22)) + labs(fill = "class",
                                                         x=NULL,
                                                         y=NULL,
                                                         title = " Pie Chart of Netflix shows ") +
  coord_polar(theta = "y",start=0) + scale_fill_manual(values=custom_col)

#wrangle
install.packages("readr")
## Installing package into 'C:/Users/ACER/AppData/Local/R/win-library/4.3'
## (as 'lib' is unspecified)
## package 'readr' successfully unpacked and MD5 sums checked
## Warning: cannot remove prior installation of package 'readr'
## Warning in file.copy(savedcopy, lib, recursive = TRUE): problem copying
## C:\Users\ACER\AppData\Local\R\win-library\4.3\00LOCK\readr\libs\x64\readr.dll
## to C:\Users\ACER\AppData\Local\R\win-library\4.3\readr\libs\x64\readr.dll:
## Permission denied
## Warning: restored 'readr'
## 
## The downloaded binary packages are in
##  C:\Users\ACER\AppData\Local\Temp\RtmpSkN19w\downloaded_packages
install.packages("forcats")
## Installing package into 'C:/Users/ACER/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\ACER\AppData\Local\Temp\RtmpSkN19w\downloaded_packages
install.packages("modelr")
## Installing package into 'C:/Users/ACER/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\ACER\AppData\Local\Temp\RtmpSkN19w\downloaded_packages
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
train <- train %>% mutate(
  Survived = factor(Survived),
  Pclass = factor(Pclass),
  Embarked = factor(Embarked),
  Sex = factor(Sex)
)
head(train)
summary(train)
##   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                              
## 
summary(train)
##   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                              
## 
setwd('C:/Users/ACER/Downloads')
train <- read.csv('train.csv')

#memberi warna
custom_col <- c("#d90429","#ef233c","#edf2f4")

pclass_counts <- as.data.frame(table(train$Pclass))
colnames(pclass_counts) <- c("Pclass", "Frequency")

#membuat pie chart
pie <- ggplot(pclass_counts, aes(x = "", 
                                 y = Frequency, 
                                 fill = factor(Pclass))) +
  geom_bar(width = 1, stat = "identity") + 
  coord_polar(theta = "y", start = 0) +
  scale_fill_manual(values = custom_col) +
  labs(fill = "Class", 
       x = NULL, 
       y = NULL, 
       title = "Pie Chart of Pclass") +
  theme_void() +
  theme(axis.line = element_blank(),
        plot.title = element_text(hjust = 0.5, 
                                  size = 22))
pie

##others

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

Fig.2

p_sex = ggplot(train, mapping = aes(x = Sex, fill = as.factor(Survived))) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Sex') +
  scale_fill_discrete(name="Survived") + scale_fill_manual(values = custom_col)
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
p_sex
Fig.2

Fig.2

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

Fig.2

p_emb = ggplot(train, aes(Embarked, fill = as.factor(Survived))) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Embarked') +
  theme(legend.position = "none") + scale_fill_manual(values = custom_col)
p_emb
Fig.2

Fig.2

p_sib = ggplot(train, aes(SibSp, fill = as.factor(Survived))) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'SibSp') +
  theme(legend.position = "none") + scale_fill_manual(values = custom_col)
p_sib
Fig.2

Fig.2

p_par = ggplot(train, aes(Parch, fill = as.factor(Survived))) +
  geom_bar(stat='count', position='fill') +
  labs(x = 'Parch') +
  theme(legend.position = "none")+ scale_fill_manual(values = custom_col)
p_par
Fig.2

Fig.2

p_fare = ggplot(train) +
  geom_freqpoly(mapping = aes(Fare, color = as.factor(Survived)), binwidth = 0.05) +
  scale_x_log10() +
  theme(legend.position = "none")+ scale_fill_manual(values = custom_col)
p_fare
Fig.2

Fig.2