R: Good practice – adding footnotes to graphics

17Feb09

footnoteIn some statistical programs there is the option available to attach a footnote to the graphical output that is created. This footnote may contain the name of the script or the file that produced the graphic, the author’s name and the date of creation. In SAS for example there is a footnote command to achieve this. Ever since I realized that this makes life a lot easier, I wrote a simple three-lines function in R which I use at the end of the construction of any graphic. I suppose, that this is what my professors meant with “good practice”. The nice thing about implementing this in the grid graphics system is that you can produce multiple graphics [e.g. by par(mfrow=c(2, 2))] and still the footnote will be positioned correctly.

###############################################################
##                                                           ##
##      R: Good practice - adding footnotes to graphics      ##
##                                                           ##
###############################################################

# basic information at the beginning of each script
scriptName <- "filename.R"
author <- "mh"
footnote <- paste(scriptName, format(Sys.time(), "%d %b %Y"),
                  author, sep=" / ")

# default footnote is today's date, cex=.7 (size) and color
# is a kind of grey

makeFootnote <- function(footnoteText=
                         format(Sys.time(), "%d %b %Y"),
                         size= .7, color= grey(.5))
{
   require(grid)
   pushViewport(viewport())
   grid.text(label= footnoteText ,
             x = unit(1,"npc") - unit(2, "mm"),
             y= unit(2, "mm"),
             just=c("right", "bottom"),
             gp=gpar(cex= size, col=color))
   popViewport()
}

makeFootnote(footnote)

## Example ##
plot(1:10)
makeFootnote(footnote)

###############################################################

Here an example of a footnote added to the graphical output.

Correlation matrix with footnote

Correlation matrix with footnote

Cheers, Mark

About these ads


One Response to “R: Good practice – adding footnotes to graphics”

  1. 1 Kevin Wright

    I have a very similar idea that I use to add page numbers to graphics. I use a global variable to store the page number so that I can simply do things like:

    setPageNum(1)
    plot(1:10)
    pageNum()
    plot(1:20)
    pageNum()

    Here are the functions:

    setPageNum <- function(num=1){
    assign(".pagenum.", num, envir=.GlobalEnv)
    }

    getPageNum <- function(){
    # If the global variable is missing, create it and set it to 1
    if(!exists(".pagenum.", envir=.GlobalEnv)) {
    warning("The page number has not been set…setting to 1.")
    setPageNum(1)
    }
    get(".pagenum.", envir=.GlobalEnv)
    }

    pageNum <- function(num, text="Page", date=FALSE,
    date.format="%d %b %Y %H:%M:%S",
    pos=c(.04, .04), col="gray30"){
    # Might be nice to right-justify if pos is at the right side of the page

    require("grid")
    # If num is missing, use a global variable.
    if(missing(num)){
    num <- getPageNum()
    setPageNum(num+1)
    } else setPageNum(num+1)
    pn <- paste(text,num)

    if(date)
    pn <- paste(pn, "\n", format(Sys.time(), date.format), sep="")

    grid.clip() # See: http://tolstoy.newcastle.edu.au/R/help/06/06/30031.html
    pushViewport(viewport(pos[1], pos[2], width=stringWidth(pn), height=unit(2,"lines"),
    name="pagenum", gp=gpar(fontsize=8)))
    grid.text(pn, gp=gpar(col=col),
    just=c("left","bottom"))
    popViewport()
    }

    Kevin Wright


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: