R code and plots 1-4