ggplot2: How to save components as variables to add to different plots?

Edit: SOLVED according to this StackOverflow post

I am making lots of similar plots but the exact data or variable plotted will be different between each plot. I basically want to save most of the geoms and aesthetics as a variable that I can call/add to each individual plot. This way, if I want to update a geom or aesthetic for all the plots, I only need to change it at one place (i.e. trying not to repeat myself).

Basically I want it to look something like this:

components <- geom1 + geom2 + aes1 + aes2 + theme

plot1<- ggplot(data1) + components

plot2<- ggplot(data2) + components

plot3<- ggplot(data3) + components

I got this to work with some components, like coord_fixed() , but not others. I'm not sure why and I haven't found anything through Googling. Anyone know if this can be done?

Below is the full code I am trying to do it with. Would probably only need to change data , y , type and var.equal for each plot.


data = FluorByTreatment_Fltr_Iter3.tdf,

x = Treatment,

y = Area,

type = "parametric", # ANOVA or Kruskal-Wallis

var.equal = FALSE, # ANOVA or Welch ANOVA

plot.type = "box",

results.subtitle = FALSE, # Remove freq stat subtitle

violin.args = list(alpha = 0), # Remove violin

point.args = list(alpha = 0), # Remove points (unless can do beeswarm here?)

boxplot.args = list(notch = TRUE,

width = 0.4,

alpha = 0.5,

aes(fill = Treatment)


pairwise.comparisons = TRUE,

pairwise.display = "significant",

Crossposted from Reddit