adding R code for fig 1; it may need to be modified