R code for plot 3