R code for figs