refine R codes for producing plots