├── vignettes
├── .gitignore
└── using-darklyplot.Rmd
├── .Rbuildignore
├── .gitignore
├── R
├── mtg_rate.RData
├── darkly_format.R
├── mtg_rate.R
├── theme_dark2.R
└── darklyplot.R
├── data
└── mtg_rate.RData
├── man
├── figures
│ ├── README-example-1.png
│ ├── README-example2-1.png
│ ├── README-pressure-1.png
│ └── README-example-3-1.png
├── theme_dark2.Rd
├── darkly_format.Rd
├── mtg_rate.Rd
└── darklyplot.Rd
├── NAMESPACE
├── darklyplot.Rproj
├── DESCRIPTION
├── README.md
├── README.Rmd
└── doc
├── using-darklyplot.R
├── using-darklyplot.Rmd
└── using-darklyplot.html
/vignettes/.gitignore:
--------------------------------------------------------------------------------
1 | *.R
2 |
--------------------------------------------------------------------------------
/.Rbuildignore:
--------------------------------------------------------------------------------
1 | ^.*\.Rproj$
2 | ^\.Rproj\.user$
3 | ^README\.Rmd$
4 | ^Meta$
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .Rproj.user
2 | .Rhistory
3 | .RData
4 | .Ruserdata
5 | Meta
6 |
--------------------------------------------------------------------------------
/R/mtg_rate.RData:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/R/mtg_rate.RData
--------------------------------------------------------------------------------
/data/mtg_rate.RData:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/data/mtg_rate.RData
--------------------------------------------------------------------------------
/man/figures/README-example-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/man/figures/README-example-1.png
--------------------------------------------------------------------------------
/man/figures/README-example2-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/man/figures/README-example2-1.png
--------------------------------------------------------------------------------
/man/figures/README-pressure-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/man/figures/README-pressure-1.png
--------------------------------------------------------------------------------
/man/figures/README-example-3-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lenkiefer/darklyplot/HEAD/man/figures/README-example-3-1.png
--------------------------------------------------------------------------------
/NAMESPACE:
--------------------------------------------------------------------------------
1 | # Generated by roxygen2: do not edit by hand
2 |
3 | export(darkly_format)
4 | export(darklyplot)
5 | export(theme_dark2)
6 | import(ggthemes)
7 | import(mdthemes)
8 |
--------------------------------------------------------------------------------
/darklyplot.Rproj:
--------------------------------------------------------------------------------
1 | Version: 1.0
2 |
3 | RestoreWorkspace: Default
4 | SaveWorkspace: Default
5 | AlwaysSaveHistory: Default
6 |
7 | EnableCodeIndexing: Yes
8 | UseSpacesForTab: Yes
9 | NumSpacesForTab: 2
10 | Encoding: UTF-8
11 |
12 | RnwWeave: Sweave
13 | LaTeX: pdfLaTeX
14 |
15 | AutoAppendNewline: Yes
16 | StripTrailingWhitespace: Yes
17 |
18 | BuildType: Package
19 | PackageUseDevtools: Yes
20 | PackageInstallArgs: --no-multiarch --with-keep.source
21 |
--------------------------------------------------------------------------------
/R/darkly_format.R:
--------------------------------------------------------------------------------
1 | #' Format helper function for labels
2 | #' @param x input vector frame requires input
3 | #' @param labelx label identifier, takes "round" or "percent', all others default to character
4 | #' @param n.decimals when labelx=round digits parameter for round(), when labelx=percent accuracy parameter for scales::percent()
5 | #' @export
6 | #'
7 | darkly_format = function(x, labelx, n.decimals = 2) {
8 | case_when(
9 | labelx == "round" ~ as.character(round(x, n.decimals)),
10 | labelx == "percent" ~ scales::percent(x, n.decimals),
11 | T ~ as.character(x)
12 | )
13 | }
14 |
--------------------------------------------------------------------------------
/man/theme_dark2.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/theme_dark2.R
3 | \name{theme_dark2}
4 | \alias{theme_dark2}
5 | \title{Alternative dark theme for ggplot2}
6 | \usage{
7 | theme_dark2(base_size = 12, base_family = "Courier New")
8 | }
9 | \arguments{
10 | \item{base_size}{base font size, given in pts. defaults to 12}
11 |
12 | \item{base_family}{base font family, defaults to Courier New}
13 | }
14 | \description{
15 | A dark themed function
16 | }
17 | \examples{
18 | ggplot(data=NULL,aes(x=1:5,y=1:5))+geom_line()+theme_dark2()
19 | }
20 | \keyword{theme}
21 |
--------------------------------------------------------------------------------
/man/darkly_format.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/darkly_format.R
3 | \name{darkly_format}
4 | \alias{darkly_format}
5 | \title{Format helper function for labels}
6 | \usage{
7 | darkly_format(x, labelx, n.decimals = 2)
8 | }
9 | \arguments{
10 | \item{x}{input vector frame requires input}
11 |
12 | \item{labelx}{label identifier, takes "round" or "percent', all others default to character}
13 |
14 | \item{n.decimals}{when labelx=round digits parameter for round(), when labelx=percent accuracy parameter for scales::percent()}
15 | }
16 | \description{
17 | Format helper function for labels
18 | }
19 |
--------------------------------------------------------------------------------
/R/mtg_rate.R:
--------------------------------------------------------------------------------
1 | #' Mortgage rate data
2 | #'
3 | #'
4 | #' Time series of U.S. weekly average 30-year mortgage rates from April 2, 1971 through July 2, 2020.
5 | #'
6 | #' Data are from the Freddie Mac Primary Mortgage Market Survey.
7 | #'
8 | #' Rates are expressed in percentage points
9 | #' 3 columnns:
10 | #' date: date of survey
11 | #' rate: U.S. weekly average 30-year mortgage rate in percentage points
12 | #' pts: Average discount points and origination fees in percentage points
13 | #'
14 | #' @docType data
15 | #'
16 | #' @usage data(mtg_rate)
17 | #'
18 | #' @keywords datasets
19 | #'
20 |
21 | #' @source \href{http://www.freddiemac.com/pmms/}{Freddie Mac PMMS}
22 | #'
23 | #' @examples
24 | #' data(mtg_rate)
25 | "mtg_rate"
26 |
--------------------------------------------------------------------------------
/DESCRIPTION:
--------------------------------------------------------------------------------
1 | Package: darklyplot
2 | Title: Make Simple Dark Themed Time Series Plots
3 | Version: 0.1.0
4 | Authors@R: person("Leonard", "Kiefer", role = c("aut", "cre"), email = "lenkiefer@hotmail.com")
5 | Description: This packages lets you create simple dark-theme times series plots.
6 | It extends ggplot2 and relies on mdthemes to make color coded axis labels.
7 | The axis labels use ggthemes::geom_rangeframe to create Tufte-like axes.
8 | Through parameters, the user can alter the colors, include shading under the line,
9 | and also add a single reference line.
10 | Encoding: UTF-8
11 | LazyData: true
12 | Depends:
13 | dplyr,
14 | glue,
15 | ggplot2,
16 | ggthemes,
17 | mdthemes,
18 | scales,
19 | shiny
20 | RoxygenNote: 7.1.0
21 | Suggests:
22 | knitr,
23 | rmarkdown
24 | VignetteBuilder: knitr
25 |
--------------------------------------------------------------------------------
/man/mtg_rate.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/mtg_rate.R
3 | \docType{data}
4 | \name{mtg_rate}
5 | \alias{mtg_rate}
6 | \title{Mortgage rate data}
7 | \format{
8 | An object of class \code{tbl_df} (inherits from \code{tbl}, \code{data.frame}) with 2571 rows and 3 columns.
9 | }
10 | \source{
11 | \href{http://www.freddiemac.com/pmms/}{Freddie Mac PMMS}
12 | }
13 | \usage{
14 | data(mtg_rate)
15 | }
16 | \description{
17 | Time series of U.S. weekly average 30-year mortgage rates from April 2, 1971 through July 2, 2020.
18 | }
19 | \details{
20 | Data are from the Freddie Mac Primary Mortgage Market Survey.
21 |
22 | Rates are expressed in percentage points
23 | 3 columnns:
24 | date: date of survey
25 | rate: U.S. weekly average 30-year mortgage rate in percentage points
26 | pts: Average discount points and origination fees in percentage points
27 | }
28 | \examples{
29 | data(mtg_rate)
30 | }
31 | \keyword{datasets}
32 |
--------------------------------------------------------------------------------
/man/darklyplot.Rd:
--------------------------------------------------------------------------------
1 | % Generated by roxygen2: do not edit by hand
2 | % Please edit documentation in R/darklyplot.R
3 | \name{darklyplot}
4 | \alias{darklyplot}
5 | \title{Function to create a dark themed plot}
6 | \usage{
7 | darklyplot(
8 | df,
9 | column,
10 | col = "white",
11 | n.decimals = 0,
12 | refline = FALSE,
13 | refValue = 0,
14 | refCol = NA,
15 | shade = FALSE,
16 | shadeCol = "dodgerblue",
17 | shadeAlpha = 0.15,
18 | minCol = "#6CB23F",
19 | maxCol = "#FD5305",
20 | firstCol = "white",
21 | lastCol = "#00AFEF",
22 | labelx = "round",
23 | Ndodge = 3
24 | )
25 | }
26 | \arguments{
27 | \item{df}{input data frame requires input, must have a date column named date}
28 |
29 | \item{column}{input data column name to plot}
30 |
31 | \item{col}{color for line, defaults to "white"}
32 |
33 | \item{n.decimals}{number of decimals to display, default is 0}
34 |
35 | \item{refline}{include a reference line? default is FALSE}
36 |
37 | \item{refValue}{value for reference line, defaults to 0}
38 |
39 | \item{shade}{shade under the line? default is FALSE}
40 |
41 | \item{shadeCol}{color for shading under line default is "dodgerblue"}
42 |
43 | \item{shadeAlpha}{alpha (transparency) for shading, default is 0.15}
44 |
45 | \item{minCol}{color for minimum value}
46 |
47 | \item{maxCol}{color for maximum value}
48 |
49 | \item{firstCol}{color for first value}
50 |
51 | \item{lastCol}{color of last value}
52 |
53 | \item{Ndodge}{n.dodge parameter passed to scale_x_date(guide_axis(n.dodge = Ndodge)...)}
54 |
55 | \item{labelX}{function for labeling values, default is round, percent also available}
56 | }
57 | \description{
58 | A dark themed plot
59 | }
60 | \examples{
61 | darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3)
62 | darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3,shade=TRUE,refline=TRUE)
63 | }
64 | \keyword{theme}
65 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | # darklyplot
5 |
6 |
7 |
8 |
9 |
10 | The goal of darklyplot is to create simple time series plots with a dark
11 | background. The miniminum and maximum values are highlighted, and color
12 | coded along with the y axis and x axis labels.
13 |
14 | ## Installation
15 |
16 | You can install the development version of darklyplot from
17 | [GitHub](https://github.com/) with:
18 |
19 | ``` r
20 | # install.packages("devtools")
21 | devtools::install_github("lenkiefer/darklyplot")
22 | ```
23 |
24 | ## Examples
25 |
26 | This is a basic example which shows you how to solve a common problem:
27 |
28 | ``` r
29 | library(darklyplot)
30 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3)
31 | ```
32 |
33 |
34 |
35 | You can vary the chart look with several parameters:
36 |
37 | ``` r
38 | darklyplot(df=mtg_rate,
39 | column="rate",
40 | col="white", #can use R color names or hex
41 | n.decimals=0,
42 | refline=TRUE,
43 | refValue=5,
44 | refCol="purple",
45 | shade=TRUE,
46 | shadeCol="#fe5305",
47 | shadeAlpha=0.35,
48 | minCol="blue",
49 | maxCol="red",
50 | firstCol="orange",
51 | lastCol="pink",
52 | labelx="round",
53 | Ndodge=2)
54 | ```
55 |
56 |
57 |
58 | This will work with any dataframe with a numeric column and a date index
59 | (named “date”):
60 |
61 | ``` r
62 | set.seed(20200704)
63 | df_test=data.frame(date=seq.Date(from=as.Date("2020-01-01"),to=as.Date("2020-06-30"),by="1 day"))
64 | df_test$random_variable=rnorm(NROW(df_test),0,1)
65 | darklyplot(df=df_test,column="random_variable",n.decimals=2)+labs(title="A Random Variable")
66 | ```
67 |
68 |
69 |
--------------------------------------------------------------------------------
/README.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | output: github_document
3 | ---
4 |
5 |
6 |
7 | ```{r, include = FALSE}
8 | knitr::opts_chunk$set(
9 | collapse = TRUE,
10 | comment = "#>",
11 | fig.path = "man/figures/README-",
12 | out.width = "100%"
13 | )
14 | ```
15 |
16 | # darklyplot
17 |
18 |
19 |
20 |
21 | The goal of darklyplot is to create simple time series plots with a dark background. The miniminum and maximum values are highlighted, and color coded along with the y axis and x axis labels.
22 |
23 | ## Installation
24 |
25 | You can install the development version of darklyplot from [GitHub](https://github.com/) with:
26 |
27 | ``` r
28 | # install.packages("devtools")
29 | devtools::install_github("lenkiefer/darklyplot")
30 | ```
31 | ## Examples
32 |
33 | This is a basic example which shows you how to solve a common problem:
34 |
35 | ```{r example,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
36 | library(darklyplot)
37 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3)
38 | ```
39 |
40 | You can vary the chart look with several parameters:
41 |
42 | ```{r example2,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
43 | darklyplot(df=mtg_rate,
44 | column="rate",
45 | col="white", #can use R color names or hex
46 | n.decimals=0,
47 | refline=TRUE,
48 | refValue=5,
49 | refCol="purple",
50 | shade=TRUE,
51 | shadeCol="#fe5305",
52 | shadeAlpha=0.35,
53 | minCol="blue",
54 | maxCol="red",
55 | firstCol="orange",
56 | lastCol="pink",
57 | labelx="round",
58 | Ndodge=2)
59 |
60 | ```
61 |
62 | This will work with any dataframe with a numeric column and a date index (named "date"):
63 |
64 | ```{r example-3,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
65 | set.seed(20200704)
66 | df_test=data.frame(date=seq.Date(from=as.Date("2020-01-01"),to=as.Date("2020-06-30"),by="1 day"))
67 | df_test$random_variable=rnorm(NROW(df_test),0,1)
68 | darklyplot(df=df_test,column="random_variable",n.decimals=2)+labs(title="A Random Variable")
69 | ```
70 |
--------------------------------------------------------------------------------
/doc/using-darklyplot.R:
--------------------------------------------------------------------------------
1 | ## ---- include = FALSE---------------------------------------------------------
2 | knitr::opts_chunk$set(
3 | collapse = TRUE,
4 | comment = "#>"
5 | )
6 |
7 | ## ----setup1, eval=FALSE-------------------------------------------------------
8 | # # install.packages("devtools")
9 | # devtools::install_github("lenkiefer/darklyplot")
10 |
11 | ## ----vignette,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
12 | library(darklyplot)
13 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3)
14 |
15 | ## ----vignette-2,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
16 | darklyplot(df=mtg_rate,column="rate",
17 | labelx="roundx",n.decimals=3,
18 | minCol="white",maxCol="white",
19 | firstCol="white",lastCol="white")
20 |
21 | ## ----vignette-3,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
22 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
23 | refline=TRUE,refCol="yellow",refValue=5)
24 |
25 | ## ----vignette-4,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
26 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
27 | refCol="yellow",refline=TRUE,refValue=5,
28 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)
29 |
30 | ## ---- eval=FALSE--------------------------------------------------------------
31 | # #> Error: Invalid input: date_trans works with objects of class Date only
32 |
33 | ## ----vignette-6,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
34 | txhousing2 <- txhousing[txhousing$city=="Lubbock",]
35 | txhousing2$date <- as.Date(ISOdate(txhousing2$year, txhousing2$month,1))
36 |
37 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
38 | labs(title="Monthly home sales in Lubbock, Texas")
39 |
40 | ## ----vignette-7,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
41 | # filter out months with missing sales data
42 | txhousing2 <- txhousing2[!is.na(txhousing2$sales),]
43 |
44 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
45 | labs(title="Monthly home sales in Lubbock, Texas")
46 |
47 | ## ----vignette-8,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8----
48 | # compute monthly percent changes for Austin Texas
49 | txhousing3 <- txhousing[txhousing$city=="Austin",]
50 | txhousing3$date <- as.Date(ISOdate(txhousing3$year, txhousing3$month,1))
51 | txhousing3$dsales <- (txhousing3$sales/lag(txhousing3$sales,12) -1 )
52 | txhousing3 <- txhousing3[!is.na(txhousing3$dsales),]
53 |
54 | darklyplot(df=txhousing3,column="dsales",
55 | refline=TRUE,
56 | labelx="percent",
57 | #n.decimals corresponds to accuracy in scales::percent when labelx="percent"
58 | n.decimals=.1)+
59 | labs(title="12-month perent change home sales in Austin, Texas")
60 |
61 |
--------------------------------------------------------------------------------
/R/theme_dark2.R:
--------------------------------------------------------------------------------
1 | #' Alternative dark theme for ggplot2
2 | #'
3 | #' A dark themed function
4 | #' @param base_size base font size, given in pts. defaults to 12
5 | #' @param base_family base font family, defaults to Courier New
6 | #' @keywords theme
7 | #' @export
8 | #' @examples
9 | #' ggplot(data=NULL,aes(x=1:5,y=1:5))+geom_line()+theme_dark2()
10 |
11 | theme_dark2 = function(base_size = 12, base_family = "Courier New") {
12 | theme_grey(base_size = base_size, base_family = base_family) %+replace%
13 |
14 | theme(
15 | line=element_line(color="white"),
16 | rect=element_rect(color="white"),
17 | text=element_text(color="white"),
18 | # Specify axis options
19 | axis.line = element_blank(),
20 | axis.text.x = element_text(size = base_size*0.8, color = "white", lineheight = 0.9),
21 | axis.text.y = element_text(size = base_size*0.8, color = "white", lineheight = 0.9),
22 | axis.ticks = element_line(color = "white", size = 0.2),
23 | axis.title.x = element_text(size = base_size, color = "white", margin = margin(0, 10, 0, 0)),
24 | axis.title.y = element_text(size = base_size, color = "white", angle = 90, margin = margin(0, 10, 0, 0)),
25 | axis.ticks.length = unit(0.3, "lines"),
26 | # Specify legend options
27 | legend.background = element_rect(color = NA, fill = " gray10"),
28 | legend.key = element_rect(color = "white", fill = " gray10"),
29 | legend.key.size = unit(1.2, "lines"),
30 | legend.key.height = NULL,
31 | legend.key.width = NULL,
32 | legend.text = element_text(size = base_size*0.8, color = "white"),
33 | legend.title = element_text(size = base_size*0.8, face = "bold", hjust = 0, color = "white"),
34 | legend.position = "right",
35 | legend.text.align = NULL,
36 | legend.title.align = NULL,
37 | legend.direction = "vertical",
38 | legend.box = NULL,
39 | # Specify panel options
40 | panel.background = element_rect(fill = " gray10", color = NA),
41 | #panel.border = element_rect(fill = NA, color = "white"),
42 | panel.border=element_blank(),
43 | panel.grid.major = element_line(color = "grey35"),
44 | panel.grid.minor = element_line(color = "grey20"),
45 | panel.spacing = unit(0.5, "lines"),
46 | # Specify facetting options
47 | strip.background = element_rect(fill = "grey30", color = "grey10"),
48 | strip.text.x = element_text(size = base_size*0.8, color = "white"),
49 | strip.text.y = element_text(size = base_size*0.8, color = "white",angle = -90),
50 | # Specify plot options
51 | plot.background = element_rect(color = " gray10", fill = " gray10"),
52 | plot.title = element_text(size = base_size*1.2, color = "white",hjust=0,lineheight=1.25,
53 | margin=margin(2,2,2,2)),
54 | plot.subtitle = element_text(size = base_size*1, color = "white",hjust=0, margin=margin(2,2,2,2)),
55 | plot.caption = element_text(size = base_size*0.8, color = "white",hjust=0),
56 | plot.margin = unit(rep(1, 4), "lines")
57 |
58 | )
59 |
60 | }
61 |
62 |
63 |
--------------------------------------------------------------------------------
/doc/using-darklyplot.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Using darklyplot"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Using darklyplot}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | \usepackage[utf8]{inputenc}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | knitr::opts_chunk$set(
12 | collapse = TRUE,
13 | comment = "#>"
14 | )
15 | ```
16 |
17 |
18 | The goal of darklyplot is to create simple time series plots with a dark background. The miniminum and maximum values are highlighted, and color coded along with the y axis and x axis labels. This vignette walks through basic usage and explores somo of the options.
19 |
20 | darklyplot can be installed from [GitHub](https://github.com/) with:
21 |
22 | ``` {r setup1, eval=FALSE}
23 | # install.packages("devtools")
24 | devtools::install_github("lenkiefer/darklyplot")
25 | ```
26 |
27 | ## A first plot
28 |
29 | The darklyplot package comes with a dataset `mtg_rate` which contains a time series of U.S. weekly average 30-year fixed mortgage rates from Freddie Mac's [Primary Mortgage Market Survey](http://www.freddiemac.com/pmms/). The following code constructs a time series plot of the weekly values from April 2, 1971 through July 2, 2020.
30 |
31 | ```{r vignette,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
32 | library(darklyplot)
33 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3)
34 | ```
35 |
36 | The darklyplot plot combines a dark theme with a minimalist aesthetic. Using `geom_rangeframe` from the [ggthemes](https://jrnold.github.io/ggthemes/reference/geom_rangeframe.html) package we limit the axis to cover the minimum and maxiumum values. We also highlight the first, last, minimum and maximum value with separate axis ticks and different colors. The user can select the colors to highlight the values.
37 |
38 | For example, you could force darklyplot to use only one color, white, to represent all values:
39 |
40 | ```{r vignette-2,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
41 | darklyplot(df=mtg_rate,column="rate",
42 | labelx="roundx",n.decimals=3,
43 | minCol="white",maxCol="white",
44 | firstCol="white",lastCol="white")
45 | ```
46 |
47 | ## Shading and reference lines
48 |
49 | darklyplot allows you to add a reference line. By default the reference line is set to 0, but you can adjust it to any fixed value. For example, we can modify our mortgage rate plot to add a reference line at 5 percentage points.
50 |
51 |
52 | ```{r vignette-3,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
53 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
54 | refline=TRUE,refCol="yellow",refValue=5)
55 | ```
56 |
57 | You can also shade between the reference line and the data by setting `shade=TRUE`. The default shading is `dodgerblue` with alpha set to 0.15, but you can adjust those values with the `shadeCol` and `shadeAlpha` parameters.
58 |
59 |
60 | ```{r vignette-4,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
61 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
62 | refCol="yellow",refline=TRUE,refValue=5,
63 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)
64 | ```
65 |
66 |
67 | ## Use with any time series data
68 |
69 | You can used darklyplot with any time series as long as the data is stored a dataframe with a date formatted column called `date`, and the desired column is numeric. THe `ggplot` package comes with a dataframe called `txhousing` that has a column called date. However, it is a double not a date so if you tried to use it with darklplot you would get an error.
70 |
71 | In the example below we first filter `txhousing` to just the observations for Lubbock, Texas and try to plot sales with darklyplot. This code will result in an error
72 |
73 | ```{r vignette-5,message=FALSE,eval=FALSE,warning=FALSE,out.width="50%",fig.width=12,purl = FALSE,fig.height=8}
74 | txhousing2 <- txhousing[txhousing$city=="Lubbock",]
75 |
76 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3,
77 | refCol="yellow",refline=TRUE,refValue=5,
78 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)
79 | ```
80 |
81 | ```{r, eval=FALSE}
82 | #> Error: Invalid input: date_trans works with objects of class Date only
83 | ```
84 | If we convert the `date` column to date format the plot will work:
85 |
86 | ```{r vignette-6,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
87 | txhousing2 <- txhousing[txhousing$city=="Lubbock",]
88 | txhousing2$date <- as.Date(ISOdate(txhousing2$year, txhousing2$month,1))
89 |
90 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
91 | labs(title="Monthly home sales in Lubbock, Texas")
92 | ```
93 |
94 |
95 | ### Where is my min/max?
96 |
97 | The plot above lacks a min/max. That is because the dataframe we created has some missing observations. Future versions of darklyplot may adjust for this, but for now we have to work around that issue:
98 |
99 | ```{r vignette-7,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
100 | # filter out months with missing sales data
101 | txhousing2 <- txhousing2[!is.na(txhousing2$sales),]
102 |
103 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
104 | labs(title="Monthly home sales in Lubbock, Texas")
105 | ```
106 |
107 | ## Using percent formats
108 |
109 | It's often the case that you want plot data with percentage changes. We can use darklplot's `labelx` parameter to format percents on the axis labels with `scales::percent`.
110 |
111 | ```{r vignette-8,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
112 | # compute monthly percent changes for Austin Texas
113 | txhousing3 <- txhousing[txhousing$city=="Austin",]
114 | txhousing3$date <- as.Date(ISOdate(txhousing3$year, txhousing3$month,1))
115 | txhousing3$dsales <- (txhousing3$sales/lag(txhousing3$sales,12) -1 )
116 | txhousing3 <- txhousing3[!is.na(txhousing3$dsales),]
117 |
118 | darklyplot(df=txhousing3,column="dsales",
119 | refline=TRUE,
120 | labelx="percent",
121 | #n.decimals corresponds to accuracy in scales::percent when labelx="percent"
122 | n.decimals=.1)+
123 | labs(title="12-month perent change home sales in Austin, Texas")
124 | ```
125 |
--------------------------------------------------------------------------------
/vignettes/using-darklyplot.Rmd:
--------------------------------------------------------------------------------
1 | ---
2 | title: "Using darklyplot"
3 | output: rmarkdown::html_vignette
4 | vignette: >
5 | %\VignetteIndexEntry{Using darklyplot}
6 | %\VignetteEngine{knitr::rmarkdown}
7 | \usepackage[utf8]{inputenc}
8 | ---
9 |
10 | ```{r, include = FALSE}
11 | knitr::opts_chunk$set(
12 | collapse = TRUE,
13 | comment = "#>"
14 | )
15 | ```
16 |
17 |
18 | The goal of darklyplot is to create simple time series plots with a dark background. The miniminum and maximum values are highlighted, and color coded along with the y axis and x axis labels. This vignette walks through basic usage and explores somo of the options.
19 |
20 | darklyplot can be installed from [GitHub](https://github.com/) with:
21 |
22 | ``` {r setup1, eval=FALSE}
23 | # install.packages("devtools")
24 | devtools::install_github("lenkiefer/darklyplot")
25 | ```
26 |
27 | ## A first plot
28 |
29 | The darklyplot package comes with a dataset `mtg_rate` which contains a time series of U.S. weekly average 30-year fixed mortgage rates from Freddie Mac's [Primary Mortgage Market Survey](http://www.freddiemac.com/pmms/). The following code constructs a time series plot of the weekly values from April 2, 1971 through July 2, 2020.
30 |
31 | ```{r vignette,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
32 | library(darklyplot)
33 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3)
34 | ```
35 |
36 | The darklyplot plot combines a dark theme with a minimalist aesthetic. Using `geom_rangeframe` from the [ggthemes](https://jrnold.github.io/ggthemes/reference/geom_rangeframe.html) package we limit the axis to cover the minimum and maxiumum values. We also highlight the first, last, minimum and maximum value with separate axis ticks and different colors. The user can select the colors to highlight the values.
37 |
38 | For example, you could force darklyplot to use only one color, white, to represent all values:
39 |
40 | ```{r vignette-2,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
41 | darklyplot(df=mtg_rate,column="rate",
42 | labelx="roundx",n.decimals=3,
43 | minCol="white",maxCol="white",
44 | firstCol="white",lastCol="white")
45 | ```
46 |
47 | ## Shading and reference lines
48 |
49 | The darklyplot packages allows you to add a reference line to your plot. By default the reference line is set to 0, but you can adjust it to any fixed value. For example, we can modify our mortgage rate plot to add a reference line at 5 percentage points.
50 |
51 |
52 | ```{r vignette-3,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
53 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
54 | refline=TRUE,refCol="yellow",refValue=5)
55 | ```
56 |
57 | You can also shade between the reference line and the data by setting `shade=TRUE`. The default shading is `dodgerblue` with alpha set to 0.15, but you can adjust those values with the `shadeCol` and `shadeAlpha` parameters.
58 |
59 |
60 | ```{r vignette-4,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
61 | darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
62 | refCol="yellow",refline=TRUE,refValue=5,
63 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)
64 | ```
65 |
66 |
67 | ## Use with any time series data
68 |
69 | You can used darklyplot with any time series as long as the data is stored a dataframe with a date formatted column called `date`, and the desired column is numeric. THe `ggplot` package comes with a dataframe called `txhousing` that has a column called date. However, it is a double not a date so if you tried to use it with darklplot you would get an error.
70 |
71 | In the example below we first filter `txhousing` to just the observations for Lubbock, Texas and try to plot sales with darklyplot. This code will result in an error
72 |
73 | ```{r vignette-5,message=FALSE,eval=FALSE,warning=FALSE,out.width="50%",fig.width=12,purl = FALSE,fig.height=8}
74 | txhousing2 <- txhousing[txhousing$city=="Lubbock",]
75 |
76 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3,
77 | refCol="yellow",refline=TRUE,refValue=5,
78 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)
79 | ```
80 |
81 | ```{r, eval=FALSE}
82 | #> Error: Invalid input: date_trans works with objects of class Date only
83 | ```
84 | If we convert the `date` column to date format the plot will work:
85 |
86 | ```{r vignette-6,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
87 | txhousing2 <- txhousing[txhousing$city=="Lubbock",]
88 | txhousing2$date <- as.Date(ISOdate(txhousing2$year, txhousing2$month,1))
89 |
90 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
91 | labs(title="Monthly home sales in Lubbock, Texas")
92 | ```
93 |
94 |
95 | ### Where is my min/max?
96 |
97 | The plot above lacks a min/max. That is because the dataframe we created has some missing observations. Future versions of darklyplot may adjust for this, but for now we have to work around that issue:
98 |
99 | ```{r vignette-7,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
100 | # filter out months with missing sales data
101 | txhousing2 <- txhousing2[!is.na(txhousing2$sales),]
102 |
103 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
104 | labs(title="Monthly home sales in Lubbock, Texas")
105 | ```
106 |
107 | ## Using percent formats
108 |
109 | It's often the case that you want plot data with percentage changes. We can use darklplot's `labelx` parameter to format percents on the axis labels with `scales::percent`.
110 |
111 | ```{r vignette-8,message=FALSE,warning=FALSE,out.width="50%",fig.width=12,fig.height=8}
112 | # compute monthly percent changes for Austin Texas
113 | txhousing3 <- txhousing[txhousing$city=="Austin",]
114 | txhousing3$date <- as.Date(ISOdate(txhousing3$year, txhousing3$month,1))
115 | txhousing3$dsales <- (txhousing3$sales/lag(txhousing3$sales,12) -1 )
116 | txhousing3 <- txhousing3[!is.na(txhousing3$dsales),]
117 |
118 | darklyplot(df=txhousing3,column="dsales",
119 | refline=TRUE,
120 | labelx="percent",
121 | #n.decimals corresponds to accuracy in scales::percent when labelx="percent"
122 | n.decimals=.1)+
123 | labs(title="12-month perent change home sales in Austin, Texas")
124 | ```
125 |
--------------------------------------------------------------------------------
/R/darklyplot.R:
--------------------------------------------------------------------------------
1 | #' Function to create a dark themed plot
2 | #'
3 | #' A dark themed plot
4 | #' @param df input data frame requires input, must have a date column named date
5 | #' @param column input data column name to plot
6 | #' @param col color for line, defaults to "white"
7 | #' @param n.decimals number of decimals to display, default is 0
8 | #' @param refline include a reference line? default is FALSE
9 | #' @param refValue value for reference line, defaults to 0
10 | #' @param shade shade under the line? default is FALSE
11 | #' @param shadeCol color for shading under line default is "dodgerblue"
12 | #' @param shadeAlpha alpha (transparency) for shading, default is 0.15
13 | #' @param minCol color for minimum value
14 | #' @param maxCol color for maximum value
15 | #' @param firstCol color for first value
16 | #' @param lastCol color of last value
17 | #' @param labelX function for labeling values, default is round, percent also available
18 | #' @param Ndodge n.dodge parameter passed to scale_x_date(guide_axis(n.dodge = Ndodge)...)
19 | #' @keywords theme
20 | #' @export
21 | #' @examples
22 | #' darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3)
23 | #' darklyplot(mtg_rate,"rate",labelx="roundx",n.decimals=3,shade=TRUE,refline=TRUE)
24 | #' @import mdthemes
25 | #' @import ggthemes
26 |
27 |
28 |
29 | darklyplot <- function(df,
30 | column,
31 | col="white",
32 | n.decimals=0,
33 | refline=FALSE,
34 | refValue=0,
35 | refCol=NA,
36 | shade=FALSE,
37 | shadeCol="dodgerblue",
38 | shadeAlpha=0.15,
39 | minCol="#6CB23F",
40 | maxCol="#FD5305",
41 | firstCol="white",
42 | lastCol="#00AFEF",
43 | labelx="round",
44 | Ndodge=3){
45 |
46 | if (is.na(refCol)){refCol=col}
47 | column=sym(column)
48 | df <- mutate(df, yvar=!!column)
49 | dd <- max(df$date)
50 | dd2 <- last(df[df$yvar==min(df$yvar),]$date)
51 | dd3 <- last(df[df$yvar==max(df$yvar),]$date)
52 | g1 <-
53 | ggplot(data = df, aes(x = date, y = !!column)) +
54 | geom_line(color = col, size = 1.05) +
55 | geom_point(
56 | data = . %>% filter(date == min(date)),
57 | color = firstCol,
58 | size = 6,
59 | alpha = 0.5
60 | ) +
61 | geom_point(
62 | data = . %>% filter(date == dd2),
63 | color = minCol,
64 | size = 6,
65 | alpha = 0.5
66 | ) +
67 | geom_point(
68 | data = . %>% filter(date == dd3),
69 | color = maxCol,
70 | size = 6,
71 | alpha = 0.5
72 | ) +
73 | geom_point(
74 | data = . %>% filter(date == max(date)),
75 | color = lastCol,
76 | size = 6,
77 | alpha = 0.5
78 | ) +
79 | if (refline) {
80 | geom_segment(
81 | y = refValue,
82 | yend = refValue,
83 | x = min(df$date),
84 | xend = dd,
85 | linetype = 2,
86 | color = refCol
87 | )
88 | }
89 | g1 <-
90 | g1 +
91 | ggthemes::geom_rangeframe(color = col) +
92 | scale_y_continuous(
93 | breaks = c(
94 | if (refline) {
95 | refValue
96 | },
97 | max(df$yvar),
98 | last(df$yvar),
99 | first(df$yvar),
100 | min(df$yvar)
101 | ),
102 | limits = c(min(
103 | ifelse(refline, refValue, min(df$yvar)), min(pretty(df$yvar))
104 | ),
105 | max(
106 | ifelse(refline, refValue, max(df$yvar)), max(pretty(df$yvar))
107 | )),
108 | guide = guide_axis(n.dodge = 1),
109 | labels = c(
110 | if (refline) {
111 | glue::glue(
112 | "{shiny::span('",
113 | darkly_format(refValue, labelx, n.decimals),
114 | "', style='color:",
115 | refCol,
116 | "')}"
117 | )
118 | },
119 | glue::glue(
120 | "{shiny::span('**",
121 | darkly_format(max(df$yvar), labelx, n.decimals),
122 | "**', style='color:",
123 | maxCol,
124 | "')}"
125 | ),
126 | glue::glue(
127 | "{shiny::span('**",
128 | darkly_format(tail(df, 1)$yvar, labelx, n.decimals),
129 | "**', style='color:",
130 | lastCol,
131 | "')}"
132 | ),
133 | glue::glue(
134 | "{shiny::span('**",
135 | darkly_format(head(df, 1)$yvar, labelx, n.decimals),
136 | "**', style='color:",
137 | firstCol,
138 | "')}"
139 | ),
140 | glue::glue(
141 | "{shiny::span('**",
142 | darkly_format(min(df$yvar), labelx, n.decimals),
143 | "**', style='color:",
144 | minCol,
145 | "')}"
146 | )
147 | )
148 | ) +
149 | scale_x_date(
150 | breaks = c(min(df$date), dd3, dd2, max(df$date)),
151 | guide = guide_axis(n.dodge = Ndodge),
152 | labels =
153 | c(
154 | #as.character(min(df$date)),
155 | glue::glue(
156 | "{shiny::span('**",
157 | as.character(min(df$date)),
158 | "**', style='color:",
159 | firstCol,
160 | "')}"
161 | ),
162 | glue::glue(
163 | "{shiny::span('**",
164 | as.character(dd3),
165 | "**', style='color:",
166 | maxCol,
167 | "')}"
168 | ),
169 | glue::glue(
170 | "{shiny::span('**",
171 | as.character(dd2),
172 | "**', style='color:",
173 | minCol,
174 | "')}"
175 | ),
176 | glue::glue(
177 | "{shiny::span('**",
178 | as.character(max(df$date)),
179 | "**', style='color:",
180 | lastCol,
181 | "')}"
182 | )
183 | )
184 | # as.character(max(df$date)))
185 | ) +
186 | labs(
187 | subtitle = glue::glue(
188 | "{shiny::span('**First** ',style='color:",
189 | firstCol,
190 | "')}",
191 | " ",
192 | "{shiny::span('**Max**',style='color:",
193 | maxCol,
194 | "')}",
195 | " ",
196 | "{shiny::span('**Min**',style='color:",
197 | minCol,
198 | "')}",
199 | " ",
200 | "{shiny::span('**Last**',style='color:",
201 | lastCol,
202 | "')}"
203 | )
204 | ) +
205 | as_md_theme(
206 | theme_dark2(base_size = 18) +
207 | theme(
208 | axis.ticks.length = unit(0.5, "cm"),
209 | axis.ticks = element_line(color = col, size = 0.2),
210 | plot.title = element_text(face = "bold"),
211 | panel.grid.minor = element_blank(),
212 | plot.subtitle = element_text(size = rel(0.8)),
213 | panel.grid.major = element_blank()
214 | )
215 | )
216 |
217 | g1 + if (shade) {
218 | geom_ribbon(
219 | ymin = refValue,
220 | aes(ymax = yvar),
221 | alpha = shadeAlpha,
222 | fill = shadeCol,
223 | color = NA
224 | )
225 | }
226 |
227 | }
228 |
--------------------------------------------------------------------------------
/doc/using-darklyplot.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
The goal of darklyplot is to create simple time series plots with a dark background. The miniminum and maximum values are highlighted, and color coded along with the y axis and x axis labels. This vignette walks through basic usage and explores somo of the options.
323 |darklyplot can be installed from GitHub with:
324 | 326 |The darklyplot package comes with a dataset mtg_rate which contains a time series of U.S. weekly average 30-year fixed mortgage rates from Freddie Mac’s Primary Mortgage Market Survey. The following code constructs a time series plot of the weekly values from April 2, 1971 through July 2, 2020.
The darklyplot plot combines a dark theme with a minimalist aesthetic. Using geom_rangeframe from the ggthemes package we limit the axis to cover the minimum and maxiumum values. We also highlight the first, last, minimum and maximum value with separate axis ticks and different colors. The user can select the colors to highlight the values.
For example, you could force darklyplot to use only one color, white, to represent all values:
334 |darklyplot(df=mtg_rate,column="rate",
335 | labelx="roundx",n.decimals=3,
336 | minCol="white",maxCol="white",
337 | firstCol="white",lastCol="white")darklyplot allows you to add a reference line. By default the reference line is set to 0, but you can adjust it to any fixed value. For example, we can modify our mortgage rate plot to add a reference line at 5 percentage points.
343 |darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
344 | refline=TRUE,refCol="yellow",refValue=5)You can also shade between the reference line and the data by setting shade=TRUE. The default shading is dodgerblue with alpha set to 0.15, but you can adjust those values with the shadeCol and shadeAlpha parameters.
darklyplot(df=mtg_rate,column="rate",labelx="roundx",n.decimals=3,
348 | refCol="yellow",refline=TRUE,refValue=5,
349 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)You can used darklyplot with any time series as long as the data is stored a dataframe with a date formatted column called date, and the desired column is numeric. THe ggplot package comes with a dataframe called txhousing that has a column called date. However, it is a double not a date so if you tried to use it with darklplot you would get an error.
In the example below we first filter txhousing to just the observations for Lubbock, Texas and try to plot sales with darklyplot. This code will result in an error
txhousing2 <- txhousing[txhousing$city=="Lubbock",]
357 |
358 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3,
359 | refCol="yellow",refline=TRUE,refValue=5,
360 | shade=TRUE,shadeCol="yellow",shadeAlpha=0.35)If we convert the date column to date format the plot will work:
txhousing2 <- txhousing[txhousing$city=="Lubbock",]
364 | txhousing2$date <- as.Date(ISOdate(txhousing2$year, txhousing2$month,1))
365 |
366 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
367 | labs(title="Monthly home sales in Lubbock, Texas")The plot above lacks a min/max. That is because the dataframe we created has some missing observations. Future versions of darklyplot may adjust for this, but for now we have to work around that issue:
372 |# filter out months with missing sales data
373 | txhousing2 <- txhousing2[!is.na(txhousing2$sales),]
374 |
375 | darklyplot(df=txhousing2,column="sales",labelx="roundx",n.decimals=3)+
376 | labs(title="Monthly home sales in Lubbock, Texas")It’s often the case that you want plot data with percentage changes. We can use darklplot’s labelx parameter to format percents on the axis labels with scales::percent.
# compute monthly percent changes for Austin Texas
384 | txhousing3 <- txhousing[txhousing$city=="Austin",]
385 | txhousing3$date <- as.Date(ISOdate(txhousing3$year, txhousing3$month,1))
386 | txhousing3$dsales <- (txhousing3$sales/lag(txhousing3$sales,12) -1 )
387 | txhousing3 <- txhousing3[!is.na(txhousing3$dsales),]
388 |
389 | darklyplot(df=txhousing3,column="dsales",
390 | refline=TRUE,
391 | labelx="percent",
392 | #n.decimals corresponds to accuracy in scales::percent when labelx="percent"
393 | n.decimals=.1)+
394 | labs(title="12-month perent change home sales in Austin, Texas")