This globe shows the total recorded trade value for each country, based on all years and commodities in the dataset. Each country reflects its cumulative imports and exports in U.S. dollars, representing its overall trade activity during the observed period.
Countries without data are shown in gray. These include:
Old countries or dissolved regions (e.g., Serbia and Montenegro),
Trade zones or regional groupings (e.g., EU-28, Other Asia, nes),
Or countries with incomplete or missing records.
The globe highlights where trade was most heavily reported and where gaps exist in the data. It gives a high-level view of how global trade is distributed and where it’s missing.
Source Code
---title: "Globe"format: html: theme: cosmo css: styles.cssjupyter: falseexecute: echo: false warning: false message: false---```{r}library(tidyverse)library(tidymodels)library(primer.data)library(knitr)library(kableExtra)library(broom)library(gt)library(stringr)library(fixest)gct <-read_csv("commodity_trade_statistics_data.csv")``````{r}library(tidyverse)library(plotly)library(rnaturalearth)library(rnaturalearthdata)library(countrycode)# Pre-process datagct_filtered <- gct %>%filter(flow =="Import", !is.na(trade_usd)) %>%mutate(period =if_else(year <=2008, "Pre-2008", "Post-2008"))# Aggregate by country and periodgct_map <- gct_filtered %>%group_by(country_or_area, period) %>%summarize(total_trade =sum(trade_usd, na.rm =TRUE), .groups ="drop") %>%mutate(trade_label =case_when( total_trade >=1e12~paste0("$", round(total_trade /1e12, 1), "T"), total_trade >=1e9~paste0("$", round(total_trade /1e9, 1), "B"), total_trade >=1e6~paste0("$", round(total_trade /1e6, 1), "M"), total_trade >0~paste0("Small value ($", round(total_trade, 0), ")"), total_trade ==0~"No trade reported",TRUE~NA_character_ ),iso_a3 =countrycode(country_or_area, "country.name", "iso3c") )# Prepare world mapworld <-ne_countries(scale ="medium", returnclass ="sf")# Join for both periodsworld_data <- world %>%select(iso_a3, name) %>%right_join(gct_map, by ="iso_a3") %>%mutate(z =ifelse(is.na(total_trade), -1, total_trade),hover_text =ifelse(is.na(total_trade),paste(name, "<br>No data"),paste(name, "<br>", trade_label)) )# Define color scalecolor_scale <-list(list(0, "rgb(200,200,200)"),list(0.000001, "#f5f0e1"),list(0.33, "#c2b280"),list(0.66, "#8c6d31"),list(1, "#5c4422"))# Separate frames for pre/postframes <-split(world_data, world_data$period)# Create initial plot (Pre-2008)p <-plot_ly() %>%add_trace(type ="choropleth",locations = frames[["Pre-2008"]]$iso_a3,z = frames[["Pre-2008"]]$z,text = frames[["Pre-2008"]]$hover_text,colorscale = color_scale,zmin =-1,zmax =max(world_data$z, na.rm =TRUE),hoverinfo ="text",marker =list(line =list(width =0)),showscale =TRUE,colorbar =list(title ="Trade Value", tickprefix ="$") ) %>%layout(geo =list(projection =list(type ="orthographic"),showland =TRUE,showcountries =TRUE,landcolor ="gray90",oceancolor ="white",countrycolor ="gray20",bgcolor ="white",showframe =FALSE,showcoastlines =FALSE ),updatemenus =list(list(type ="buttons",direction ="right",x =0.5,y =1.05,showactive =TRUE,buttons =list(list(label ="Pre-2008",method ="restyle",args =list(list(locations =list(frames[["Pre-2008"]]$iso_a3),z =list(frames[["Pre-2008"]]$z),text =list(frames[["Pre-2008"]]$hover_text) )) ),list(label ="Post-2008",method ="restyle",args =list(list(locations =list(frames[["Post-2008"]]$iso_a3),z =list(frames[["Post-2008"]]$z),text =list(frames[["Post-2008"]]$hover_text) )) ) ) ) ),annotations =list(list(text ="Note: Yellow countries have missing trade data",x =0.5,y =-0.1,showarrow =FALSE,xref ="paper",yref ="paper",font =list(size =12, color ="gray20") ) ) )p```This globe shows the total recorded trade value for each country, based on all years and commodities in the dataset. Each country reflects its cumulative imports and exports in U.S. dollars, representing its overall trade activity during the observed period.Countries without data are shown in gray. These include:Old countries or dissolved regions (e.g., Serbia and Montenegro),Trade zones or regional groupings (e.g., EU-28, Other Asia, nes),Or countries with incomplete or missing records.The globe highlights where trade was most heavily reported and where gaps exist in the data. It gives a high-level view of how global trade is distributed and where it’s missing.