我只是根据这个问题想出来的
How can I combine multiple ggplot2 elements into the return of a function?
.
set.seed(1)
df <- data.frame(x=rnorm(200, sd=3),
y=rnorm(200, sd=3))
baseDesign <- function(line_colors = c("red", "grey", "red"),
line_types = c('dashed', 'solid', 'dashed'),
plot_breaks = seq(-100,100,4),
plot_minor_breaks = seq(-100,100,1),
plot_limits = c(-13.5, 13.5)) {
p <- list(geom_hline(yintercept = c(-1, 0, 1),
color=line_colors, linetype=line_types),
geom_vline(xintercept = c(-1, 0, 1),
color=line_colors, linetype=line_types),
scale_x_continuous(expression(log[2]*" SILAC ratio (female - male)"),
breaks=plot_breaks, minor_breaks=plot_minor_breaks,
limits=plot_limits),
scale_y_continuous(expression(log[2]*" LFQ (female - male)"),
breaks=plot_breaks, minor_breaks=plot_minor_breaks,
limits=plot_limits))
return(p)
}
ggplot(df, aes(x=x, y=y)) +
geom_point(size=2, shape=21, color='grey20', alpha=.3) +
baseDesign()