Long to wide format with tidyr (and save it in n files)

The data comes from the https://esa.un.org/unpd/wpp/UN population projections

library(tidyr) #load tidyr or <a href="https://www.tidyverse.org/">tidyverse</a>, the latter being a collection of libraries

setwd("/Users/...") #set your working directory

dt <- read.csv("mydataset.csv", header=T) #read data

head(dt) #look at data

##   Index       Country Year Age Male_Pop Female_Pop
## 1     1 AmericanSamoa 2000   0      874        836
## 2     2 AmericanSamoa 2000   1      773        747
## 3     3 AmericanSamoa 2000   2      760        735
## 4     4 AmericanSamoa 2000   3      783        760
## 5     5 AmericanSamoa 2000   4      820        796
## 6     6 AmericanSamoa 2000   5      851        825

The idea would be to have a KEY column with the variables names and a VALUE column with the values. Since we have 2 value columns (male_pop and female_pop) we first need to gather them into 1 value column (Pop_sex) and then paste Pop_sex with Age.

# get it into the right format for "spread"
dt1 <- dt %>%

  gather(Pop_sex, value, 5:6) %>%

  unite(Pop_age, 5, 4, sep="_", remove=T) %>% # paste cols 5 and 4

  spread(Pop_age, value) %>% # spread into wide format

  write.csv(., file = "~/My folder of choice/nameofmyfile.csv") # this is optional

There’s a useful trick I’ve been using to get n csv files out of one long format dataset (eg. 1 file per year), I’ve found this somewhere in stackoverflow:

customFun  = function(mydt) {
  write.csv(mydt,paste0("name_",unique(mydt$year),".csv"))
  return(mydt)
}

mydt %>% 
  unite(newvar, 3:4, sep="_", remove=T) %>%
  spread(newvar, value) %>%
  group_by(year) %>% 
  do(customFun(.))

Note of the author: wide formats are never very useful but in case you really need them (linear regression &co) tidyr is a very compact solution. Be mindful that spreading over >1000 cols takes time. To get back from wide to long format use gather

Geofacet: Bangladesh 64 districts education

Geofacet example using World Bank data on Bangladesh education attainment

  1. set up grids
  2. upload data, source: World Bank
  3. plot and save

1. Grid for Bangladesh districts:

library(tidyverse)
library(geofacet)
library(ggthemes)
options(scipen = 99)
mygrid <- data.frame(
row = c(1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11),
col = c(3, 2, 3, 4, 3, 4, 5, 3, 4, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 2, 3, 4, 5, 6, 7, 2, 3, 4, 5, 6, 7, 9),
code = c("BG01","BG02","BG03","BG04","BG05","BG06","BG07","BG08","BG09","BG10","BG11","BG12","BG13","BG14","BG15","BG16","BG17","BG18","BG19","BG20","BG21","BG22","BG23","BG24","BG25","BG26","BG27","BG28","BG29","BG30","BG31","BG32","BG33","BG34","BG35","BG36","BG37","BG38","BG39","BG40","BG41","BG42","BG43","BG44","BG45","BG46","BG47","BG48","BG49","BG50","BG51","BG52","BG53","BG54","BG55","BG56","BG57","BG58","BG59","BG60","BG61","BG62","BG63", "BG64"),
name = c("Panchagar","Takurgaong","Nilphamar","Lamonirhat","Dinajpur","Rangpur","Kurigram","Jaipurat","Gaibandha","Naogaon","Bogra","Jamalpur","Sherpar","Mymensingh","Netrokona","Suramganj","Sylhet","Chapai","Rajshani","Nator","Sirajganj","Tangail","Gazipur","Kishoreganj","Habiganj","Moulvibazar","Kushtia","Pabna","Dhaka","Nardiaganj","Narsingdi","Brahmanbaria","Meherpur","Jhenaidah","Magura","Rajbari","Manikganj","Munshiganj","Comilla","Khagrachhari","Rangramati","Chuadanga","Jessore","Gopalganj","Faridpur","Madanipur","Shariyapur","Chandpur","Feni","Chittagong","Badanbari","Narail","Pirojpur","Barisal","Jhalkati","Laksimipur","Noakhali","Satkhira","Khulna","Bagerhat","Borguna","Patuakhali","Bhola","Cox's Bazar"),
stringsAsFactors = FALSE
)

Preview the grid:

geofacet::grid_preview(mygrid)

Screen Shot 2017-07-23 at 16.17.02.png

2. Data from World Bank

dt3<-structure(list(ordinal = c(58L, 58L, 58L, 58L, 58L, 58L, 58L, 34L, 56L, 57L, 34L, 56L, 57L, 34L, 56L, 57L, 34L, 56L, 57L, 34L, 56L, 57L, 34L, 56L, 57L, 34L, 56L, 57L, 53L, 55L, 59L, 53L, 55L, 59L, 53L, 55L, 59L, 53L, 55L, 59L, 53L, 55L, 59L, 53L, 55L, 59L, 53L, 55L, 59L, 47L, 54L, 47L, 54L, 47L, 54L, 47L, 54L, 47L, 54L, 47L, 54L, 47L, 54L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 22L, 27L, 30L, 33L, 45L, 48L, 63L, 64L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 20L, 23L, 46L, 49L, 51L, 52L, 60L, 61L, 62L, 8L, 18L, 28L, 29L, 40L, 50L, 8L, 18L, 28L, 29L, 40L, 50L, 8L, 18L, 28L, 29L, 40, 50L, 8L, 18L, 28L, 29L, 40L, 50L, 8L, 18L, 28L, 29L, 40L, 50L, 8L, 18L, 28L, 29L, 40L, 50L, 8L, 18L, 28L, 29L, 40L, 50L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 11L, 25L, 26L, 31L, 38L, 41L, 42L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 9L, 13L, 14L, 17L, 19L, 21L, 24L, 32L, 36L, 37L, 2L, 4L,6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 2L, 4L, 6L, 7L, 10L, 15L, 16L, 43L, 1L, 3L, 5L, 12L, 35L, 39L, 44L, 1L, 3L, 5L, 12L, 35L, 39L, 44L, 1L, 3L,
5L, 12L, 35L, 39L, 44L, 1L, 3L, 5L, 12L, 35L, 39L, 44L, 1L, 3L, 5L, 12L, 35L, 39L, 44L, 1L, 3L, 5L, 12L, 35L, 39L, 44L, 1L, 3L, 5L, 12L, 35L, 39L, 44L), Division.Name = structure(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 3L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 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2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 4L, 4L, 4L), .Label = c("BARISAL", "CHITTAGONG", "DHAKA", "KHULNA", "RAJSHAHI", "RANGPUR", "SYLHET"
), class = "factor"), Zila.Name = structure(c(50L, 50L, 50L, 50L, 50L, 50L, 50L, 63L, 33L, 47L, 63L, 33L, 47L, 63L, 33L, 47L, 63L, 33L, 47L, 63L, 33L, 47L, 63L, 33L, 47L, 63L, 33L, 47L, 15L, 30L, 56L, 15L, 30L, 56L, 15L, 30L, 56L, 15L, 30L, 56L, 15L, 30L, 56L, 15L, 30L, 56L, 15L, 30L, 56L, 26L, 18L, 26L, 18L, 26L, 18L,
26L, 18L, 26L, 18L, 26L, 18L, 26L, 18L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 22L, 40L, 46L, 59L, 6L, 41L, 61L, 62L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 19L, 29L, 9L, 45L, 54L, 60L, 64L, 21L, 37L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 12L, 36L, 39L, 53L,
25L, 35L, 38L, 12L, 36L, 39L, 53L, 25L, 35L, 38L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 8L, 17L, 27L, 55L, 16L, 20L, 34L, 58L, 11L, 23L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 4L, 24L, 52L, 2L, 10L, 32L, 48L, 42L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L, 3L, 5L, 51L, 13L, 1L, 28L, 57L), .Label = c("BAGERHAT", "BANDARBAN", "BARGUNA", "BARISAL",
"BHOLA", "BOGRA", "BRAHMANBARIA", "CHANDPUR", "CHAPAI NABABGANJ", "CHITTAGONG", "CHUADANGA", "COMILLA", "COX'S BAZAR", "DHAKA", "DINAJPUR", "FARIDPUR", "FENI", "GAIBANDHA", "GAZIPUR", "GOPALGANJ", "HABIGANJ", "JAMALPUR", "JESSORE", "JHALOKATI", "JHENAIDAH", "JOYPURHAT", "KHAGRACHHARI", "KHULNA", "KISHOREGANJ", "KURIGRAM", "KUSHTIA", "LAKSHMIPUR", "LALMONIRHAT", "MADARIPUR", "MAGURA",
"MANIKGANJ", "MAULVIBAZAR", "MEHERPUR", "MUNSHIGANJ", "MYMENSINGH", "NAOGAON", "NARAIL", "NARAYANGANJ", "NARSINGDI", "NATORE", "NETRAKONA", "NILPHAMARI", "NOAKHALI", "PABNA", "PANCHAGARH", "PATUAKHALI", "PIROJPUR", "RAJBARI", "RAJSHAHI", "RANGAMATI", "RANGPUR", "SATKHIRA", "SHARIATPUR", "SHERPUR", "SIRAJGANJ", "SUNAMGANJ", "SYLHET", "TANGAIL", "THAKURGAON"), class = "factor"), name = structure(c(50L, 50L, 50L, 50L, 50L, 50L, 50L, 64L, 33L, 47L, 64L, 33L, 47L, 64L, 33L, 47L, 64L, 33L, 47L, 64L, 33L, 47L, 64L, 33L, 47L, 64L, 33L,
47L, 15L, 30L, 55L, 15L, 30L, 55L, 15L, 30L, 55L, 15L, 30L, 55L, 15L, 30L, 55L, 15L, 30L, 55L, 15L, 30L, 55L, 22L, 18L, 22L, 18L, 22L, 18L, 22L, 18L, 22L, 18L, 22L, 18L, 22L, 18L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 23L, 40L, 46L, 59L, 5L, 41L, 61L, 62L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L,
63L, 21L, 38L, 19L, 29L, 9L, 45L, 54L, 60L, 63L, 21L, 38L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 7L, 14L, 43L, 44L, 31L, 49L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 12L, 36L, 39L, 53L, 26L, 35L, 37L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L,
8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 8L, 17L, 27L, 56L, 16L, 20L, 34L, 58L, 11L, 24L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L,
1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 3L, 25L, 52L, 1L, 10L, 32L, 48L, 42L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L, 6L, 4L, 51L, 13L, 2L, 28L, 57L), .Label = c("Badanbari", "Bagerhat", "Barisal", "Bhola", "Bogra", "Borguna", "Brahmanbaria", "Chandpur", "Chapai", "Chittagong", "Chuadanga", "Comilla", "Cox's Bazar", "Dhaka", "Dinajpur", "Faridpur", "Feni", "Gaibandha", "Gazipur", "Gopalganj", "Habiganj", "Jaipurat", "Jamalpur", "Jessore", "Jhalkati", "Jhenaidah", "Khagrachhari", "Khulna", "Kishoreganj", "Kurigram", "Kushtia", "Laksimipur", "Lamonirhat", "Madanipur", "Magura", "Manikganj", "Meherpur", "Moulvibazar", "Munshiganj", "Mymensingh", "Naogaon", "Narail",
"Nardiaganj", "Narsingdi", "Nator", "Netrokona", "Nilphamar", "Noakhali", "Pabna", "Panchagar", "Patuakhali", "Pirojpur", "Rajbari", "Rajshani", "Rangpur", "Rangramati", "Satkhira", "Shariyapur", "Sherpar", "Sirajganj", "Suramganj", "Sylhet", "Takurgaong", "Tangail"), class = "factor"), row = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), col = c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 2L,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L, 14L, 9L, 8L, 11L, 17L, 10L, 15L, 16L, 14L, 9L, 8L, 11L, 17L, 10L, 15L, 16L, 14L, 9L, 8L, 11L, 17L, 10L, 15L, 16L, 14L, 9L, 8L, 11L, 17L, 10L, 15L, 16L, 14L, 9L, 8L, 11L, 17L, 10L, 15L, 16L, 14L, 9L, 8L, 11L, 17L, 23L, 24L, 18L, 20L, 19L, 21L,22L,25L,26L,23L,24L,18L,20L,19L,21L,22L,25L,26L,23L,24L,
18L,20L,19L,21L,22L,25L,26L,23L,24L,18L,20L,19L,21L, 22L,25L,26L,23L, 24L, 18L, 20L, 19L, 21L, 22L, 25L, 26L, 23L, 24L, 18L, 20L, 19L, 21L, 22L, 25L, 26L, 23L, 24L, 18L, 20L, 19L, 21L, 22L, 25L, 26L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 32L, 29L, 30L, 31L, 27L, 28L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 39L, 37L, 38L, 36L, 34L, 35L, 33L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L, 49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 48L,
49L, 40L, 41L, 45L, 44L, 46L, 47L, 42L, 43L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 54L, 55L, 53L, 51L, 50L, 56L, 57L, 52L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L, 61L, 63L, 62L, 64L, 60L, 59L, 58L), .Label = c(" BG02", " BG03", " BG04", " BG05", " BG06", " BG07", " BG08", " BG10", " BG11", " BG12", " BG16", "BG01", "BG09", "BG13", "BG14", "BG15", "BG17", "BG18", "BG19", "BG20", "BG21", "BG22", "BG23", "BG24", "BG25", "BG26", "BG27", "BG28", "BG29", "BG30", "BG31", "BG32", "BG33", "BG34", "BG35", "BG36", "BG37", "BG38", "BG39", "BG40", "BG41", "BG42", "BG43", "BG44", "BG45", "BG46", "BG47", "BG48", "BG49", "BG50", "BG51", "BG52", "BG53", "BG54", "BG55", "BG56", "BG57", "BG58", "BG59", "BG60", "BG61", "BG62", "BG63", "BG64"), class = "factor"),
type = structure(c(1L, 2L, 3L, 4L, 1L, 3L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 3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0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
    value = c(53.9, 33.2, 10.3, 2.7, 50.3, 12.8, 3.8, 57.2, 59.5, 61.3, 29.1, 29, 26.1, 11, 9.3, 10.1, 2.6, 2.3, 2.5, 50.3,50.3, 50.3, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 50.4, 63.5,     56.7, 33.1, 24.8, 27.4, 13.1, 9.4, 12.3, 3.4, 2.3, 3.6, 50.3,     50.3, 50.3, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 50, 62.9, 34.2,     23.9, 12.7, 10.7, 3.1, 2.5, 50.3, 50.3, 12.8, 12.8, 3.8,     3.8, 67.4, 58.1, 64.2, 66.9, 53.8, 53.7, 68.7, 50.5, 21.3,     29.1, 26.2, 23, 30.1, 33.9, 25, 34.6, 9.1, 10.2, 7.7, 8.3,     13, 10.1, 5.2, 11.7, 2.3, 2.6, 1.9, 1.8, 3.1, 2.3, 1.1, 3.2, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8, 12.8,     12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 35.4, 62.1, 59.7, 54.4, 48.4, 60.9, 55, 61.6, 53.9, 43.3, 28.1, 29.2, 32, 33, 27.2, 30.4, 30.2, 35.6, 17.5, 8.1, 8.7, 10.7, 13.5, 9.7, 11.3, 6.9, 8.7, 3.8, 1.8, 2.3, 2.8, 5.1, 2.2, 3.2, 1.3, 1.8, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8,     12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8,     3.8, 56.8, 28.8, 42.7, 51.7, 57, 56.7, 32.1, 34.8, 39.1,     34.6, 28.6, 29.1, 9.4, 23.3, 14.8, 11.3, 11.5, 11.3, 1.7,     13.1, 3.5, 2.4, 2.9, 2.8, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3,     12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 47.3, 56.1, 49, 56.7, 54.3, 53, 59.8, 36.1, 31.8, 38.4,     31, 33.1, 33.3, 29.9, 13.9, 10, 10.8, 10, 10.1, 11.2, 8.2,     2.7, 2.1, 1.8, 2.4, 2.5, 2.6, 2, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 44.7, 38.8, 59.3, 57, 54.5, 46.4, 56, 58.9, 55, 46.5, 40.1, 41.1, 29.5, 29, 32.8, 40, 32.6, 32.3, 34.3, 36.5, 12.9, 17, 9.3, 11.5, 10, 11, 9.2, 7.3, 8.9, 13.4, 2.3, 3.1, 1.9, 2.5, 2.6, 2.6, 2.1, 1.5, 1.8, 3.5, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8,  12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 39.8, 33.8, 36.6, 70, 38.9, 52.9, 47.6, 49.4, 41.2, 46, 45.9, 20.9, 36.3, 34.8, 38.2, 36.8, 15.2, 17.1, 14.4, 7.6, 19.1, 10.4, 12, 11.3, 3.8, 3.1, 3, 1.5, 5.8, 2, 2.2, 2.5, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8,  12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 43.6, 60.6, 48.1, 62.3, 42.8, 41.1, 51.3, 42.9, 28.6, 37.8, 27.1, 41.4, 36.7, 35.1, 11.1, 8.8, 11.7, 8.6, 12.8, 16.8, 10.9, 2.3, 2.1, 2.4, 2, 3, 5.4, 2.7, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 50.3, 12.8, 12.8, 12.8,    12.8, 12.8, 12.8, 12.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8, 3.8    )), .Names = c("ordinal", "Division.Name", "Zila.Name", "name", "row", "col", "code", "type", "mean", "value"), class = "data.frame", row.names = c(NA, -448L))

3. Plot and save:

library(ggplot2)
p <- ggplot(dt3 , aes(type, value, fill=type))+
geom_col(position = position_dodge())+
scale_fill_manual(values = c("#7fc97f", "#beaed4","#fdc086","#ffff99"))+
themebw()+
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank()),
plot.title=element_text(size=20, face="bold"))+
facet_geo(~name, grid = mygrid) +
labs(title = "Educational attainment in Bangladesh", fill="Edu attainment%",
caption = "By Aledemogr    Source: World Bank"
)
# ggsave("bgd1.png",height = 17, width = 10)</code>

 

bgd1

Geofacet: Nepal 75 districts

Screen Shot 2017-07-20 at 16.07.30row,col,code,name
1,4, NP01, Humla
1,5, NP02, Mugu
1,6, NP03, Dolpa
1,7, NP04, Mustang
1,8, NP05, Manang
2,9, NP06, Gorkha
2,1, NP07, Dharchula
2,2, NP08, Bajhang
2,3, NP09, Bajura
2,4, NP10, Kalikot
2,5, NP11, Jumla
2,6, NP12, Rukum
2,7, NP13, Myagdi
2,8, NP14, Kaski
2,10, NP15, Dhading
2,11, NP16, Rasuwa
2,12, NP17, Sindhupalchowk
2,13, NP18, Dolakha
2,14, NP19, Solukhumbu
2,15, NP20, Sankhuwasabha
2,16, NP21, Taplejung
3,9, NP22, Lamjung
3,1, NP23, Baitadi
3,2, NP24, Doti
3,3, NP25, Achham
3,4, NP26, Dailekh
3,5, NP27, Jajarkot
3,6, NP28, Rolpa
3,7, NP29, Baglung
3,8, NP30, Parbat
3,10, NP31, Nuwakot
3,11, NP32, Kavrepalanchok
3,12, NP33, Kathmandu
3,13, NP34, Okhaldhunga
3,14, NP35, Khotang
3,15, NP36, Bhojpur
3,16, NP37, Dhankuta
3,17, NP38, Tehrathum
4,9, NP39, Tanahun
4,1, NP40, Kanchanpur
4,2, NP41, Dadeldhura
4,3, NP42, Kailali
4,4, NP43, Surkhet
4,5, NP44, Salyan
4,6, NP45, Pyuthan
4,7, NP46, Gulmi
4,8, NP47, Syangja
4,10, NP48, Chitwan
4,11, NP49, Patan
4,12, NP50, Bhaktapur
4,13, NP51, Ramechhap
4,14, NP52, Udayapur
4,15, NP53, Sunsari
4,16, NP54, Panchthar
4,17, NP55, Ilam
5,9, NP56, Nawalparasi
5,4, NP57, Bardiya
5,5, NP58, Banke
5,6, NP59, Dang
5,7, NP60, Argakhanchi
5,8, NP61, Palpa
5,10, NP62, Parsa
5,11, NP63, Makwanpur
5,12, NP64, Sindhuli
5,13, NP65, Dhanussa
5,14, NP66, Siraha
5,15, NP67, Saptari
5,16, NP68, Morang
5,17, NP69, Jhapa
6,7, NP70, Kapilvastu
6,8, NP71, Rupandehi
6,10, NP72, Bara
6,11, NP73, Rahuttahat
6,12, NP74, Sarlahi
6,13, NP75, Mahottari

Geofacet grids: Nigeria Federal States

Geofacet grid for Nigeria’s 37 Federal States (below):

Screen Shot 2017-07-20 at 12.31.24.png

row,col,code,name
1,4,NG.KT,Katsina
1,5, NG.KN, Kano
1,2,NG.SO,Sokoto
1,3, NG.ZA, Zamfara
1,6, NG.JI, Jigawa
1,7, NG.YO, Yobe
2,2, NG.KE, Kebbi
2,3, NG.NI, Niger
2,4, NG.KD, Kaduna
2,7, NG.BO, Borno
2,6, NG.GO, Gombe
2,5, NG.BA, Bauchi
3,1, NG.OY, Oyo
3,2, NG.KW, Kwara
3,3,NG.FC, Abuja FCT
3,4, NG.NA, Nassarawa
3,6, NG.AD, Adamawa
3,5, NG.PL, Plateau
4,3, NG.EK, Ekiti
4,1, NG.OG, Ogun
4,2, NG.OS, Osun
4,4, NG.KO, Kogi
4,6, NG.TA, Taraba
4,5, NG.BE, Benue
5,3,NG.ED, Edo
5,1, NG.LA, Lagos
5,2, NG.ON, Ondo
5,6,NG.EB, Ebonyi
5,4, NG.AN, Anambra
5,5, NG.EN, Enugu
6,2, NG.DE, Delta
6,3, NG.IM, Imo
6,4,NG.AB, Abia
6,5, NG.CR, Cross River
7,3, NG.BY, Bayelsa
7,4, NG.RI, Rivers
7,5, NG.AK, Akwa Ibom

A nice example of hafen/geofacet from Washington Post to ggplot2

I’ve recently came across the hafen/geofacet function and was pondering to blog an example. Then, I came across a perfect example, thanks to  kanishkamisra for working on the dataset & code and making it available via github here!

 

usa_vs_state1

BAR CHART: a ggplot balance plot (2)

Merchandise trade balance plot in ggplot2

BAR CHART+LINE

Graph 2: Merchandise trade balance

You can find the data for this plot here or alternatively here is the dput data for balance:

structure(list(variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Merchandize Trade Balance", class = "factor"),
type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L), .Label = "Balance", class = "factor"), year = c(2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L), value = c(-0.5, -1.5, -0.1, -0.4, -0.2,
0, 0.1, -0.1, -0.6, -0.2, -0.2, -1.3, 0), geo = structure(c(2L,
4L, 7L, 9L, 1L, 6L, 12L, 5L, 3L, 11L, 10L, 13L, 8L), .Label = c("CIS",
"Dev. Asia Pacific", "Eastern Asia", "Europe", "Latin Am. And Carr.",
"North Africa", "North America", "Oceania", "South Eastern Europe",
"South-Eastern Asia", "Southern Asia", "Sub-Saharan Africa",
"Western Asia"), class = "factor")), .Names = c("variable",
"type", "year", "value", "geo"), class = "data.frame", row.names = c(NA,
-13L))
library(dplyr) #to manipulate the dataset
library(ggplot2) #plotting
mer.bal <- mydt %>%
filter(variable == "Merchandize Trade Balance")

base <- mer.bal %>%
filter(type != "Balance") %>%
mutate(
value = ifelse(type == "Exports", value, -value)
)
balance <- mer.bal %>%
filter(type == "Balance")

ggplot(balance, aes(x = geo, y = value, fill=factor(type))) +
geom_bar(data = base %>%
filter(type=="Exports"), aes(col=type), stat = "identity") +
geom_bar(data = base %>%
filter(type=="Imports"), aes(col=type), stat = "identity") +
geom_bar(data = balance, aes(col=type), stat = "identity", width=.2) +
ggtitle(expression(atop("Merchandise trade balance", atop(italic("(Bln US$ by MDG Regions in 2013)"), "")))) +
theme_bw()+
theme(axis.text.x = element_text(size=8, color="black"),
axis.text.y = element_text(size=8, color="black"),
legend.text=element_text(size=10),
plot.title = element_text(size = 20, face = "bold", colour = "black", vjust = -1))+
scale_fill_manual(values = c(Exports = "#0072B2", Imports = "#56B4E9", Balance="red"), name="") +
scale_colour_manual(values = c(Exports = "#0072B2", Imports = "#56B4E9", Balance="red"), name="") +
coord_flip()+
labs(x = "", y = "")

graph3

BAR CHART + LINE: a ggplot balance plot (1)

You can download session 9 files here (R-Ladies Tbilisi) and specify your working directory with setwd(“/Users/mydomain/myforlder/)

BAR CHART + LINE:

###Graph 1: Total services trade, by value

 require(ggplot2)
require(dplyr)
mypath <- "/Users/StayPuftMarshmallowMan/Shandor Folder/"
setwd(paste(mypath))
mydt <- read.csv("Georgia_Data_UN.csv", header=T)

head(mydt)
##                                            variable     type year   value
## 1 GDP: Gross domestic product (million current US$) economic 2014 16530.0
## 2 GDP: Gross domestic product (million current US$) economic 2010 11638.0
## 3 GDP: Gross domestic product (million current US$) economic 2005  6411.0
## 4    GDP growth rate (annual %, const. 2005 prices) economic 2014     4.8
## 5    GDP growth rate (annual %, const. 2005 prices) economic 2010     6.2
## 6    GDP growth rate (annual %, const. 2005 prices) economic 2005     9.6
##   geo
## 1
## 2
## 3
## 4
## 5
## 6
levels(mydt$variable)
##  [1] "Agricultural production index (2004-2006=100)"
##  [2] "Balance (million US$)"
##  [3] "Balance of payments, current account (million US$)"
##  [4] "CO2 emission estimates (tons per capita)"
##  [5] "CPI: Consumer price index (2000=100)"
##  [6] "Economy: Agriculture (% of GVA)"
##  [7] "Economy: Industry (% of GVA)"
##  [8] "Economy: Services and other activity (% of GVA)"
##  [9] "Education: Government expenditure (% of GDP)"
## [10] "Education: Tertiary gross enrolment ratio (f-m per 100 pop.)"
[...]
## [48] "Unemployment (% of labour force)"
## [49] "Urban population (%)"
## [50] "Urban population growth rate (average annual %)"
ser.dt <- mydt %>%
filter(variable=="Total Services Trade")

Balance <- ser.dt%>%
group_by(year)%>%
summarise(value=-diff(value))

Balance <- cbind(variable=c(rep("Total Services Trade", 13)),
type= c(rep("Balance", 13)), Balance, geo=c(rep("NA", 13)))

mydata <- rbind(ser.dt, Balance)

subset with the pipe operator %>%

base <- mydata %>%
filter(type != "Balance") %>%
mutate(
value = ifelse(type == "Exports", value, -value)
)
balance <- mydata %>%
filter(type == "Balance")

ggplot(balance, aes(x = year, y = value)) +
geom_bar(data = base, aes(fill = type), stat = "identity") +
geom_point(aes(colour = type)) +
geom_line(aes(colour = type, group=1)) +
scale_fill_manual(values = c(Exports = "#D55E00", Imports = "#E69F00"), name="") +
scale_colour_manual(values = c(Balance = "#660000"), name="") +
labs(x = "", y = "Total Services Trade")+
theme_bw()

Presentation1