final_data3 <- read_excel("~/Desktop/Data Visual/final_data3.xlsx")
final_data3$GDP <- as.numeric(final_data3$GDP)
final_data3$Electricity <- as.numeric(final_data3$Electricity)
final_data3$Year <- as.numeric(final_data3$Year)
final_data3 <- final_data3 %>%
filter(Year == 2022)
plot0 <- plot_ly(final_data3,
x = ~Electricity,
y = ~GDP_data,
type = 'scatter',
mode = "markers"
)
plot0
#Okabe Ito Palette
custom_colors <- c("Eastern Mediterranean" = "#CC79A7",
"Europe" = "#009E73",
"Africa" = "#D55E00",
"Americas" = "#0072B2",
"Asia" = "#F0E442",
"Western Pacific" = "#000000")
plot2 <- plot_ly(final_data3, x = ~Electricity, y = ~GDP,
color = ~ParentLocation, colors = custom_colors,type = 'scatter', mode = "markers", marker = list(symbol = 'diamond', size = 10)) %>%
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)")
)
plot2
##1) color palette ##2) shape of point ##3) size of point ##4) group by region
#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(final_data3, x = ~Electricity, y = ~GDP_data,
color = ~ParentLocation, colors = custom_colors, text = ~paste("Region: ", Country, "\n",
"Electricity Access: ", round(Electricity, 2), "%", "\n",
"GDP: $", format(round(GDP_data / 1e9, 2), big.mark = ","), "B"),
hoverinfo = "text", type = 'scatter', mode = "markers", marker = list(symbol = 'diamond', size = 10, opacity = 0.6)) %>%
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)")
)
plot3
##5) add information to show the points GDP and Electricity ##6) opacity
#Okabe Ito Palette (1 aesthetic change)
custom_colors <- c("Eastern Mediterranean" = "#CC79A7",
"Europe" = "#009E73",
"Africa" = "#D55E00",
"Americas" = "#0072B2",
"Asia" = "#F0E442",
"Western Pacific" = "#000000")
plot4 <- plot_ly(final_data3, x = ~Electricity, y = ~GDP_data, color = ~ParentLocation,
colors = custom_colors, text = ~paste("Country: ", Country, "\n",
"Electricity Access: ", round(Electricity, 2), "%", "\n",
"GDP: $", format(round(GDP_data/ 1e9, 2), big.mark = ","), "B"),
hoverinfo = "text",
type = 'scatter',
mode = "markers",
marker = list(symbol = 'diamond', size = 10, opacity = 0.5)
) %>%
layout(
title = list(
text = "Percentage of Population with Access to Electricity vs. GDP",
x = 0.14, # 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)"),
legend = list(
title = list(
text = "Region" # Title text for the legend
))
)
plot4
##7) title / added a legend title
library(plotly)
library(dplyr)
# Okabe-Ito Palette
custom_colors <- c(
"Eastern Mediterranean" = "#CC79A7",
"Europe" = "#009E73",
"Africa" = "#D55E00",
"Americas" = "#0072B2",
"Asia" = "#F0E442",
"Western Pacific" = "#000000"
)
# Define regions
regions <- c("Eastern Mediterranean", "Europe", "Africa", "Americas", "Asia", "Western Pacific")
# Generate individual plots for each region
plots <- lapply(regions, function(region) {
region_data <- final_data3 %>% filter(ParentLocation == region)
plot_ly(
region_data,
x = ~Electricity,
y = ~GDP_data,
text = ~paste("Country: ", Country, "\n",
"Electricity Access: ", round(Electricity, 2), "%", "\n",
"GDP: $", format(round(GDP_data / 1e9, 2), big.mark = ","), "B"),
hoverinfo = "text",
type = 'scatter',
mode = "markers",
marker = list(
symbol = 'diamond',
size = 10,
opacity = 0.6,
color = custom_colors[region]
),
showlegend = FALSE
) %>%
layout(
xaxis = list(title = "Access to Electricity (%)", dtick = 20),
yaxis = list(title = "GDP (USD)") # Y-axis label
)
})
# Combine all plots into a single subplot layout
facet_plot <- subplot(
plots,
nrows = 3, # Number of rows
titleX = TRUE,
titleY = TRUE,
shareX = TRUE,
shareY = TRUE
)
# Add individual headers (region titles) for each plot
annotations <- lapply(seq_along(regions), function(i) {
row <- ceiling(i / 2) # Compute row number (2 plots per row)
col <- ifelse(i %% 2 == 1, 0.25, 0.75) # Left or right column position
list(
x = col, # Horizontal position of the annotation
y = 1 - (row - 1) / 3, # Vertical position (based on row)
text = regions[i],
xref = 'paper',
yref = 'paper',
showarrow = FALSE,
font = list(size = 14, color = "black"),
align = "center"
)
})
# Add annotations and title to the layout
facet_plot <- facet_plot %>%
layout(
title = list(
text = "Access to Electricity vs. GDP by Region",
x = 0.5,
font = list(size = 18)
),
annotations = annotations
)
facet_plot
#8) Facet groups
##Used ChatGPT for: formatting / alignment questions, how to do the faceted scatterplot, and adding annotations.