R code for Plot 3