A Few Macroeconomic Graphs in R
A collection of some graphs I created for a macroeconomic class project focused on 1995-2005.Included below is the code used for the creation of each graph. They’re all pretty similar and standard aside from a few tweaks here and there to highlight different factors.
All data is obtained either from FRED, the BEA, or World Bank.
Step one: import the libraries and data.
windowsFonts(Times=windowsFont("TT Times New Roman"))
library(ggplot2)
library(scales)
library(gridExtra)
gdp <- read.csv('GDPCA.csv')
unemp <- read.csv('unemployment.csv')
inflation <- read.csv('inflation.csv')
interest <- read.csv('DFF.csv')
budget <- read.csv('budget.csv')
receipt <- read.csv('receipts.csv')
mort <- read.csv('mort.csv')
imex <- read.csv('import_export.csv')
deficit <- read.csv('deficit.csv')
First graph up is GDP.
###GDP
ggplot(aes(x = DATE, y = GDPCA), data = gdp) +
geom_bar(stat = 'identity', width = .75) +
labs( title = '1995-2005 Real Gross Domestic Product',
subtitle = 'Annual Growth Rates in Blue') +
ylab('GDP (in Billions)') +
scale_y_continuous(label=comma) +
xlab('Year') +
scale_x_continuous(breaks=c(1995:2005), labels=c(1995:2005)) +
geom_text(label = comma(sprintf("%0.0f", gdp$GDPCA)),
size = 4.7, vjust = 11.4, color = 'white') +
geom_text(label = gdp$GROWTH,
size = 4.7, vjust = -0.3, color = 'steelblue') +
theme(axis.title = element_text(family = "Times", color="Black",
face="bold", size=22)) +
theme(plot.title = element_text(family = "Times", color="black", size=32, hjust=0)) +
theme(plot.subtitle=element_text(size=22, face="italic", color="steelblue"))
Next is a grid stack of unemployment and inflation:
###Unemployment and Inflation
p1 <- ggplot(aes(x = Year, y = Avg), data = unemp) +
geom_bar(stat = 'identity', width = .75, fill = 'steelblue') +
labs( title = 'Unemployment - Annual Rates') +
ylab('Rate') +
scale_y_continuous(label=comma) +
xlab('Year') +
scale_x_continuous(breaks=c(1995:2005), labels=c(1995:2005)) +
geom_text(label = unemp$Avg,
size = 4.7, vjust = 1.25, color = 'white') +
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18, hjust=0))
p2 <-ggplot(aes(x = Year, y = avg), data = inflation) +
geom_bar(stat = 'identity', width = .75, fill = 'steelblue') +
labs( title = 'Inflation - Annual Rates') +
ylab('Rate') +
scale_y_continuous(label=comma) +
xlab('Year') +
scale_x_continuous(breaks=c(1995:2005), labels=c(1995:2005)) +
geom_text(label = inflation$avg,
size = 4.7, vjust = 1.25, color = 'white') +
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18, hjust=0))
grid.arrange(p1, p2, nrow = 2)
A single line chart of Federal Fund Rates next:
###Interest Fed Reserve
ggplot(aes(x = time, y = DFF, group = 1), data = interest) +
geom_line() +
labs( title = 'Federal Reserve Rates (1995-2005)') +
ylab('Rate')+
xlab('Year') +
scale_x_continuous(breaks=seq(0,3654,365), labels=c(1995:2005)) +
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
Following are two simple line charts grid together of national budget balance and federal reciepts:
###Budget
g1 <- ggplot(aes(x = year, y = budget, group = 1), data = budget) +
geom_line(size = 1.45, color = 'steelblue') +
geom_line(y = 0, size = .85) +
labs( title = 'U.S. Budget (1995-2005)') +
ylab('Balance (in Billions) ')+
xlab('Year') +
scale_x_continuous(limits = c(1995,2005),
breaks=c(1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005))+
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
###receipts
g2 <- ggplot(aes(x = DATE, y = FYFRGDA188S, group = 1), data = receipt) +
geom_line(size = 1.45, color = 'steelblue') +
geom_line(y = 0, size = .85) +
labs( title = 'Federal Receipts as Percent of GDP (1995-2005)') +
ylab('PErcent of GDP')+
xlab('Year') +
scale_x_continuous(limits = c(1995,2005),
breaks=c(1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005))+
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
grid.arrange(g1, g2, nrow = 2)
## Warning: Removed 5 rows containing missing values (geom_path).
## Warning: Removed 5 rows containing missing values (geom_path).
Another one here of growth rates.. Didn’t end up using this one in final.
###growth
ggplot(aes(x = DATE, y = GROWTH, group = 1), data = gdp) +
geom_line(size = 1.45, color = 'steelblue') +
geom_line(y = 0, size = .85) +
labs( title = 'Real GDP Growth Rates (1995-2005)') +
ylab('Growth Rate')+
expand_limits(y = 0, x = 1995) +
xlab('Year') +
scale_x_continuous(breaks=c(1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005))
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
## List of 2
## $ axis.title:List of 11
## ..$ family : chr "Times"
## ..$ face : NULL
## ..$ colour : chr "#666666"
## ..$ size : num 16
## ..$ hjust : NULL
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi FALSE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## $ plot.title:List of 11
## ..$ family : chr "Times"
## ..$ face : chr "bold"
## ..$ colour : chr "Black"
## ..$ size : num 18
## ..$ hjust : num 0
## ..$ vjust : NULL
## ..$ angle : NULL
## ..$ lineheight : NULL
## ..$ margin : NULL
## ..$ debug : NULL
## ..$ inherit.blank: logi FALSE
## ..- attr(*, "class")= chr [1:2] "element_text" "element"
## - attr(*, "class")= chr [1:2] "theme" "gg"
## - attr(*, "complete")= logi FALSE
## - attr(*, "validate")= logi TRUE
Can’t talk about this time period without discussing the lead up to the juouising crisis!
###MOrtage rates
ggplot(aes(x = DATE, y = MORTGAGE30US, group = 1), data = mort) +
geom_line(size=1.4, color='steelblue') +
labs( title = '30-Year Fixed Mortgage Rates (1995-2005)') +
ylab('Rate')+
xlab('Year') +
scale_x_continuous(breaks=seq(0,574,57.4), labels=c(1995:2005)) +
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
Lastly, Foriegn Trade plots.
#import and exports
f1 <-ggplot(aes(x = DATE), data = imex) +
geom_line(aes(y = exports, color = 'exports'), size = 1.25) +
geom_line(aes(y = imports, color = 'imports'), size = 1.25) +
labs( title = 'Imports and Exports (1995 - 2005)') +
ylab('Annual Amount (In Billions)')+
xlab('Year') +
scale_x_continuous(breaks=seq(0,42,4.2), labels=c(1995:2005))+
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
#deficit
f2 <- ggplot(aes(x = DATE), data = deficit) +
geom_line(aes(y = deficit/100), size = 1.25, color = 'dark red') +
labs( title = 'Trade Balance (1995 - 2005)') +
ylab('Balance (In Billions)')+
xlab('Year') +
scale_x_continuous(breaks=seq(0,121,12.1), labels=c(1995:2005))+
theme(axis.title = element_text(family = "Times", color="#666666", size=16)) +
theme(plot.title = element_text(family = "Times", color="Black", size=18,
face = 'bold', hjust=0))
grid.arrange(f1, f2, nrow = 2)