Is there away to add a conditional aes to a ggplot?
I am trying to build a function that generates a kernel plot but I want allow the function user the option to toggle weights on and off. I know I could write it as
if ( toggle_on = TRUE) { ggoplot with weighting) else if (toggle_on = FALSE) {ggplot without weights)
But I don't like this because I have to copy the plot code twice which gives an opportunity for the versions of the plot with and without weights to diverge if someone else later on changes one and forgets to change the other.
I would prefer a solution like this:
Below is one way to alter a plot via a function argument without having to rewrite the plot. Rather than making the "toggle" adjustment in the plot function, the adjustment can be made in the data prior to plotting. The altered column is then passed to the aes argument (size in this example).