Modified the R code for plot4