R Code for Plot4