library(readxl)
library(writexl)
library(dplyr)
##
## 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(tidyr)
library(zoo)
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
final_data3 <- read_excel("~/Desktop/Data Visual/final_data3.xlsx")
final_data <- read_excel("~/Desktop/Data Visual/final_data3.xlsx")
final_data3$GDP <- as.numeric(final_data$GDP)
final_data3$Electricity <- as.numeric(final_data$Electricity)
final_data3$Year <- as.numeric(final_data$Year)
filtered_data <- final_data3 %>%
filter(ParentLocation == "Asia")
test <- filtered_data[,c(1,2,6,9,13)]
plot0 <- plot_ly(
test,
x = ~Electricity,
y = ~GDP_data,
color = ~ParentLocation,
text = ~paste(Country, "<br>Year:", Year),
hoverinfo = "text",
type = 'scatter',
mode = "markers", # Change mode to only markers
frame = ~Year
) %>%
layout(
title = list(
text = "Access to Electricity vs. GDP per Capita",
x = 0.5,
y = 0.95,
font = list(size = 15),
align = "center"
),
xaxis = list(title = "Access to Electricity (%)"),
yaxis = list(title = "GDP per Capita")
) %>%
animation_opts(
frame = 500,
redraw = FALSE,
easing = "linear"
)
plot0
## Warning: Ignoring 34 observations
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
## Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
filtered_data <- filtered_data %>%
complete(Country, Year, fill = list(Electricity = NA, GDP_data = NA)) %>%
arrange(Country, Year)
## AVERAGE DATA
avg_gdp_data <- final_data3 %>%
group_by(ParentLocation, Year) %>%
summarize(avg_GDP = mean(GDP, na.rm = TRUE)) %>%
filter(Year > 2011)
## `summarise()` has grouped output by 'ParentLocation'. You can override using
## the `.groups` argument.
avg_electricity_data <- final_data3 %>%
group_by(ParentLocation, Year) %>%
summarize(avg_electricity = mean(Electricity, na.rm = TRUE)) %>%
filter(Year > 2011)
## `summarise()` has grouped output by 'ParentLocation'. You can override using
## the `.groups` argument.
New <- avg_electricity_data %>%
left_join(avg_gdp_data, by = c("ParentLocation", "Year"))
final_data3$Food_Insecurity <- as.numeric(final_data$Food_Insecurity)
## Warning: NAs introduced by coercion
final_data3 <- final_data3 %>%
mutate(Food_Insecurity = as.numeric(Food_Insecurity))
# Group by ParentLocation and Year, and calculate the mean
avg_food_insecurity_data <- final_data3 %>%
group_by(ParentLocation, Year) %>%
summarize(avg_food_insecurity = mean(Food_Insecurity, na.rm = TRUE)) %>%
filter(Year > 2011)
## `summarise()` has grouped output by 'ParentLocation'. You can override using
## the `.groups` argument.
New <- New %>%
left_join(avg_food_insecurity_data, by = c("ParentLocation", "Year"))
final_data3$GDP_data <- as.numeric(final_data$GDP_data)
avg_gdp <- final_data %>%
group_by(ParentLocation, Year) %>%
summarize(avg_GDP = mean(GDP_data, na.rm = TRUE)) %>%
filter(Year > 2011)
## `summarise()` has grouped output by 'ParentLocation'. You can override using
## the `.groups` argument.
New <- New %>%
left_join(avg_gdp, by = c("ParentLocation", "Year"))
write_xlsx(New, "average_data.xlsx")
plot1 <- plot_ly(New, x = ~avg_electricity, y = ~avg_GDP.y, fill = ~ParentLocation,
color = ~ParentLocation, text = ~ParentLocation,
hoverinfo = "text", type = 'scatter', mode = "markers", frame = ~Year) %>%
layout(
title = list(
text = "Access to Electricity vs. GDP",
x = 0.5, # Center the title horizontally
y = 0.95, # Adjust the vertical position of the title
font = list(size = 15), # Set the font size
align = "center" # Align the title in the center
),
xaxis = list(title = "Electricity Access (%)"),
yaxis = list(title = "GDP (USD)")
)
plot1
## Warning: Ignoring 6 observations
Redraw = t/ f smoothly transition
#Version 2)
#Okabe Ito Palette (1 aesthetic change)
custom_colors <- c("Eastern Mediterranean" = "#CC79A7",
"Europe" = "#009E73",
"Africa" = "#D55E00",
"Americas" = "#0072B2",
"Asia" = "#F0E442",
"Western Pacific" = "#000000")
plot2 <- plot_ly(New, x = ~avg_electricity, y = ~avg_GDP.y,
color = ~ParentLocation, colors = custom_colors, text = ~ParentLocation,
hoverinfo = "text", type = 'scatter', mode = "markers", marker = list(symbol = 'diamond', size = 10), frame = ~Year) %>%
layout(
title = list(
text = "Access to Electricity vs. GDP",
x = 0.28, # Center the title horizontally
y = .98, # Adjust the vertical position of the title
font = list(size = 15) # Set the font size
),
xaxis = list(title = "Electricity Access (%)"),
yaxis = list(title = "GDP (USD)", showline = TRUE)
)
plot2
## Warning: Ignoring 6 observations
#Version 3)
#Okabe Ito Palette (1 aesthetic change)
custom_colors <- c("Eastern Mediterranean" = "#CC79A7",
"Europe" = "#009E73",
"Africa" = "#D55E00",
"Americas" = "#0072B2",
"Asia" = "#F0E442",
"Western Pacific" = "#000000")
plot3 <- plot_ly(New, x = ~avg_electricity, y = ~avg_GDP.y, color = ~ParentLocation,
colors = custom_colors, text = ~paste("Region: ", ParentLocation, "\n",
"Electricity Access: ", round(avg_electricity, 2), "%", "\n",
"GDP: $", format(round(avg_GDP.y / 1e9, 2), big.mark = ","), "B"),
hoverinfo = "text",
type = 'scatter',
mode = "markers",
marker = list(symbol = 'diamond', size = 10),
frame = ~Year
) %>%
layout(
title = list(
text = "Access to Electricity vs. GDP",
x = 0.28, # Center the title horizontally
y = .98, # Adjust the vertical position of the title
font = list(size = 15) # Set the font size
),
xaxis = list(title = "Electricity Access (%)"),
yaxis = list(title = "GDP (USD)", showline = TRUE),
updatemenus = list(
list(
type = "buttons",
showactive = FALSE,
buttons = list(
list(label = "Pause", method = "animate",
args = list(NULL, list(frame = list(duration = 0, redraw = TRUE),
mode = "immediate"))
)
),
x = -.1, # Move the button to the left
xanchor = "left", # Anchor the button to the left
y = -0.05
)
))
plot3
## Warning: Ignoring 6 observations