The following exercise delves into the question about the similarities or differences among a country´s economy and its stock market.
There are many ways to compare the stock market and a country´s economy. Herein the analysis is focused only on the contribution to the country´s GDP of the companies that make up a country index (stock market index).
Most of the data has been taken from Wikipedia, and it has been acquired through the following procedure:
The objective is to show a graph as simple and intuitive as possible while highlighting the expected disparity among indexed companies and the rest of the economy. In order to accomplish that, the graphs shows the following characteristics:
head(df)
## country GDP index population revenue employees workforce
## 1 EEUU 22675 S&P500 331449281 13221.6703 26311916 160400000
## 2 France 2938 CAC40 67427000 1393.9960 3837774 30680000
## 3 Canada 1883 TSX60 38321031 1005.3308 1872312 19520000
## 4 Italy 2106 MIB40 59169131 432.8692 1085519 25940000
## 5 Europe 15167 STOXX50 447706209 2670.4340 6870799 216229439
## 6 Spain 1461 IBEX35 47394223 457.8719 1559585 22750000
library(ggplot2)
library(tidyr)
df$country<- as.factor(df$country)
df$TotalGDP<-df$GDP
df$index<- as.factor(df$index)
df$GDP<-df$GDP-df$revenue
df<-df[order(df$TotalGDP, decreasing= FALSE),]
dl<-gather(df, variable, value, c("GDP","revenue"))
dl$variable<- as.factor(dl$variable)
g <- ggplot(dl, aes(x=factor(country, levels =df$country), y=value, fill= variable)) +geom_col()
gF<-g +
labs(x="Countries", y ="Nominal GDP ($ billions)", fill="GDP breakdown") +
ggtitle("Nominal GDP ($ billions)") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank()) +
scale_fill_discrete(labels=c("Non-index","Index")) +
coord_flip()
df$TotalWorkforce<-df$workforce
df$workforce<-df$TotalWorkforce-df$employees
dp<-gather(df, variable, value, c("employees","workforce"))
dp$value <- dp$value/1000000
p <-ggplot(dp, aes(x=factor(country, levels =df$country), y=value, fill= factor(variable, levels = c("workforce","employees")))) +
geom_col()
pF<-p +
labs(x="Countries", y ="Workforce (Millions)", fill="Workforce \nbreakdown") +
scale_fill_manual(labels=c("Non-index","Index"), values =c("#F8766D","#00BFC4")) +
ggtitle("Workforce (Millions)") +
theme(axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
legend.position = "left") +
# plot.margin = unit(c(1,-1,1,0), "mm")) +
scale_y_reverse() +
coord_flip()
library(gridExtra)
g.mid<-ggplot(df,aes(x=1,y=factor(country, levels =df$country)))+geom_text(aes(label=country))+
geom_segment(aes(x=0.7,xend=0.78,yend=country))+
geom_segment(aes(x=1.3,xend=1.38,yend=country))+
ggtitle("")+
ylab(NULL)+
scale_x_continuous(expand=c(0,0),limits=c(0.7,1.38))+
theme(axis.title=element_blank(),
panel.grid=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
panel.background=element_blank(),
axis.text.x=element_text(color=NA),
axis.ticks.x=element_line(color=NA))
# plot.margin = unit(c(1,-1,1,-1), "mm"))
grid.arrange(pF,g.mid,gF,ncol=3,widths=c(4/9,1/9,4/9))