R code for plot4