R code and plots for remaining questions 2 to 4