R code and corresponding plot 4