GRAPHS:

Version 1)

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

Version 2)

#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

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(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

Version 4)

#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

Version 5)

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

QUESTION 1)

QUESTION 2)

In my third graph, I incorporated the previously listed modifications and (5) also changed the opacity of the points. By making this change, I was better able to see the points, as there is substantial overlap amongst them. Additionally, (6) I added hover text which provided precise information for each of the points. The text included the country that the point corresponds to, as well as the relative GDP and electricity access percentage. By adding these details, the viewer can gain additional crucial details which would otherwise not be visible. By the completion of this graph, I liked this one better as it provided greater information that helps the viewer to understand the differences between the various continental regions, as I had been striving to achieve as my main message.

In my fourth graph, (7) I added a title for the legend to explain that they are showing regions. I also made the title more descriptive so that viewers can more quickly understand what the graph is showing. At this point, I liked this graph better than the others as it included additional text which made it easier to understand.

QUESTION 3)

References)

##Used ChatGPT for: formatting / alignment questions, how to do the faceted scatterplot, and adding annotations.