Abstract

Sometimes it can be useful to add a footnote number to graphics. This package makes it easy to automatically add a time-stamp and page number to graphics. The package can also be used to add general text anywhere on any type of graphics device (base/lattice/ggplot).

R setup

library("knitr")
knitr::opts_chunk$set(fig.align="center", fig.width=6, fig.height=6)
library("pagenum")

Base graphics

First, set the starting page number to 1. Each call of the pagenum() function will add the page number to the current graphic and automatically increment the page number counter which is stored in options().

setPagenum(1)
plot(Sepal.Length~Sepal.Width, data=iris, col=Species, pch=19)
pagenum()

If the argument num="" is used, then the page counter is not automatically incremented.

Multiple figures with ‘Draft’ watermark

op = par(mfrow=c(1,2))
plot(Sepal.Length ~ Sepal.Width, data=iris, col=Species, pch=19)
plot(Petal.Length ~ Petal.Width, data=iris, col=Species, pch=19)
par(op)
pagenum(num="", text="Figures 2a, 2b")
pagenum(num="", text="Draft",
        x=.5, y=.95, just=c('center','top'),
        col="wheat", cex=3)

lattice with timestamp

setPagenum(getPagenum()+1) # Manual increment
library("lattice")
xyplot(Sepal.Length~Sepal.Width, data=iris, groups=Species)
pagenum(date=TRUE)

ggplot2 with stamp at top right

library("ggplot2")
ggplot(iris, aes(x=Sepal.Width, y=Sepal.Length,
                 color=Species)) + geom_point() + theme_classic()
pagenum(text="ABC Corp - ", date=TRUE,
        x=.95, y=.95, just=c('right','top'))

Acknowledgements

Thanks for help from Paul Murrell.

Session info

sessionInfo()
## R version 3.4.0 (2017-04-21)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 7 x64 (build 7601) Service Pack 1
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=C                          
## [2] LC_CTYPE=English_United States.1252   
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] ggplot2_2.2.1   lattice_0.20-35 pagenum_1.1     knitr_1.16     
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.11     magrittr_1.5     munsell_0.4.3    colorspace_1.3-2
##  [5] rlang_0.1.1      stringr_1.2.0    plyr_1.8.4       tools_3.4.0     
##  [9] grid_3.4.0       gtable_0.2.0     htmltools_0.3.6  yaml_2.1.14     
## [13] lazyeval_0.2.0   rprojroot_1.2    digest_0.6.12    tibble_1.3.3    
## [17] evaluate_0.10    rmarkdown_1.5    labeling_0.3     stringi_1.1.5   
## [21] compiler_3.4.0   scales_0.4.1     backports_1.1.0

References