R script for plot 4