Treemap

Macrobond R API is available for users with Data+ and Legacy license (the latter without searching and revision history functions). Examples were tested on Macrobond version 1.27, Macrobond API for R version 1.2-8 and R 4.3.0.

See two methods of constructing treemaps - with packages treemap and ggplot2 + treemapify. Examples are presented on Germany's export data from Federal Statistical Office (Statistisches Bundesamt).

 

 

 

 

Treemap version 1 and version 2 - both created with treemap package (click to enlarge). Jump to code with treemap package.

 

 

 

 

 

 

 

Treemap version 3 and version 4 with ggplot2 + treemapify packages (click to enlarge). Jump to code with ggplot2 + treemapify packages.

With treemap package

library(MacrobondAPI)
library(countrycode)
library(treemap)

#create list of series from Macrobond database
list_of_series<-c("detrad4162","detrad4163","detrad4164","detrad4165","detrad4166","detrad4167","detrad4168","detrad4169","detrad4170","detrad4171","detrad4172","detrad4173","detrad4174","detrad4175","detrad4176","detrad4177","detrad4178","detrad4179","detrad4180","detrad4181","detrad4182","detrad4183","detrad4184","detrad4185","detrad4186","detrad4187","detrad4188","detrad4189","detrad4190","detrad4191","detrad4192","detrad4193","detrad4194","detrad4195","detrad4196","detrad4197","detrad4198","detrad4199","detrad4200","detrad4201","detrad4202","detrad4203","detrad4204","detrad4205","detrad4206","detrad4207","detrad4208","detrad4209","detrad4210","detrad4211","detrad4212","detrad4213","detrad4214","detrad4215","detrad4217","detrad4218","detrad4219","detrad4220","detrad4221","detrad4222","detrad4223","detrad4224","detrad4225","detrad4226","detrad4227","detrad4228","detrad4229")

#create empty vectors
value <-NULL
group<-NULL

#get last values for each series
for (i in list_of_series){
value<-append(value, last(getValues(FetchOneTimeSeries(i))))
}

#get export countries ISO codes for each series based on TradeCounterpart metadata attribute
for (i in list_of_series){
group<-append(group, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "TradeCounterpart"))
}

#change ISO codes to names
group<-countrycode(as.vector(group), origin="iso2c", destination ="country.name")

#get continent for each country
continent<-countrycode(sourcevar = group, origin = "country.name",destination = "continent")

#create data
data <- data.frame(group, value)
data2 <- data.frame(continent, group, value)

#treemap version 1
treemap(data,
index="group",
vSize="value",
type="index",
palette = "Set3",
align.labels = c("left", "top"),
fontsize.labels=10,
fontcolor.labels="black",
title="Export of cars from Germany to... (in EUR)",
fontsize.title=18)

#treemap version 2 with by continent grouping
treemap(data2,
index=c("continent", "group"),
vSize="value",
type="index",
palette = "Set1",
fontsize.labels=c(0,10),
fontcolor.labels="white",
align.labels = c("left", "top"),
title="Export of cars from Germany to... (in EUR)",
fontsize.title=18)

With ggplot2 + treemapify packages

library(MacrobondAPI)
library(countrycode)
library(treemapify)
library(ggplot2)

#create list of series from Macrobond database
list_of_series<-c("detrad4162","detrad4163","detrad4164","detrad4165","detrad4166","detrad4167","detrad4168","detrad4169","detrad4170","detrad4171","detrad4172","detrad4173","detrad4174","detrad4175","detrad4176","detrad4177","detrad4178","detrad4179","detrad4180","detrad4181","detrad4182","detrad4183","detrad4184","detrad4185","detrad4186","detrad4187","detrad4188","detrad4189","detrad4190","detrad4191","detrad4192","detrad4193","detrad4194","detrad4195","detrad4196","detrad4197","detrad4198","detrad4199","detrad4200","detrad4201","detrad4202","detrad4203","detrad4204","detrad4205","detrad4206","detrad4207","detrad4208","detrad4209","detrad4210","detrad4211","detrad4212","detrad4213","detrad4214","detrad4215","detrad4217","detrad4218","detrad4219","detrad4220","detrad4221","detrad4222","detrad4223","detrad4224","detrad4225","detrad4226","detrad4227","detrad4228","detrad4229")

#create empty vectors
value <-NULL
group<-NULL

#get last values
for (i in list_of_series){
value<-append(value, last(getValues(FetchOneTimeSeries(i))))
}

#get export countries ISO codes for each series based on TradeCounterpart metadata attribute
for (i in list_of_series){
group<-append(group, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "TradeCounterpart"))
}

#change ISO codes to names
group<-countrycode(as.vector(group), origin="iso2c", destination ="country.name")

#get continent for each country
continent<-countrycode(sourcevar = group, origin = "country.name",destination = "continent")

#create data
data <- data.frame(group, value)
data2 <- data.frame(continent, group, value)

#treemap version 3 with legend
chart<-ggplot(data2, aes(area = value, fill = continent, label = group)) +
geom_treemap() +
geom_treemap_text(colour = "black", place = "centre", size = 12)+
ggtitle("Export of cars from Germany to... (in EUR)")+
theme(plot.title=element_text(size=20, face="bold"))

#color palette
chart+scale_fill_brewer(palette="PuBu")

#treemap version 4 with by continent grouping
chart<-ggplot(data2, aes(area = value, fill = continent, label = group, subgroup=continent)) +
geom_treemap() +
geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.25, colour = "black") +
geom_treemap_text(colour = "white", place = "centre", size = 12)+
ggtitle("Export of cars from Germany to... (in EUR)")+
theme(legend.position = "none", plot.title=element_text(size=20, face="bold"))

#MB color palette
conts<-c("Europe"="#256BA2", "Africa"="#F68D2E", "Oceania"="#CD545B", "Americas"="#8E51A8", "Asia"="#58A291")
chart+scale_fill_manual(values = conts)+
geom_treemap_subgroup_border(colour = "white", size = 3)