R code and corresponding plot 2