Updated with R code and plots