creates function addLayerToLegend, consolidating code for adding a layer to the legend