refactor code for plot1.R, plot2.R, plot3.R, plot4.R