install.packages(“tidyverse”) install.packages(“janitor”) install.packages(“ggplot2”) install.packages(“maps”) install.packages(“treemapify”)

Load the libraries into R

library(tidyverse) # Includes ggplot2, dplyr, tidyr, and more library(janitor) # For cleaning data library(dplyr) # For data manipulation library(ggplot2) # For data visualization library(maps) # For maps library(treemapify) # For creating treemaps

#DataCleaning

#Import the dataset and take a look at what we are working with data_path <- “/Users/gayatrimahindrakar/Downloads/DataStatsFinalProject/Space_Corrected.csv” df <- read_csv(“/Users/gayatrimahindrakar/Downloads/DataStatsFinalProject/Space_Corrected.csv”) glimpse(df)

#Checking for Duplicates sum(duplicated(df))

#Fix column names df <- clean_names(df) head(df)

#separate date into it’s own column & fix format df\(date_1 <- substr(df\)datum, 5, 16) df\(date <- gsub(" ", "-",gsub(", ", "-",gsub("Jan", "01",gsub("Feb", "02", gsub("Mar", "03",gsub("Apr", "04",gsub("May", "05", gsub("Jun", "06",gsub("Jul", "07",gsub("Aug", "08", gsub("Sep", "09",gsub("Oct", "10",gsub("Nov", "11", gsub("Dec", "12", df\)date_1)))))))))))))) df <- mutate(df, date=as.Date(as.character(date), “%m-%d-%Y”)) head(df\(date) df\)year <- as.numeric(substr(df$date, 1, 4)) glimpse(df)

#Separate rocket name and program name into own columns df <- separate_wider_delim(df, “detail”, delim=” | “, names=c(”rocket_name”, “program”), too_few = “align_start”, too_many = “merge”) glimpse(df)

#Separate countries into own column & fix any incorrect names df <- mutate(df, country=word(location,-1)) count(df, country) df\(country <- gsub("Canaria", "Spain", gsub("Site", "Iran",gsub("Facility", "USA", gsub("Sea", "China",gsub("Ocean", "Australia", gsub("Zealand", "New Zealand", df\)country)))))) count(df, country) glimpse(df)

#Rename column unnamed_0, status_rocker, status_mission, & rocket for clarity df <- rename(df, id=unnamed_0, mission_status=status_mission, rocket_status=status_rocket, cost=rocket) colnames(df)

#Remove “Status” from rocket_status rows df\(rocket_status <- gsub("Status", "", df\)rocket_status) head(df$rocket_status)

#Remove extra columns & column x1 since it duplicates column unnamed_o df <- subset(df, select=-c(x1, date_1)) colnames(df)

#Create column for counting df$count <- as.numeric(1) head(df)

#create 2nd dataframe with na data removed sum(is.na(df)) df2 <- drop_na(df) sum(is.na(df2)) glimpse(df2)

#Visualizations

#Launches by Country country_launches <- df country_launches <- country_launches %>% group_by(country) %>% summarise(total=sum(count)) %>% rename(region=country) country_launches2 <- country_launches world_data <- map_data(‘world’) country_launches <- left_join(world_data, country_launches, by=“region”)

ggplot(country_launches2, aes(reorder(region, +total), total, fill = total))+ geom_bar(stat=“identity”, width=0.90)+ geom_text(aes(label=total), hjust=-0.1, size=3.5, fontface=“bold”)+ scale_fill_gradient(low = “blue”, high = “red”)+ labs(x=““, y=”Number of Launches”, fill=“Total Launches”)+ ylim(0, 1500)+ theme_bw()+ theme(legend.position = “none”, axis.text.x=element_text(size=8), axis.text.y=element_text(size=8), axis.title=element_text(size=12))+ coord_flip()

#Launch status Successes/Failures by country miss_success <- df %>% mutate(mission_success=ifelse(mission_status == “Success”, “Success”, NA)) %>% drop_na(mission_success) miss_success <- miss_success %>% group_by(country) %>% summarise(total=sum(count))

miss_failures <- df %>% mutate(mission_status = case_when(mission_status == “Failure” ~ “Failure”, mission_status == “Partial Failure” ~ “Partial Failure”, mission_status == “Prelaunch Failure” ~ “Prelaunch Failure”, TRUE ~ NA)) %>% drop_na(mission_status)

ggplot(miss_success, aes(reorder(country, -total), total, fill=total))+ geom_bar(stat=“identity”, width=0.90)+ geom_text(aes(label=total), hjust=0.5, vjust=-0.5, size=3.5, fontface=“bold”)+ scale_fill_gradient(low=“green”, high=“darkgreen”)+ ylim(0, 1500)+ labs(x=““, y=”Total Successful Launches”, title=“Total Succesful Launches by Country”)+ theme_bw()+ theme(legend.position=“none”, plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

ggplot(miss_failures, aes(country, fill=mission_status))+ geom_bar(stat=“count”, position=‘dodge’, width=0.75)+ labs(x=““, y=”Number of Launch Failures”, title=“Space Mission Launch Failures by Country”, fill=“Mission Status”)+ ylim(0, 150)+ theme_bw()+ theme(plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

#Worldwide total launches per year with status df_succ_fail <- df df_succ_fail\(mission_status <- gsub("Partial Failure", "Failure", gsub("Prelaunch Failure", "Failure", df\)mission_status))

ggplot(df_succ_fail, aes(year, fill=mission_status))+ geom_bar(stat=“count”, position=‘stack’, width=0.5)+ labs(x=““, y=”Total Number of Launches”, title=“Worldwide Launches per Year with Status”, fill=“Mission Status”)+ ylim(0, 125)+ theme_bw()+ theme(plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

Distribution of Launches by hour

Extract time information from ‘datum’ column

df\(time <- gsub(".*\\d{4} (\\d{2}:\\d{2}).*", "\\1", df\)datum)

Convert time to 24-hour format and extract only the hour

df\(hour <- as.numeric(substr(df\)time, 1, 2))

Count the number of launches for each hour

hourly_launches <- df %>% group_by(hour) %>% summarise(launch_count = n())

Plot the distribution of launches over the 24-hour format

ggplot(hourly_launches, aes(x = hour, y = launch_count)) + geom_bar(stat = “identity”, fill = “steelblue”) + scale_x_continuous(breaks = 0:23) + labs( title = “Distribution of Launches by Hour”, x = “Hour of the Day (24-hour format)”, y = “Number of Launches” ) + theme_minimal()

#Spent Worldwide per Year ttl_s_year <- df2 ttl_s_year <- ttl_s_year %>% group_by(year) %>% summarise(cost=sum(cost))

ggplot(ttl_s_year, aes(x=year, y=cost/10))+ geom_line(color=“blue”)+ labs(x=““, y=”Cost in Millions”, title=“Total Spent Worlwide on Space Shuttle Launches per Year”)+ xlim(1960, 2020)+ theme_bw()+ theme(plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

#Total spent/lost mis_success <- df2 %>% mutate(mission_success = ifelse(mission_status == “Success”, “Success”, NA)) %>% drop_na(mission_success) mis_failures <- df2 %>% mutate(mission_status = case_when(mission_status == “Failure” ~ “Failure”, mission_status == “Partial Failure” ~ “Failure”, mission_status == “Prelaunch Failure” ~ “Failure”, TRUE ~ NA)) %>% drop_na(mission_status)

ttc_summary <- c(sum(mis_success\(cost), sum(mis_failures\)cost)) ttl_spent_lost_labels <- c(“$143,371.5 billion”, “$4,884.14 billion”) barplot(ttc_summary, names.arg=ttl_spent_lost_labels, xlab=“Success/Failure”, ylab=““, main=”Total Amount Spent on Successful vs Lost on Failed Missions”)

#Total spent per company ttl_spent_company <- df2 %>% group_by(company_name) %>% summarise(total=sum(cost)) ttl_spent_company_1 <- ttl_spent_company %>% filter(company_name != “NASA”, company_name != “Arianespace”, company_name != “ULA”, company_name != “RVSN USSR”) ttl_spent_company_2 <- ttl_spent_company %>% filter(company_name != “US Air Force”, company_name != “ExPace”, company_name != “ESA”, company_name != “Sandia”, company_name != “Virgin Orbit”, company_name != “EER”, company_name != “JAXA”, company_name != “Rocket Lab”, company_name != “Roscosmos”, company_name != “CASC”, company_name != “SpaceX”, company_name != “Boeing”, company_name != “Lockheed”, company_name != “Eurockot”, company_name != “ILS”, company_name != “ISRO”, company_name != “Kosmotras”, company_name != “Martin Marietta”, company_name != “MHI”, company_name != “Northrop”, company_name != “VKS RF”)

ggplot(ttl_spent_company_1, aes(reorder(company_name, total), total/10, fill=total))+ geom_bar(stat=“identity”, width=0.90)+ geom_text(aes(label=total/10), hjust=-0.1, size=3.5, fontface=“bold”)+ scale_fill_gradient(low=“blue”, high=“purple”)+ labs(x=““, y=”Cost in Millions”, title=“Total Cost of Launches by Company”)+ theme_bw()+ theme(legend.position = “none”, plot.title=element_text(size=16), axis.text.x=element_text(size=8), axis.text.y=element_text(size=8), axis.title=element_text(size=12))+ coord_flip()

ggplot(ttl_spent_company_2, aes(reorder(company_name, +total), total/10000, fill=total))+ geom_bar(stat=“identity”, width=0.90)+ geom_text(aes(label=total/10000), hjust=-0.1, size=3.5, fontface=“bold”)+ scale_fill_gradient(low=“blue”, high=“purple”)+ labs(x=““, y=”Total Cost in Billions”)+ theme_bw()+ theme(legend.position=“none”, axis.text.x=element_text(size=8), axis.text.y=element_text(size=8), axis.title=element_text(size=12))+ coord_flip()

#Launches per Year by Company launch_company_year <- df %>% filter(company_name != “RVSN USSR”) %>% group_by(year, company_name) %>% reframe(company_name, year, sum(count)) %>% distinct() %>% rename(count=sum(count), Company Name=company_name) launch_company_year_2 <- df %>% filter(company_name == “RVSN USSR”) %>% group_by(year, company_name) %>% reframe(company_name, year, sum(count)) %>% distinct() %>% rename(count=sum(count), Company Name=company_name)

ggplot(launch_company_year, aes(x=year, y=count))+ geom_point(aes(color=Company Name))+ labs(y=“Total Number of Launches”, x=““, title=”Launches per Year by Company”)+ theme_bw()+ theme(plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

ggplot(launch_company_year_2, aes(x=year, y=count))+ geom_point(aes(color=Company Name))+ labs(y=“Total Number of Launches”, x=““, title=”RVSN USSR”)+ theme_bw()+ theme(legend.position=“none”, plot.title=element_text(size=16), axis.text.x=element_text(size=8, angle=90), axis.text.y=element_text(size=8), axis.title=element_text(size=12))

#Distribution of launches per Year library(dplyr)

df2 <- df2 %>% mutate( year = as.numeric(format(date, “%Y”)), month = as.factor(format(date, “%b”)), # Month abbreviated name (Jan, Feb, etc.) day_of_week = weekdays(date) # Weekday name (Mon, Tue, etc.) )

ggplot(df2, aes(x = year)) + geom_histogram(binwidth = 1, fill = “skyblue”, color = “black”, alpha = 0.7) + theme_minimal() + labs(title = “Distribution of Space Missions Launch Year”, x = “Year”, y = “Count of Launches”) + theme(axis.text.x = element_text(angle = 90, hjust = 1))

#Distribution of launches per Month # Remove rows with NA values df <- df %>% drop_na()

Convert the ‘date’ column to a proper date format if not already

df\(date <- as.Date(df\)date)

Extract the month name from the date

df\(month <- format(df\)date, “%B”)

Count the number of launches for each month

monthly_launches <- df %>% group_by(month) %>% summarise(launch_count = n(), .groups = “drop”)

Order the months for proper chronological plotting

monthly_launches\(month <- factor( monthly_launches\)month, levels = c( “January”, “February”, “March”, “April”, “May”, “June”, “July”, “August”, “September”, “October”, “November”, “December” ) )

Plot the distribution

ggplot(monthly_launches, aes(x = month, y = launch_count)) + geom_bar(stat = “identity”, fill = “lightcoral”) + labs( title = “Distribution of Rocket Launches by Month”, x = “Month”, y = “Number of Launches” ) + theme_minimal() + theme(axis.text.x = element_text(angle = 45, hjust = 1))