R code for plot 4