library(tidyverse)
library(ggplot2)

setwd("C:/Users/nunes/OneDrive/Bureau/WorkDay/INSA/Big_Data/Projet")

##########################
#Acquisition of the data #
##########################

#Variable containing all the information
tab <- read.table("yob1880.txt",h=FALSE,sep=",")
Year <- matrix(1880,ncol=1,nrow = nrow(tab))
tab <- cbind(Year,tab)

for (i in seq(1881,2019,1)){
  #Dynamic name for the source txt file
  namesource <- "yob"
  namesource <- paste(c(namesource,i),collapse = "")
  namesource <- paste (c(namesource,".txt"), collapse="")
  
  # Table used inside the for loop to extrat the informations
  tabinter <- read.table(namesource,h=FALSE,sep=",")
  
  #Converting the table into the format "Year,Name,Sex,Number"
  Year <- matrix(i,ncol=1,nrow = nrow(tabinter))
  tabinter <- cbind(Year,tabinter)
  
  #Adding the new informations to the final table
  tab <- rbind(tab,tabinter)
}

final_table <- tab%>%
  rename(
    Name = V1,
    Sex = V2,
    Number = V3
  )



####################################################################
# Partie 1 - L'influence de Marylin depuis 1880                    #
####################################################################

MarilynFemme <- final_table %>%
  filter(Name == "Marilyn")%>%
  group_by(Year,Number)%>%
  filter(Sex=='F')

MarilynHomme <- final_table %>%
  filter(Name == "Marilyn")%>%
  group_by(Year,Number)%>%
  filter(Sex=='M')

ggplot(MarilynFemme,aes(x=Year,y=Number, fill=Number))+
  geom_bar(stat = "identity")+
  ggtitle("Evolution des naissances avec le prénom Marilyn depuis 1880, femmes")

ggplot(MarilynHomme,aes(x=Year,y=Number, fill=Number))+
  geom_bar(stat = "identity")+
  ggtitle("Evolution des naissances avec le prénom Marilyn depuis 1880, hommes")





AdolphHomme <- final_table %>%
  filter(Name == "Adolph")%>%
  group_by(Year,Number)%>%
  filter(Sex=='M')


ggplot(AdolphHomme,aes(x=Year,y=Number, fill=Number))+
  geom_bar(stat = "identity")+
  ggtitle("Evolution des naissances avec le prénom Adolph depuis 1880, hommes")



####################################################################
# Partie 2 - Nombres de naissances depuis 1880                     #
####################################################################


##################################
# 1.1 - Number of births by year #
##################################

NaissancesAnDepuis1880 <- final_table %>%
  group_by(Year)%>%
  summarise(
    NbrBirths = sum(Number)
  )

ggplot(NaissancesAnDepuis1880,aes(x=Year,y=NbrBirths, color= NbrBirths))+
  geom_line(size = 3)+
  ggtitle("Nombre de naissances par année depuis 1880, sexes confondus")


NaissancesAn6090 <- final_table %>%
  group_by(Year,Sex)%>%
  filter(Year < 1990)%>%
  filter(Year> 1960)%>%
  summarise(NbrBirths = sum(Number))

ggplot(NaissancesAn6090,aes(x=Year,y=NbrBirths, color = Sex))+
  geom_line(size=5)+
  geom_point()+
  ggtitle("Nombre de naissances par année, entre 1960 et 1990")+
  
NaissancesSexeSep <- final_table %>%
  group_by(Year,Sex)%>%
  summarise(NbrBirths = sum(Number))

ggplot(NaissancesSexeSep,aes(x=Year,y=NbrBirths, color = Sex))+
  geom_line(size=5)+
  geom_point()+
  ggtitle("Nombre de naissances par année, sexe séparés")
  


####################################################################
# Partie 3 - Les prenoms les plus communs                          #
####################################################################


##############
#Depuis 1880 #
##############

#########
#Hommes #
#########

PrenomsHommeDepuis1880 <- final_table %>%
  group_by(Name)%>%
  filter(Sex == "M")%>%
  summarise(
    NumberOfBirths = sum(Number)
  )%>%
  arrange(desc(NumberOfBirths))%>%
  top_n(5)

ggplot(PrenomsHommeDepuis1880,aes(x=Name,y=NumberOfBirths))+
  geom_bar(stat = "identity")+
  ggtitle("Most given male names since 1880")


  # Create Data
PrenomsHommeDepuis1880 <- data.frame(
    name=c('James','John','Robert','Michael','William','Others'),
    value= c(2.89,2.86,2.70,2.44,2.34,86.77)
  )

# Basic piechart
ggplot(PrenomsHommeDepuis1880, aes(x="", y=value, fill=name)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0)+
  theme_void()

#########
#Femmes #
#########

PrenomsFemmesDepuis1880 <- final_table %>%
  group_by(Name)%>%
  filter(Sex == "F")%>%
  summarise(
    NumberOfBirths = sum(Number)
  )%>%
  arrange(desc(NumberOfBirths))%>%
  top_n(5)

ggplot(PrenomsFemmesDepuis1880,aes(x=Name,y=NumberOfBirths))+
  geom_bar(stat = "identity")+
  ggtitle("Most given female names since 1880")


name= c('Mary','Elizabeth','Patricia','Jennifer','Linda','Others')
value= c(2.35,0.94,0.89,0.84,0.83,94.15)

# Create Data
PrenonesFemmes1880 <- data.frame(name,value)

# Basic piechart
ggplot(PrenonesFemmes1880, aes(x="", y=value, fill=name)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0)+
  theme_void()


#####################
#Entre 2000 et 2019 #
#####################


#########
#Hommes #
#########

PrenomsHommeDepuis2000 <- final_table %>%
  group_by(Name)%>%
  filter(Sex == "M")%>%
  filter(Year>2000)%>%
  summarise(
    NumberOfBirths = sum(Number)
  )%>%
  arrange(desc(NumberOfBirths))%>%
  top_n(5)

ggplot(PrenomsHommeDepuis2000,aes(x=Name,y=NumberOfBirths))+
  geom_bar(stat = "identity")+
  ggtitle("Most given male names since 2000")
NbrMaleBirths2000 <- final_table %>%
  group_by(Sex)%>%
  filter(Sex == "M")%>%
  filter(Year>2000)%>%
  summarise(
    NumberOfBirths = sum(Number)
  )

top5MaleNames2000 <- PrenomsHommeDepuis2000%>% summarise(
  N=sum(NumberOfBirths)
)
OtherMaleNames2000 <- (NbrMaleBirths2000[2] - (top5MaleNames2000[1]))
# Create Data
PrenomsHomme2000 <- data.frame(
  name=c('Jacob','Michael','Ethan','William','Mathew','Others'),
  value= c(402290,363299,335423,333255,317778,OtherMaleNames2000)
)
# Basic piechart
ggplot(PrenomsHomme2000, aes(x="", y=value, fill=name)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0)+
  theme_void()

#########
#Femmes #
#########

PrenomsFemmeDepuis2000 <- final_table %>%
  group_by(Name)%>%
  filter(Sex == "F")%>%
  filter(Year>2000)%>%
  summarise(
    NumberOfBirths = sum(Number)
  )%>%
  arrange(desc(NumberOfBirths))%>%
  top_n(5)

ggplot(PrenomsFemmeDepuis2000,aes(x=Name,y=NumberOfBirths))+
  geom_bar(stat = "identity")+
  ggtitle("Most given female names since 2000")


NbrFemaleBirths2000 <- final_table %>%
  group_by(Sex)%>%
  filter(Sex == "F")%>%
  filter(Year>2000)%>%
  summarise(
    NumberOfBirths = sum(Number)
  )

top5FemaleNames2000 <- PrenomsFemmeDepuis2000%>% summarise(
  N=sum(NumberOfBirths)
)
OtherFemaleNames2000 <- (NbrMaleBirths2000[2] - (top5FemaleNames2000[1]))

name= c('Emma','Olivia','Emily','Isabella','Sophia','Others')
value= c(363402,327356,315202,313471,293494,OtherFemaleNames2000)

# Create Data
PrenomsFemme2000 <- data.frame(name,value)

# Basic piechart
ggplot(PrenomsFemme2000, aes(x="", y=value, fill=name)) +
  geom_bar(stat="identity", width=1) +
  coord_polar("y", start=0)+
  theme_void()


##############################################
# The evolution of the name James since 1880 #
##############################################

EvolutionJames <- final_table %>%
  group_by(Year)%>%
  filter(Name == "James")%>%
  summarise(
    NumberOfBirths = sum(Number)
  )

ggplot(EvolutionJames,aes(x=Year,y=NumberOfBirths))+
  geom_line(size=5)+
  geom_point()+
  ggtitle("The evolution of the name James since 1880")
  


##############################################
# The evolution of the name Mary since 1880 #
##############################################
EvolutionMary <- final_table %>%
  group_by(Year)%>%
  filter(Name == "Mary")%>%
  summarise(
    NumberOfBirths = sum(Number)
  )

ggplot(EvolutionMary,aes(x=Year,y=NumberOfBirths))+
  geom_line(size=5)+
  geom_point()+
  ggtitle("The evolution of the name Mary since 1880")












