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 needlibrary(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 lookhead(georgia)
#check if countries' names match the database of rworldmapsjoinCountryData2Map(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 matchcountry_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 beforegeorgia <- 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 againjoinCountryData2Map(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 datageorgia_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 itgeorgia_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 availablegeorgia_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 variablearrange_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 yeargeorgia$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)
#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 mapmap <-mapCountryData(joined_df, nameColumnToPlot ="category_factor", catMethod ="categorical",addLegend = F, mapTitle = mapTitle, missingCountryCol ="gray", colourPalette = custom_palette)#adding the legenddo.call(addMapLegendBoxes, c(map, x ='bottomleft', title ="Exporters, in percent:",horiz = F, cex = cex_legend, pt.cex =1))return(map)}# Creating maps for each yeargeorgia_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