R script to generate diagrams