R code for Plot 2