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.
ggbetweenstats(
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",
centrality.plotting = FALSE, # Don't plot mean
bf.message = FALSE # Remove Bayes stat subtitle
) +
geom_beeswarm(size = 4, # Add dodged points
shape = 20, # Represent datapoints as bullet (smaller circle)
cex = 3 # Increase point spacing
) +
stat_summary(
fun.min = function(Area) mean(Area) - sd(Area)/sqrt(length(Area)),
fun.max = function(Area) mean(Area) + sd(Area)/sqrt(length(Area)),
geom = "errorbar",
color = "White",
width = .15,
linewidth = 0.75
) +
stat_summary(fun = mean, # Calculate and plot means
geom = "point",
shape = 18, # Represent mean as a diamond
size = 3,
color = "White" # Highlight in red
) +
stat_n_text() + # Display sample size per condition
xlab("") + # Delete X-axis label
ylab(label = expression(paste("Area of ", italic("WOX5:GFP"), " Expression (", mu, m^2, ")",
sep = ""))) +
theme_simple() +
scale_fill_brewer(palette = "Dark2") +
theme(axis.title.y.right = element_blank(), # Remove 2nd axis title
axis.text.y.right = element_blank(), # Remove 2nd axis labels
axis.ticks.y.right = element_blank(), # Remove 2nd axis ticks
legend.position = "none" # Remove color legend
) +
coord_fixed(ratio = .000375) # Reduce spacing between groups on x axis
Crossposted from Reddit