Wine exports of Georgia

I create a map highlighting the major importers of Georgian wine for the years 2003, 2007, 2014, and 2021. This map specifically features countries that purchase more than 1% of Georgia’s wine exports. Countries with less significant purchases, each below the 1% threshold, are grouped together.

This approach provides a clear view of Georgia’s key wine export markets over these selected years.

#load the packages we need
library(xlsx)
library(rworldmap)
library(tidyverse)
library(maps)
library(reactable)

#import the data.
georgia<-xlsx::read.xlsx("wine.xlsx",sheetName = "GEO_yearly_2003_21")

#have a look
head(georgia)
  Georgia..exports.million.L     X2003     X2004     X2005     X2006     X2007
1                      World 35.390553 40.664164 64.993509 24.539794 14.897250
2         Russian Federation 30.258139 34.558064 56.714201 13.077662        NA
3                    Ukraine  2.204609  2.446773  4.205678  5.469079  7.723290
4                 Kazakhstan  0.726252  1.181706  0.929390  2.034241  1.937378
5                      China  0.001015        NA  0.036484  0.091804  0.196296
6                    Belarus  0.331826  0.454567  0.487944  0.771235  0.741602
      X2008    X2009     X2010     X2011     X2012     X2013      X2014
1 17.134850 9.552192 12.872715 16.899917 20.211528 35.984013 45.9304988
2  0.006180       NA        NA  0.000135  0.000374 15.581073 28.5837190
3  9.097597 4.416621  6.701518  8.211869  9.708645  9.879008  6.2804015
4  1.491325 1.165688  1.634965  2.229676  3.222178  3.515655  4.0947173
5  0.079640 0.156461  0.203575  0.451631  0.896372  0.709663  0.8668269
6  1.290645 1.037001  1.228439  2.038305  2.288882  2.302703  1.8409320
       X2015     X2016     X2017     X2018     X2019     X2020     X2021
1 27.1715361 37.591273 58.057521 62.680592 70.086030 68.774534 80.466581
2 13.4708797 20.030665 35.766200 38.747434 43.113617 41.134465 47.356252
3  2.6960330  4.846788  6.992322  7.904224  8.194949  8.818327  9.634246
4  3.6217456  2.575159  2.436051  2.711765  2.639482  2.231905  3.091442
5  1.9999204  3.724611  5.413276  4.921522  5.204202  3.491343  4.367785
6  0.9997595  1.114545  1.504857  1.232075  2.192296  3.085515  3.594752
      Total NA. Georgia.exports.USD  X2003.1  X2004.1  X2005.1  X2006.1
1 743.82753  NA               World 42611750 48719443 81328940 41050600
2 418.39906  NA  Russian Federation 31811181 36000928 62975879 16619243
3 125.43198  NA             Ukraine  4785732  4620639  9325894 11560807
4  43.47072  NA          Kazakhstan  1325244  2673592  2276855  4881380
5  32.81243  NA               China     4266       NA    52733   206993
6  28.53788  NA             Belarus   866423  1052394  1087745  1571751
   X2007.1  X2008.1  X2009.1  X2010.1  X2011.1  X2012.1   X2013.1   X2014.1
1 29197385 36862781 31996981 41137668 54086235 64827652 128299366 180401719
2       NA   141386       NA       NA     5549    22945  56845604 111416838
3 14798435 19015703 12527855 18914463 22581124 27373809  30467358  20411992
4  4388058  3683358  4464299  5918748  8798525 12095162  13966779  16485166
5   434194   175484   543508   706103  1959453  3782271   3296091   4664887
6  1452916  3308464  4093957  4169569  5534159  6014031   6651465   7242686
   X2015.1   X2016.1   X2017.1   X2018.1   X2019.1   X2020.1   X2021.1
1 95794826 113533712 171357267 196851003 222849364 210311748 239276633
2 43343905  55801289  98926884 114548704 133225898 119558322 130758328
3  7876207  12405143  17825015  21582374  23052396  23781396  25151946
4 14442139   8140465   7763495   8886922   8489048   6884476   9628254
5  8655155  13953854  20465752  19777061  18863542  13267960  16297627
6  3185267   3385986   4710962   4182745   7223074   9408333  11019499
  Grand.Total NA..1 Georgia.exports.USD.l  X2003.2  X2004.2  X2005.2  X2006.2
1  2030495072    NA                 World 1.204043 1.198093 1.251339 1.672818
2  1012002882    NA    Russian Federation 1.051326 1.041752 1.110408 1.270811
3   328058288    NA               Ukraine 2.170785 1.888462 2.217453 2.113849
4   145191965    NA            Kazakhstan 1.824772 2.262485 2.449838 2.399608
5   127106935    NA                 China 4.202956       NA 1.445373 2.254727
6    86161425    NA               Belarus 2.611076 2.315157 2.229241 2.037966
   X2007.2  X2008.2  X2009.2  X2010.2  X2011.2  X2012.2  X2013.2  X2014.2
1 1.959918 2.151334 3.349700 3.195726 3.200385 3.207459 3.565455 3.927711
2       NA       NA       NA       NA       NA       NA 3.648375 3.897913
3 1.916079 2.090190 2.836525 2.822415 2.749815 2.819529 3.084050 3.250109
4 2.264947 2.469856 3.829755 3.620107 3.946100 3.753722 3.972739 4.025960
5 2.211935 2.203466 3.473760 3.468515 4.338615 4.219533 4.644586 5.381567
6 1.959159 2.563419 3.947881 3.394201 2.715079 2.627497 2.888547 3.934249
   X2015.2  X2016.2  X2017.2  X2018.2  X2019.2  X2020.2  X2021.2 Grand.Total.1
1 3.525558 3.020215 2.951509 3.140542 3.179655 3.057989 2.973615      2.729793
2 3.217600 2.785793 2.765932 2.956291 3.090112 2.906524 2.761163      2.418750
3 2.921406 2.559456 2.549227 2.730486 2.813001 2.696815 2.610681      2.615428
4 3.987618 3.161151 3.186919 3.277173 3.216180 3.084573 3.114487      3.339995
5 4.327750 3.746392 3.780659 4.018485 3.624675 3.800246 3.731326      3.873744
6 3.186033 3.037999 3.130504 3.394879 3.294752 3.049194 3.065441      3.019195

Exports are in million litres.

#check if countries' names match the database of rworldmaps
joinCountryData2Map(georgia,joinCode = "NAME",nameJoinColumn = "Georgia..exports.million.L",verbose = T)
82 codes from your data successfully matched countries in the map
14 codes from your data failed to match with a country code in the map
      failedCodes failedCountries                  
 [1,] NA          "World"                          
 [2,] NA          "Rep. of Moldova"                
 [3,] NA          "Czechia"                        
 [4,] NA          "China, Hong Kong SAR"           
 [5,] NA          "Türkiye"                        
 [6,] NA          "Rep. of Korea"                  
 [7,] NA          "Other Asia, nes"                
 [8,] NA          "Dem. People's Rep. of Korea"    
 [9,] NA          "Serbia and Montenegro (...2005)"
[10,] NA          "China, Macao SAR"               
[11,] NA          "Br. Virgin Isds"                
[12,] NA          "Côte d'Ivoire"                  
[13,] NA          "North Macedonia"                
[14,] NA          "Number of trade partners"       
161 codes from the map weren't represented in your data
#create a vector for countries that don’t match
country_mapping <- c("Rep. of Moldova" = "Moldova",
                     "Czechia" = "Czech Republic",
                     "China, Hong Kong SAR"="Hong Kong",
                     "Türkiye"="Turkey",
                     "Rep. of Korea"="South Korea",
                     "Dem. People's Rep. of Korea"="North Korea",
                     "China, Macao SAR"="Macao",
                     "Br. Virgin Isds"="British Virgin Islands",
                     "Côte d'Ivoire"="Ivory Coast",
                     "Serbia and Montenegro (...2005)"="Serbia and Montenegro")

#replace the non-matched countries by the vector created before
georgia <- georgia %>%
  mutate(Georgia..exports.million.L = case_when(
    Georgia..exports.million.L %in% names(country_mapping) ~ country_mapping[Georgia..exports.million.L],
    TRUE ~ Georgia..exports.million.L))

#delete the last row of the number of trade partners.
georgia<-georgia[-nrow(georgia),] 

#replace "Russian Federation" with "Russia"
georgia<-georgia %>%
  mutate(Georgia..exports.million.L=ifelse(Georgia..exports.million.L=="Russian Federation","Russia",Georgia..exports.million.L))

#eliminate "Other Asia, nes" since we're interested in countries, not regions.
georgia<-georgia[georgia$Georgia..exports.million.L != "Other Asia, nes",]

#check again
joinCountryData2Map(georgia,joinCode = "NAME",nameJoinColumn = "Georgia..exports.million.L",verbose = T)
91 codes from your data successfully matched countries in the map
3 codes from your data failed to match with a country code in the map
     failedCodes failedCountries        
[1,] NA          "World"                
[2,] ""          "Serbia and Montenegro"
[3,] NA          "North Macedonia"      
152 codes from the map weren't represented in your data
#subset the data to include the years of interest.
georgia_2003<-georgia[,c(1,2)]
georgia_2007<-georgia[,c(1,6)]
georgia_2014<-georgia[,c(1,13)]
georgia_2021<-georgia[,c(1,20)]

#subset trade partners with 1% or more.
georgia_2003$share<-round(georgia_2003$X2003/georgia_2003$X2003[1]*100,2)
georgia_2007$share<-round(georgia_2007$X2007/georgia_2007$X2007[1]*100,2)
georgia_2014$share<-round(georgia_2014$X2014/georgia_2014$X2014[1]*100,2)
georgia_2021$share<-round(georgia_2021$X2021/georgia_2021$X2021[1]*100,2)

#if the share of any country is less than 1%, annotate it as such.
georgia_2003<-georgia_2003%>%
  mutate(category=ifelse(share>=1,Georgia..exports.million.L,"Less than 1%"))
georgia_2007<-georgia_2007%>%
  mutate(category=ifelse(share>=1,Georgia..exports.million.L,"Less than 1%"))
georgia_2014<-georgia_2014%>%
  mutate(category=ifelse(share>=1,Georgia..exports.million.L,"Less than 1%"))
georgia_2021<-georgia_2021%>%
  mutate(category=ifelse(share>=1,Georgia..exports.million.L,"Less than 1%"))

#delete the "world" row.
georgia_2003<-georgia_2003[-1,]
georgia_2007<-georgia_2007[-1,]
georgia_2014<-georgia_2014[-1,]
georgia_2021<-georgia_2021[-1,]

#create a function to add a column with the country alongside its share.
add_legend <- function(df, year) {
  year_col <- paste0("X", year)
  df$legend <- ifelse(!is.na(df[[year_col]]) & df$category != "Less than 1%",
                      paste(df$share, df$category, sep = " , "), NA)
  return(df)
}

#apply it with the data
georgia_2003 <- add_legend(georgia_2003, 2003)
georgia_2007 <- add_legend(georgia_2007, 2007)
georgia_2014 <- add_legend(georgia_2014, 2014)
georgia_2021 <- add_legend(georgia_2021, 2021)

#update the function for countries less than 1% to be annotated as such.
update_legend <- function(df) {
  df$legend <- ifelse(is.na(df$legend) & df$category == "Less than 1%", "Less than 1%", df$legend)
  return(df)
}

#apply it
georgia_2003 <- update_legend(georgia_2003)
georgia_2007 <- update_legend(georgia_2007)
georgia_2014 <- update_legend(georgia_2014)
georgia_2021 <- update_legend(georgia_2021)

#create another function to create a factor column, based on and as a duplicate of "legend" column, for the purpose of using it to customise the colors of the map. 
add_category_factor <- function(df, group_by_col, mutate_col) {
  df %>%
    group_by({{ group_by_col }}) %>%
    mutate(category_factor = fct_inorder({{ mutate_col }}))
}

#apply it.
georgia_2003 <- add_category_factor(georgia_2003, share, legend)
georgia_2007 <- add_category_factor(georgia_2007, share, legend)
georgia_2014 <- add_category_factor(georgia_2014, share, legend)
georgia_2021 <- add_category_factor(georgia_2021, share, legend)

#remove countries where data is not available
georgia_2003<-georgia_2003[!is.na(georgia_2003$share),]
georgia_2007<-georgia_2007[!is.na(georgia_2007$share),]
georgia_2014<-georgia_2014[!is.na(georgia_2014$share),]
georgia_2021<-georgia_2021[!is.na(georgia_2021$share),]

#create a function to arrange/sort data by a specific variable
arrange_by_var <- function(.data, var) {
    .data %>%
        arrange({{ var }})
}

#arrange by share.
georgia_2003<-arrange_by_var(georgia_2003,desc(share))
georgia_2007<-arrange_by_var(georgia_2007,desc(share))
georgia_2014<-arrange_by_var(georgia_2014,desc(share))
georgia_2021<-arrange_by_var(georgia_2021,desc(share))

#calculate the share for each year
georgia$Share2003 <- round(georgia$X2003 / georgia$X2003[georgia$Georgia..exports.million.L == "World"]*100,2)
georgia$Share2007 <- round(georgia$X2007 / georgia$X2007[georgia$Georgia..exports.million.L == "World"]*100,2)
georgia$Share2014 <- round(georgia$X2014 / georgia$X2014[georgia$Georgia..exports.million.L == "World"]*100,2)
georgia$Share2021 <- round(georgia$X2021 / georgia$X2021[georgia$Georgia..exports.million.L == "World"]*100,2)
#use reactable to display a table with exports alongside their share.
#round data to third decimal point
georgia[,c(2,6,13,20)]<-round(georgia[,c(2,6,13,20)],3)

reactable(georgia[,c(1,2,6,13,20)], columns = list(
    Georgia..exports.million.L = colDef(name="Country"),
    X2003=colDef(name="Georgian exports (2003)", cell = function(value, index) {
      if(is.na(georgia$X2003[index]) || is.na(georgia$Share2003[index])) {
      ""
    } else {
        sprintf("%s (%.2f%%)", format(georgia$X2003[index], big.mark = ","), georgia$Share2003[index])
    }
    }),
    X2007=colDef(name="Georgian exports (2007)",cell = function(value, index) {
      if(is.na(georgia$X2007[index]) || is.na(georgia$Share2007[index])) {
      ""
    } else {
        sprintf("%s (%.2f%%)", format(georgia$X2007[index], big.mark = ","), georgia$Share2007[index])
    }
    }),
    X2014=colDef(name="Georgian exports (2014)",cell = function(value, index) {
      if(is.na(georgia$X2014[index]) || is.na(georgia$Share2014[index])) {
      ""
    } else {
        sprintf("%s (%.2f%%)", format(georgia$X2014[index], big.mark = ","), georgia$Share2014[index])
    }
    }),
    X2021=colDef(name="Georgian exports (2021)",cell = function(value, index) {
      if(is.na(georgia$X2021[index]) || is.na(georgia$Share2021[index])) {
      ""
    } else {
        sprintf("%s (%.2f%%)", format(georgia$X2021[index], big.mark = ","), georgia$Share2021[index])
    }
    })
),defaultColDef = colDef(vAlign = "center"), searchable = T, showPageInfo = T, defaultPageSize = 20, showPageSizeOptions = T, bordered = T, striped = T, highlight = T)
#custom colors based on "category_factor"
custom_palette <- colorRampPalette(c("lightblue", "blue"))(length(unique(georgia_2003$category_factor)))

#create a function to generate the map.
create_georgia_map <- function(df, year, mapTitle, cex_legend) {
  # Joining country data to the map
  joined_df <- joinCountryData2Map(df, joinCode = "NAME", nameJoinColumn = "Georgia..exports.million.L", verbose = T)
  
#plotting the map
map <- mapCountryData(joined_df, nameColumnToPlot = "category_factor", catMethod = "categorical",
                        addLegend = F, mapTitle = mapTitle, 
                        missingCountryCol = "gray", colourPalette = custom_palette)
  
#adding the legend
  do.call(addMapLegendBoxes, c(map, x = 'bottomleft', title = "Exporters, in percent:",
                               horiz = F, cex = cex_legend, pt.cex = 1))
  return(map)
}

# Creating maps for each year
georgia_map3 <- create_georgia_map(georgia_2003, 2003, "Wine exports of Georgia, 2003, in million Litres", 0.7)
30 codes from your data successfully matched countries in the map
1 codes from your data failed to match with a country code in the map
     failedCodes failedCountries        
[1,] ""          "Serbia and Montenegro"
213 codes from the map weren't represented in your data

georgia_map7 <- create_georgia_map(georgia_2007, 2007, "Wine exports of Georgia, 2007, in million Litres", 0.5)
36 codes from your data successfully matched countries in the map
0 codes from your data failed to match with a country code in the map
     failedCodes failedCountries
207 codes from the map weren't represented in your data
Warning in rwmGetColours(colourPalette, numColours): 5 colours specified and 13
required, using interpolation to calculate colours

georgia_map14 <- create_georgia_map(georgia_2014, 2014, "Wine exports of Georgia, 2014, in million Litres", 0.6)
46 codes from your data successfully matched countries in the map
0 codes from your data failed to match with a country code in the map
     failedCodes failedCountries
197 codes from the map weren't represented in your data
Warning in rwmGetColours(colourPalette, numColours): 5 colours specified and 9
required, using interpolation to calculate colours

georgia_map21 <- create_georgia_map(georgia_2021, 2021, "Wine exports of Georgia, 2021, in million Litres", 0.6)
62 codes from your data successfully matched countries in the map
1 codes from your data failed to match with a country code in the map
     failedCodes failedCountries  
[1,] NA          "North Macedonia"
181 codes from the map weren't represented in your data
Warning in rwmGetColours(colourPalette, numColours): 5 colours specified and 8
required, using interpolation to calculate colours