R script to plot queries