R code for Plot 4