Tricks for R Plots - SOLV-Code/FigRs GitHub Wiki

Here are some simple tricks that help with bringing R graphics into a collaborative process. The illustration at the end combines all of them. Note that this page focuses on plotting tricks. For more general R code illustrations (e.g. data wrangling), check out R Snippets for Fish Folks.

Print to pdf slides

R figures can be printed straight to a multi-page pdf file with the same dimensions as powerpoint slides. Once set up, this allows you to create hi-res presentations on the fly, for example in response to a request for changing one of the inputs in a simulation model. It also works for creating simple animations based on the same principle as the old flip-books. These pseudo-animations work on all browsers and operating systems, and are easy to share with end users (small file size, high resolution, common file type). Remember to use the same plot limits on each page to ensure seamless scrolling! (see illustration below)

pdf("TricksForPlots_SampleSlides.pdf",width=11, height=8.5,onefile=TRUE)
# insert all your plotting code here
dev.off() # close the plotting device

Multi-Panel Layouts

The layout() function divides the screen or pdf page (i.e. the active plotting device) into smaller panels. You can specify different heights and widths for the panels, plot figures that span multiple panels, and determine the order in which the panels are filled.

mat <- matrix(c(1,3,2,3), byrow=TRUE, nrow=2)
layout(mat, heights=c(2,1), widths=c(1,2))
layout.show(3) # check the 3 panels

Plot outside the bounds

The plot argument xpd = TRUE or xpd = NA expands a plotting area beyond its current boundaries. This lets you run point labels into the margins (xpd=TRUE) or visually connect two points in different panels (xpd=NA). For example, if you have two panels with the same range of years on the horizontal axis, then abline(v=c(1980, 1990, 2000, 2010), xpd=NA) will add lines across both panels to emphasize the start of each decade.

Illustration

Check out the result here.

# using the built-in airquality data set
head(airquality)

library(tidyverse)

# get variable names for plotting
vars.list <- names(airquality)[1:4]

# get common upper limits for y axis for each variable
y.lims.lower <- unlist(airquality[,vars.list] %>% 
                   dplyr::summarize_all(funs(min),na.rm=TRUE))
y.lims.upper <- unlist(airquality[,vars.list] %>% 
                   dplyr::summarize_all(funs(max),na.rm=TRUE))


# start the pdf
pdf("SampleSlides.pdf",width=11, height=8.5,onefile=TRUE)


for(month.plot  in 5:9){

# specify the layout
mat <- matrix(c(1:4,5,5,5,5), byrow=FALSE, ncol = 2)
layout(mat, heights=c(1,1,1,1), widths=c(1.5,1))
#layout.show(5) # check the 3 panels


data.month <- airquality %>% dplyr::filter(Month == month.plot)

# plot 1 panel for each time series
for(var.plot in vars.list){
  
plot(data.month$Day,data.month[,var.plot],xlim=c(0,31),
                 ylim=c(y.lims.lower[var.plot],y.lims.upper[var.plot]), bty = "n",
                 xlab= "Day", ylab= var.plot, bty="n", pch=19, col="red",type="o") 
  
if(var.plot == vars.list[4]){abline(v=c(7,12),col="darkblue",xpd=NA)}
    }

# add some other plot
par(pty="s") # square plot area
plot(data.month[,3],data.month[,4], xlab= vars.list[3],ylab=vars.list[4],
           pch=19,col="red", xlim = c(y.lims.lower[3],y.lims.upper[3]),
          ylim=c(y.lims.lower[4],y.lims.upper[4]))
par(pty="m") # reset plot area

title(main=paste("Month", month.plot), outer=TRUE, cex=1.5, line=-2)

} # end looping through month


dev.off() # close the plotting device