# A new R package darklyplot

## I try to make a new R package

Today I try my hand at building an R package called darklyplot. This package is a little extension of ggplot2 to create a dark themed time series plot.

This packages lets you create simple dark-theme times series plots. It extends ggplot2 and relies on mdthemes to make color coded axis labels. The axis labels use ggthemes::geom_rangeframe to create Tufte-like axes. Through parameters, the user can alter the colors, include shading under the line, and also add a single reference line.

I’ve tweeted out a few examples:

It’s all very much under development, but I posted a very rough version to github. You can install it with:

library(devtools)
install_github("lenkiefer/darklyplot")
# requires: dplyr, glue, ggplot2, ggthemes, mdthemes, scales, shiny

There’s really only one key function darklyplot, along with some helpers and some data I loaded in so you have something to try.

library(darklyplot)
?darklyplot

You can test the function out. It takes in a data frame (must have a date column named date), and outputs a ggplot2 plot. With ggplot2, you can add labels.

darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3)+
labs(caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey",
title="U.S. Weekly Average 30-year Fixed Mortgage Rate (%)")

I added some little embellishments. You can shade under the plot:

darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3,shade=TRUE,refline=TRUE)+
labs(caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey",
title="U.S. Weekly Average 30-year Fixed Mortgage Rate (%)")

And you can change the color of the markers.

darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3,
minCol="blue",maxCol="red",firstCol="orange",lastCol="white"
)+
labs(caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey",
title="U.S. Weekly Average 30-year Fixed Mortgage Rate (%)")

There’s a lot of work to do. But it does give you the ability to make a stylish dark chart with any time series data. Take for example today’s jobs report:

# Since I'm using mdthemes I need to use <br> instead of \n to break line
tidyquant::tq_get("UNRATE",get="economic.data",from="2000-01-01") %>%
darklyplot("price",n.decimals=1)+
labs(title="U.S. unemployment rate (%)",
caption=paste0("@lenkiefer Source: U.S. Bureau of Labor Statistics, Unemployment Rate [UNRATE],",
"<br> retrieved from FRED, Federal Reserve Bank of St. Louis;<br>",
"July 2, 2020."))