MATERI

setwd("D:/BISMILLAH KULYEH/SEM 2/VDE")
netflix = read.csv("netflix_titles.csv")
head(netflix)
##    show_id    type                                   title
## 1 81145628   Movie Norm of the North: King Sized Adventure
## 2 80117401   Movie              Jandino: Whatever it Takes
## 3 70234439 TV Show                      Transformers Prime
## 4 80058654 TV Show        Transformers: Robots in Disguise
## 5 80125979   Movie                            #realityhigh
## 6 80163890 TV Show                                 Apaches
##                   director
## 1 Richard Finn, Tim Maltby
## 2                         
## 3                         
## 4                         
## 5         Fernando Lebrija
## 6                         
##                                                                                                                                                                                 cast
## 1                                        Alan Marriott, Andrew Toth, Brian Dobson, Cole Howard, Jennifer Cameron, Jonathan Holmes, Lee Tockar, Lisa Durupt, Maya Kay, Michael Dobson
## 2                                                                                                                                                                   Jandino Asporaat
## 3 Peter Cullen, Sumalee Montano, Frank Welker, Jeffrey Combs, Kevin Michael Richardson, Tania Gunadi, Josh Keaton, Steve Blum, Andy Pessoa, Ernie Hudson, Daran Norris, Will Friedle
## 4                                                           Will Friedle, Darren Criss, Constance Zimmer, Khary Payton, Mitchell Whitfield, Stuart Allan, Ted McGinley, Peter Cullen
## 5           Nesta Cooper, Kate Walsh, John Michael Higgins, Keith Powers, Alicia Sanz, Jake Borelli, Kid Ink, Yousef Erakat, Rebekah Graf, Anne Winters, Peter Gilroy, Patrick Davis
## 6                                                                                                      Alberto Ammann, Eloy Azorín, Verónica Echegui, Lucía Jiménez, Claudia Traisac
##                                    country        date_added release_year
## 1 United States, India, South Korea, China September 9, 2019         2019
## 2                           United Kingdom September 9, 2016         2016
## 3                            United States September 8, 2018         2013
## 4                            United States September 8, 2018         2016
## 5                            United States September 8, 2017         2017
## 6                                    Spain September 8, 2017         2016
##     rating duration
## 1    TV-PG   90 min
## 2    TV-MA   94 min
## 3 TV-Y7-FV 1 Season
## 4    TV-Y7 1 Season
## 5    TV-14   99 min
## 6    TV-MA 1 Season
##                                                           listed_in
## 1                                Children & Family Movies, Comedies
## 2                                                   Stand-Up Comedy
## 3                                                          Kids' TV
## 4                                                          Kids' TV
## 5                                                          Comedies
## 6 Crime TV Shows, International TV Shows, Spanish-Language TV Shows
##                                                                                                                                            description
## 1         Before planning an awesome wedding for his grandfather, a polar bear king must take back a stolen artifact from an evil archaeologist first.
## 2    Jandino Asporaat riffs on the challenges of raising kids and serenades the audience with a rousing rendition of "Sex on Fire" in his comedy show.
## 3         With the help of three human allies, the Autobots once again protect Earth from the onslaught of the Decepticons and their leader, Megatron.
## 4                       When a prison ship crash unleashes hundreds of Decepticons on Earth, Bumblebee leads a new Autobot force to protect humankind.
## 5 When nerdy high schooler Dani finally attracts the interest of her longtime crush, she lands in the cross hairs of his ex, a social media celebrity.
## 6             A young journalist is forced into a life of crime to save his father and family in this series based on the novel by Miguel Sáez Carral.
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))

pie

pie2 = 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")

pie2

pie3 = 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)

pie3

pie4 = 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)

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(x ="", fill = factor(type)))+
  geom_bar()

custom_col = c("red", "orange", "blue")
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)

STUDY CASE

#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('stringr')
## Warning: package 'stringr' was built under R version 4.3.3
setwd("D:/BISMILLAH KULYEH/SEM 2/VDE")
train = read.csv("train.csv")
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
#Mengganti tipe data menjadi factor
train$Survived = factor(train$Survived)
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
#Bisa juga pakai ini
train = train %>% mutate(
  Survived = factor(Survived),
  Pclass = factor(Pclass),
  Embarked = factor(Embarked),
  Sex = factor(Sex)
)

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
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                              
## 
library(ggplot2)

p1 = ggplot(train, aes(x="", fill = factor(Pclass))) +
  geom_bar(width = 1.5) +
  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 PClass Train") +   
  coord_polar(theta = "y", start = 0)

p1

library(ggplot2)
library(dplyr)

train_summary <- train %>%
  count(Pclass)

p2 <- ggplot(train_summary, aes(x = "", y = n, fill = factor(Pclass))) +
  geom_bar(stat = "identity", width = 1) +
  coord_polar(theta = "y", start = 0) +
  theme_minimal() +
  theme(axis.line = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank(),
        panel.grid = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 22),
        legend.title = element_text(size = 14),
        legend.text = element_text(size = 12)) +
  labs(fill = "Class",
       x = NULL,
       y = NULL,
       title = "Pie Chart of PClass Train") +
  geom_text(aes(label = paste0(n, " (", round(n / sum(n) * 100, 1), "%)")), 
            position = position_stack(vjust = 0.5), 
            color = "black", size = 4) +
  scale_fill_brewer(palette = "Set3")

p2