Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
migueldiazpdj authored Nov 5, 2023
1 parent ad4fff7 commit 1bc8652
Show file tree
Hide file tree
Showing 5 changed files with 315 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Code/Ageofretirment.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
library(usmap)
library(ggplot2)
library(extrafont)

# Create a data frame with state names and average retirement age
retirement_data <- data.frame(
state = c("District of Columbia", "Hawaii", "Massachusetts", "South Dakota", "Maryland", "Connecticut", "New Jersey", "Vermont", "Rhode Island", "New Hampshire",
"Colorado", "Virginia", "North Dakota", "Minnesota", "Utah", "Nebraska", "Iowa", "Texas", "Kansas", "California",
"New York", "Washington", "Pennsylvania", "Wisconsin", "Florida", "Montana", "Illinois", "Idaho", "Wyoming", "Tennessee",
"Oregon", "Maine", "Nevada", "Delaware", "Arizona", "North Carolina", "South Carolina", "Ohio", "Indiana", "Missouri", "Georgia", "Mississippi",
"Louisiana", "New Mexico", "Michigan", "Kentucky", "Alabama", "Oklahoma", "Arkansas", "Alaska", "West Virginia"),
values = c(67, 66, 66, 66, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 63,
63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 62, 62, 62, 62, 62, 62, 62, 61, 61)
)

# Your existing code for creating the plot
Retirement_plot <- plot_usmap(data = retirement_data, color = "#0061ff") +
scale_fill_continuous(
low = "#60efff", high = "#0061ff", name = "", label = scales::comma
) + theme(legend.position = "right",
legend.text = element_text(size = 16)) +
labs(title = "Average Retirement Age in Every State")

# Change the title font
Retirement_plot + theme(
plot.title = element_text(family = "Lucida Sans Unicode", size = 16, face = "bold", hjust = 0.5, vjust = - 10)
) +
annotate(geom = "text", x = 0.5, y = -0.5, label = "Mean: 63.86 and Median: 64", size = 5, hjust = -0.4, vjust = 30)

ggsave("Retirement_plot.jpg",width = 3840, height = 2160, units = c("px"))

# Mean and median of the plot:
Median <- median(retirement_data$values)
Median
Mean <- mean(retirement_data$values)
Mean


44 changes: 44 additions & 0 deletions Code/Employment_Rate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Load the ggplot2 library
library(ggplot2)
library(extrafont)
# Create a data frame from the provided data
data <- data.frame(
Country = c("Netherlands", "Iceland", "Australia", "New Zealand", "Switzerland", "Norway", "Canada", "Denmark",
"United Kingdom", "Austria", "Germany", "United States", "Ireland", "G7", "Japan", "Finland",
"Sweden", "Israel", "OECD - Total", "Mexico", "Colombia", "Estonia", "Türkiye", "Euro area",
"France", "European Union", "Slovenia", "Luxembourg", "Latvia", "Costa Rica", "Portugal",
"Lithuania", "Poland", "Korea", "Belgium", "Hungary", "Czech Republic", "Russia", "Spain", "Chile",
"Slovak Republic", "Italy", "Greece", "South Africa"),
Employment_Rate = c(77.05, 69.35, 65.18, 62.81, 61.39, 59.04, 57.80, 56.27, 54.38, 53.83, 51.35, 51.28, 48.84, 48.23, 47.85, 47.02, 46.37,
43.84, 43.75, 41.95, 37.45, 37.45, 36.97, 36.51, 35.41, 35.39, 33.08, 30.45, 30.23, 30.02, 29.23,
29.10, 28.18, 27.76, 26.90, 26.50, 25.63, 25.16, 23.78, 23.65, 21.82, 20.30, 18.25, 10.45)
)

# Highlight "Netherlands" and "Iceland" with different colors
highlighted_countries <- c("Spain", "United States")
data$Highlight <- ifelse(data$Country %in% highlighted_countries, "Highlighted", "Other")

# Create a ggplot
plot <- ggplot(data, aes(x = reorder(Country, Employment_Rate), y = Employment_Rate, fill = Highlight)) +
geom_bar(stat = "identity") +
labs(x = "Country", y = "Employment Rate") +
ggtitle("Employment Rate by Country (15-24 years-olds)") +
scale_fill_manual(values = c("Highlighted" = c("#d62828"), "Other" = "#003049"))

# Rotate the x-axis labels for better readability
plot + theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1, size = 12),
axis.title.x = element_text(size = 14),
axis.title.y = element_text(size = 14),
plot.title = element_text(size = 16, hjust = 0.5))

ggsave("EmplymentRate.jpg",width = 3840, height = 2160, units = c("px"))

spain_employment_rate <- data[data$Country == "Spain", "Employment_Rate"]
print(spain_employment_rate)

usa_employment_rate <- data[data$Country == "United States", "Employment_Rate"]
print(usa_employment_rate)
58 changes: 58 additions & 0 deletions Code/FacetPlot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
library(dplyr)
library(ggplot2)
library(reshape2)
library(extrafont)

# Importing time_spent and cleaning the data (transforming to hours)
time_spent
time_spent_edited <- time_spent[,-c(1,2)]
colnames(time_spent_edited) <- c("Age","Alone","Friends","Children","Family","Partner","Coworkers")
time_spent_edited <- time_spent_edited %>%
mutate(time_spent_edited[,c(2:7)]/60)
time_spent_edited <- time_spent_edited %>%
mutate(round(time_spent_edited[,c(2:7)],2))
rownames(time_spent_edited) <- time_spent_edited$Age
View(time_spent_edited)

# Melting data to form the necessary column names and numbers
time_spent_long <- melt(time_spent_edited, id.vars = "Age", variable.name = "Activity", value.name = "Hours")

# Define custom colors for each activity (total of 6 colors)
custom_colors <- c("#e03c31", "#f7ea48","#2dc84d", "#90e0ef", "#147bd1", "#FF00FF")

# Create a faceted plot with custom line colors and modified titles
facet_plot <- ggplot(time_spent_long, aes(x = Age, y = Hours, color = Activity)) +
geom_line(size = 1.75) +
labs(x = "Age", y = "Hours Spent") +
scale_x_continuous(limits = c(15, max(time_spent_long$Age))) +
facet_wrap(~Activity, ncol = 3) + # Display facets in 3 columns
scale_color_manual(values = custom_colors) + # Assign custom colors to each facet
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(color = "gray"),
strip.text = element_text(size = 17, color = "black"),
strip.background = element_rect(size=0.8, linetype='solid'),
legend.position = "none" # Remove the legend
)
ggsave("Facet_plot.jpg",width = 3840, height = 2160, units = c("px"))
# Print the faceted plot
print(facet_plot)

#Creating a line graph with each representation of time spend based on age
ggplot(time_spent_long, aes(x = Age, y = Hours, color = Activity)) +
geom_line(size = 1.2) +
scale_color_manual(values = custom_colors) +
labs(x = "Age", y = "Hours Spent") +
scale_x_continuous(limits = c(15, max(time_spent_edited$Age))) +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 12),
plot.background = element_rect(fill = "white"),
plot.title = element_text(size = 16, face = "bold", hjust = 0.5) # Center the title horizontally
) +
ggtitle("Time Spent during life")
ggsave("Time_spent.jpg",width = 3840, height = 2160, units = c("px"))
141 changes: 141 additions & 0 deletions Code/SeparatedGrphicsProject.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
library(dplyr)
library(ggplot2)
library(reshape2)
library(extrafont)

#Graphing line plots of each variable in their own graph
ggplot(time_spent_long, aes(x = Age, y = Minutes, color = Activity)) +
geom_line() +
labs(x = "Age", y = "Minutes Spent") +
scale_x_continuous(limits = c(15, max(time_spent_long$Age))) +
facet_wrap(~Activity)

#Filtering for Alone. Making a table and a graph
Alone_Table <- time_spent_edited %>%
summarize(Age,Alone)
Alone_Table
ggplot(time_spent_edited, aes(x=Age,y=Alone))+
geom_line(size = 1.5, color = "#e03c31") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#e03c31"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#e03c31", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours Alone",
title = "Time Alone"
)
ggsave("Alone.jpg",width = 3840, height = 2160, units = c("px"))

#Filtering for With_Friends. Making a table and a graph
Friends_Table <- time_spent_edited %>%
summarize(Age,Friends)
Friends_Table
ggplot(time_spent_edited, aes(x=Age,y=Friends))+
geom_line(size = 1.5, color = "#f7ea48") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#f7ea48"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#f7ea48", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours with Friends",
title = "Time with Friends"
)
ggsave("Friends.jpg",width = 3840, height = 2160, units = c("px"))

#Filtering for With_Children. Making a table and a graph
Children_Table <- time_spent_edited %>%
summarize(Age,Children)
Children_Table
ggplot(time_spent_edited, aes(x=Age,y=Children))+
geom_line(size = 1.5, color = "#2dc84d") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#2dc84d"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#2dc84d", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours with Children",
title = "Time with your Children"
)
ggsave("Children.jpg",width = 3840, height = 2160, units = c("px"))

mean(time_spent_edited$Children)
median(time_spent_edited$Children)

#Filtering for With_Immediate_Family. Making a table and a graph
Family_Table <-time_spent_edited %>%
summarize(Age,Family)
Family_Table
ggplot(time_spent_edited, aes(x=Age,y=Family))+
geom_line()
ggplot(time_spent_edited, aes(x=Age,y=Family))+
geom_line(size = 1.5, color = "#90e0ef") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#90e0ef"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#90e0ef", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours with Family",
title = "Time with your Family"
)

ggsave("Family.jpg",width = 3840, height = 2160, units = c("px"))

#Filtering for With_Partner. Making a table and a graph
Partner_Table <-time_spent_edited %>%
summarize(Age,Partner)
Partner_Table
ggplot(time_spent_edited, aes(x=Age,y=Partner))+
geom_line()
ggplot(time_spent_edited, aes(x=Age,y=Partner))+
geom_line(size = 1.5, color = "#147bd1") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#147bd1"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#147bd1", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours with Patner",
title = "Time with your Partner"
)
ggsave("Partner.jpg",width = 3840, height = 2160, units = c("px"))

#Filtering for Coworker. Making a table and a graph
Partner_Table <-time_spent_edited %>%
summarize(Age,Coworkers)
Partner_Table
ggplot(time_spent_edited, aes(x=Age,y=Coworkers))+
geom_line()
ggplot(time_spent_edited, aes(x=Age,y=Coworkers))+
geom_line(size = 1.5, color = "#FF00FF") +
theme_minimal() +
theme(
text = element_text(family = "Lucida Sans Unicode", size = 12),
axis.title = element_text(size = 15, color = "#FF00FF"),
legend.position = "none", # Remove the legend
plot.title = element_text(size = 24, color = "#FF00FF", hjust = 0.5) # Título grande en el centro
) +
labs(
x = "Age",
y = "Hours with Coworkers",
title = "Time with your Coworkers"
)
ggsave("Coworker.jpg",width = 3840, height = 2160, units = c("px"))
34 changes: 34 additions & 0 deletions Code/us_child_care.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
library(ggplot2)

#Data frame
data <- data.frame(
Year = c(1965, 1975, 1985, 1998),
Married_Fathers = c(17, 17, 26, 51),
Married_Mothers = c(84, 67, 80, 99),
Single_Mothers = c(59, 63, 67, 85)
)

# Line ggplot
ggplot(data, aes(x = Year)) +
geom_line(aes(y = Married_Fathers, color = "Married Fathers"), size = 1) +
geom_line(aes(y = Married_Mothers, color = "Married Mothers"), size = 1) +
geom_line(aes(y = Single_Mothers, color = "Single Mothers"), size = 1) +
geom_point(aes(y = Married_Fathers, color = "Married Fathers"), size=1.5)+
geom_point(aes(y = Married_Mothers, color = "Married Mothers"), size = 1.5)+
geom_point(aes(y = Single_Mothers, color = "Single Mothers"), size =1.5)+
labs(title = "Child Care Time per day (with children age 18 or under)",
x = "Year",
y = "minutes") +
scale_color_manual(
values = c("Married Fathers" = "#00a6ed", "Married Mothers" = "#f6511d", "Single Mothers" = "#ffb400")) +
theme_minimal() +
theme(legend.title = element_blank(),
plot.title = element_text(size = 20, hjust = 0.5),
legend.text = element_text(size = 18),
axis.title = element_text(size = 16),
axis.text = element_text(size = 12))


ggsave("UsChildCare_plot.jpg",width = 3840, height = 2160, units = c("px"))


0 comments on commit 1bc8652

Please sign in to comment.