This page involves the analysis part of our project.
Code
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Code
data <-get(load("final_data.RData"))ages <-c("Happy(18-24)(%)","Happy(25-34)(%)","Happy(35-44)(%)","Happy(45-54)(%)","Happy(55-64)(%)","Happy(65+)(%)")year_ages <- data |>select(Year, ages)
Warning: Using an external vector in selections was deprecated in tidyselect 1.1.0.
ℹ Please use `all_of()` or `any_of()` instead.
# Was:
data %>% select(ages)
# Now:
data %>% select(all_of(ages))
See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
ggplot(year_ages, aes(x = Year)) +geom_line(aes(y =`Happy(18-24)(%)`, color = ages[1], group=1)) +geom_line(aes(y =`Happy(25-34)(%)`, color = ages[2], group=1)) +geom_line(aes(y =`Happy(35-44)(%)`, color = ages[3], group=1)) +geom_line(aes(y =`Happy(45-54)(%)`, color = ages[4], group=1)) +geom_line(aes(y =`Happy(55-64)(%)`, color = ages[5], group=1)) +geom_line(aes(y =`Happy(65+)(%)`, color = ages[6], group=1)) +labs(title ="Happiness Rates by Age Groups by Year")
Code
#GRAPH---2rate_of_change <-c()for (x in (1:14)) { ratio <- ((as.numeric(final_data[(x +1), "Literate(%)"]) *100) /as.numeric(final_data[x, "Literate(%)"])) -100print(ratio) rate_of_change <-c(rate_of_change,ratio)}
ggplot(data, aes(x = Year)) +geom_line(aes(y =as.numeric(`Literate(%)`), color ="red", group=2)) +geom_line(aes(y = happy_pop_total_ratio, color ="blue", group=1)) +labs(title ="Literacy and Happiness by Years") +ylab("Literate(%)") +scale_color_manual(values =c("red", "blue"),labels =c("Happy Population Total","Literate"))
Code
#Pie Plothappiness_percentages <- data[11, c("Happy(18-24)(%)", "Happy(25-34)(%)", "Happy(35-44)(%)", "Happy(45-54)(%)", "Happy(55-64)(%)", "Happy(65+)(%)")]actual_populations <-as.numeric(happiness_percentages)pie(actual_populations, labels = (c("Happy(18-24)(%)", "Happy(25-34)(%)", "Happy(35-44)(%)","Happy(45-54)(%)", "Happy(55-64)(%)", "Happy(65+)(%)")), main ="Population Distribution by Age Group (2018)")
Code
pie_labels <-paste0(round(100* happiness_percentages/sum(happiness_percentages), 2), "%")pie(actual_populations, labels = pie_labels , main ="Population Distribution by Age Group (2018)")
Code
library(ggplot2)your_data <-get(load('testing_data.RData'))ggplot(your_data, aes(x = Year)) +geom_point(aes(y =`Happy(18-24)(%)`, color ="Happy(18-24)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(18-24)(%)`, color ="Neutral(18-24)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(18-24)(%)`, color ="Unhappy(18-24)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Happy(25-34)(%)`, color ="Happy(25-34)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(25-34)(%)`, color ="Neutral(25-34)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(25-34)(%)`, color ="Unhappy(25-34)"), position =position_jitter(width =0.2)) +labs(title ="Scatter Plot of Happy, Neutral, and Unhappy People of Younger Age Group",x ="Year",y ="Percentage") +scale_color_manual(values =c("Happy(18-24)"="blue", "Neutral(18-24)"="green", "Unhappy(18-24)"="red","Happy(25-34)"="blue", "Neutral(25-34)"="green", "Unhappy(25-34)"="red")) +theme_minimal()
Code
library(ggplot2)your_data <-get(load('testing_data.RData'))ggplot(your_data, aes(x = Year)) +geom_point(aes(y =`Happy(35-44)(%)`, color ="Happy(35-44)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(35-44)(%)`, color ="Neutral(35-44)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(35-44)(%)`, color ="Unhappy(35-44)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Happy(45-54)(%)`, color ="Happy(45-54)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(45-54)(%)`, color ="Neutral(45-54)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(45-54)(%)`, color ="Unhappy(45-54)"), position =position_jitter(width =0.2)) +labs(title ="Scatter Plot of Happy, Neutral, and Unhappy People Middle Aged Group",x ="Year",y ="Percentage") +scale_color_manual(values =c("Happy(35-44)"="blue", "Neutral(35-44)"="green", "Unhappy(35-44)"="red","Happy(45-54)"="blue", "Neutral(45-54)"="green", "Unhappy(45-54)"="red")) +theme_minimal()
Code
library(ggplot2)your_data <-get(load('testing_data.RData'))ggplot(your_data, aes(x = Year)) +geom_point(aes(y =`Happy(55-64)(%)`, color ="Happy(55-64)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(55-64)(%)`, color ="Neutral(55-64)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(55-64)(%)`, color ="Unhappy(55-64)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Happy(65+)(%)`, color ="Happy(65+)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Neutral(65+)(%)`, color ="Neutral(65+)"), position =position_jitter(width =0.2)) +geom_point(aes(y =`Unhappy(65+)(%)`, color ="Unhappy(65+)"), position =position_jitter(width =0.2)) +labs(title ="Scatter Plot of Happy, Neutral, and Unhappy People of Older Age Group",x ="Year",y ="Percentage") +scale_color_manual(values =c("Happy(55-64)"="blue", "Neutral(55-64)"="green", "Unhappy(55-64)"="red","Happy(65+)"="blue", "Neutral(65+)"="green", "Unhappy(65+)"="red")) +theme_minimal()
Code
library(ggplot2)your_data <-get(load('testing_data.RData'))age_groups <-c("18-24", "25-34", "35-44", "45-54", "55-64", "65+")plots_list <-list()for (age_group in age_groups) { age_group_columns <-grep(paste0("(", age_group, ")"), colnames(your_data), value =TRUE) plot <-ggplot(your_data, aes(x = Year)) +geom_bar(aes(y =get(paste0("Pop","(", age_group,")")), fill =factor(age_group)),stat ="identity", position ="stack") +labs(title =paste("Population Distribution - Age Group", age_group),x ="Year",y ="Total Population") +theme_minimal() +theme(legend.position ="top") plots_list[[age_group]] <- plot}for (age_group in age_groups) {print(plots_list[[age_group]])}