Download financial data from FRED
library(fredr)
#fredr_set_key("insert_your_api_key")
#(You can get it here: https://research.stlouisfed.org/docs/api/api_key.html)
fred_data<-fredr(series_id = "WALCL",observation_start = as.Date("2003-01-01"))
head(fred_data)
## # A tibble: 6 x 3
## date series_id value
## <date> <chr> <dbl>
## 1 2003-01-01 WALCL 730994
## 2 2003-01-08 WALCL 723762
## 3 2003-01-15 WALCL 720074
## 4 2003-01-22 WALCL 735953
## 5 2003-01-29 WALCL 712809
## 6 2003-02-05 WALCL 719643
#Other recommendable APIs:
#https://cran.r-project.org/web/packages/Rblpapi/Rblpapi.pdf
#https://cran.r-project.org/web/packages/bea.R/bea.R.pdf
#https://cran.r-project.org/web/packages/OECD/OECD.pdf
#https://cran.r-project.org/web/packages/Quandl/Quandl.pdf
Generate a nice default layout for the plot
theme_am<-
function (base_size = 12, base_family = "")
{
library(ggthemes)
library(scales)
theme_hc(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.text.x = element_text(color = "grey20", size = 11,family="Calibri Light"),
axis.text.y = element_text(color = "grey20", size = 11,family="Calibri Light"),
axis.title.x = element_text(color = "grey20", size = 12,family="Calibri Light"),
axis.title.y = element_text(color = "grey20", size = 12,family="Calibri Light"),
plot.title = element_text(color="#04103b", size=13, face="bold",family="Calibri Light"),
legend.text = element_text(color = "grey20", size = 12,family="Calibri Light")
)
}
Generate and format the plot
library(extrafont)
## Registering fonts with R
library(ggplot2)
scaleFactor <- max(fred_data$value/1000) / max((fred_data$value/fred_data$value[1]))
my_plot<-
ggplot(fred_data, aes(x=date)) +
geom_line(aes(y=value/1000),size = 0.8,color="#dd0400") +
geom_line(aes(y=(value/value[1]) * scaleFactor),size = 0.8,color="#dd0400") +
scale_y_continuous(name="Total Assets in bn $", sec.axis=sec_axis(~./scaleFactor, name="Indexed in %",labels = scales::percent_format(accuracy = 1))) +
theme_am()+
scale_x_date(labels = date_format("%m-%Y"))+
theme(plot.title = element_text(color="#04103b", size=13, face="bold",family="Calibri Light"))+
labs(title="Federal Reserve Banks Total Assets",x ="")
my_plot
Export editable plot to powerpoint
library(officer)
library(rvg)
#https://davidgohel.github.io/officer/articles/offcran/powerpoint.html
#1) Convert your chart
editable_graph <- dml(ggobj = my_plot)
#2) Create a power point
doc <- read_pptx()
#If you want to use an existing presentation
#doc <- read_pptx(target_ppt)
#3) Add a new slide
doc <- add_slide(doc, layout = "Title and Content", master = "Office Theme")
#4) Add your plot
doc <- ph_with(x = doc, editable_graph, location = ph_location_type(type = "body"))
#Sometimes with custom slide layouts this doesn't work - default to fullsize if there are issues
#doc <- ph_with(x = doc, editable_graph, location = ph_location_fullsize())
#5) Save the new presentation
print(doc, target = "officer_ppt.pptx")