Code

library(readxl)
pwt1001 <- read_excel("pwt110.xlsx",sheet="Data")
pwt1001 <-pwt1001[1:7]
pwt1001$gdp_per_cap <- pwt1001$rgdpo/pwt1001$pop

WPP2022 <- read_excel("WPP2024_GEN_F01_DEMOGRAPHIC_INDICATORS_COMPACT.xlsx",skip = 16)

colnames(WPP2022)[3] <- "country"
colnames(WPP2022)[11] <- "year"

colnames(WPP2022)[27] <- "fertility"
WPP2022$fertility <- as.numeric(WPP2022$fertility)

merged_fertility_gdp <- merge(pwt1001,WPP2022, by=c("country","year"))
library(plotly)
df <- merged_fertility_gdp %>%
  filter(year >= 1970)

pl <- plot_ly(df, x = ~gdp_per_cap,
              y = ~fertility, opacity = 0.5,
              frame = ~year) %>%
  
  add_trace(type = "scatter", mode = "markers", size = ~pop)

pl
updatemenus <- list(
  list(
    active = 0,
    x = 1,
    type = 'buttons',
    direction = "right",
    buttons = list(
      list(
        label = "Linear X",
        method = "update",
        args = list(list(visible = c(FALSE, TRUE)),
                    list (xaxis = list(zeroline = FALSE,
                            title = "GDP per capita USD")
        ))),
    list(
      label = "Log X",
      method = "update",
      args = list(list(visible = c(TRUE, FALSE)),
                  list(xaxis = list(zeroline = FALSE,
                                    title = "log(GDP per capita USD)",
                                    type = "log")
                                    )))
    )
  )
)
p3 <- plot_ly(df, type = "scatter",
              mode = "markers", frame = ~year) %>%
  
  add_trace(x = ~gdp_per_cap, y = ~fertility,
            opacity = .5, size = ~pop,
            text = paste("<b>Country</b>: ", df$country,
                         "<br><b>Fertility</b>: ",
                         df$fertility,
                         "<br><b>GDP per cap</b>: ",
                         paste0("$", round(df$gdp_per_cap,0)),
                         "<br><b>Population</b>: ",
                         paste0(round(df$pop,2), "M")),
            marker = list(color = "blue",
                          line = list(color = "blue")),
            name = "") %>%
  
  add_trace(x = ~gdp_per_cap, y = ~fertility,
            opacity = .5, size = ~pop,
            text = paste("<b>Country</b>: ", df$country,
                         "<br><b>Fertility</b>: ",
                         df$fertility,
                         "<br><b>GDP per cap</b>: ",
                         paste0("$", round(df$gdp_per_cap, 0)),
                         "<br><b>Population</b>: ",
                         paste0(round(df$pop, 2), "M")),
            marker = list(color = "blue",line = list(color = "blue")),
            name = "") %>%
  
  layout(title = "GDP Per Capita vs. Average Birth Per Woman from 1970 - 2019",
       yaxis = list(zeroline = FALSE,
                    title = "Average births per woman"),
       xaxis = list(zeroline = FALSE,
                    title = "GDP per capita USD"),
       showlegend = FALSE,
       updatemenus = updatemenus
    ) %>%
  
    config(displayModelBar = F)

p3