This seems like an issue many people would face, along the lines of the Don't Repeat Yourself (DRY) principle. I couldn't find the answer anywhere, perhaps I've been searching for the wrong terms, which means my question title probably isn't very good. If people have better suggestions of how to title the question that would be appreciated.
I have several ggplot2
plots, and they all have some commands in common, as well as other commands that vary too much, so it is not worthwhile writing them altogether as a loop/function.
How can I include the common commands in a neat, one-liner?
An example would probably explain more clearly:
common.lines <- "theme_bw() +
geom_point(size = 2) +
stat_smooth(method = lm, alpha = 0.6) +
ylab("Height")"
my.plot <- ggplot(data = my_df, aes(x = "Length", y = "Height")) +
common.lines
jim.plot <- ggplot(data = jim_df, aes(x = "Width", y = "Height")) +
common.lines
My question is, how do I construct common.lines
? Making a string like the above doesn't work. I also tried making a vector and then paste
ing with +
as a separator.
Any suggestions?
Cheers