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 radar charts - with ggplot2 + ggradar and fsmb. Examples are presented on Innovation Input Sub-index from Global Innovation Index.
Radar chart version 1 created with ggplot2 and ggradar packages (click to enlarge). Jump to code with ggplot2 + ggradar package.
Radar chart version 2 with fmsb package (click to enlarge). Jump to code with fmsb package.
With ggplot2 + ggradar
library(MacrobondAPI)
library(tidyverse)
library(countrycode)
library(ggradar)
#First column is the name of the groups and each column represents a variable.
#All columns must have the same scale.
#If the values of your columns are not between 0 and 1 you will need to specify the minimum value with grid.min and the maximum value with grid.max.
#create list of series
list_of_series<-c("gii0828", "gii0974", "gii1266", "gii0682", "gii1120", #Nigeria
"gii0849", "gii0995", "gii1287", "gii0703", "gii1141", #South Africa
"gii0773", "gii0919", "gii1211", "gii0627", "gii1065") #Ethiopia
#create empty vectors
values<-NULL
region<-NULL
#get region
for (i in list_of_series){
region<-append(region, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "Region"))
}
#change region ISO codes to names
region<-countrycode(as.vector(region), origin="iso2c", destination ="country.name")
#set description
description_labels<-c("Human Capital & Research", "Infrastructure", "Business \nSophistication",
"Institutions", "Market \nSophistication")
descriptions<-rep(description_labels, 3) #create this for data frame
#get values
for (i in list_of_series){
values<-append(values, last(getValues(FetchOneTimeSeries(i))))
}
#prepare data frame
data <- data.frame(region, descriptions, values)
#change format of data from long to wide
data <- spread(data, descriptions, values)
#set colors
colors <- c("#58A291", "#F68D2E", "#CD545B")
#plot
ggradar(data,
values.radar = c(0, 40, 80), #0%, 50%, 100% of values
axis.labels = c(description_labels), #description of each 'arm'
grid.min=0,grid.mid=40, grid.max=80, #grid labels
background.circle.colour = "white", #background settings
axis.line.colour = "grey", #axis line's settings
axis.label.size=4,
gridline.min.colour = "grey", #gridlines' settings
gridline.mid.colour = "grey",
gridline.max.colour = "grey",
grid.label.size=5,
group.colours = colors, #chart's lines settings
group.line.width= 1.2,
group.point.size=5,
legend.title="Global Innovation Index", #legend's settings
legend.position = "top",
legend.text.size = 10)
With fmsb
library(MacrobondAPI)
library(tidyverse)
library(countrycode)
library(fmsb)
#create list of series
list_of_series<-c("gii0828", "gii0974", "gii1266", "gii0682", "gii1120", #Nigeria
"gii0849", "gii0995", "gii1287", "gii0703", "gii1141", #South Africa
"gii0773", "gii0919", "gii1211", "gii0627", "gii1065") #Ethiopia
#create empty vectors
values<-NULL
region<-NULL
#get region
for (i in list_of_series){
region<-append(region, getMetadataValues(getMetadata(FetchOneTimeSeries(i)), "Region"))
}
#change ISO codes to names
region<-countrycode(as.vector(region), origin="iso2c", destination ="country.name")
#set description
description_labels<-c("Human Capital & Research", "Infrastructure", "Business \nSophistication",
"Institutions", "Market \nSophistication")
descriptions<-rep(description_labels, 3) #create this for data frame
#get values
for (i in list_of_series){
values<-append(values, last(getValues(FetchOneTimeSeries(i))))
}
#Data frame must have first row representing the maximum values of the data and the second row the minimum values.
#prepare data frame
data <- data.frame(region, descriptions, values)
#prepare data frame for max and min values
max_min <- as.data.frame(rbind(rep(80, 5), rep(0, 5)))
rownames(max_min) <- c("max", "min")
colnames(max_min) <- description_labels
#change format of data from long to wide
data <- spread(data, descriptions, values) %>%
column_to_rownames(var="region") #move first column to row names
data <- rbind(max_min, data) #join two data frames
#set colors
colors <- c("#58A291", "#F68D2E", "#CD545B")
#plot (mark radar chart and legend to plot them together)
radarchart(data,
axistype=1, #gridlines' settings
cglcol = "gray",
axislabcol="grey",
caxislabels=c(0,20, 40, 60, 80),
pcol = colors, #chart's lines settings
plty = 1,
plwd = 2,
vlcex=0.8, #size of value labels
title = "Global Innovation Index")
legend(x=1.3, y=1, #legend's position
legend = rownames(data[-c(1,2),]), #exclude names for max and min
bty="n", #border around legend, n=none
pt.cex=1.8, #width of sample colors
pch = 20,
col = colors)