R Code for Plot 4