我正在尝试制作一个函数来绘制各种变量。我想在一个函数中初始化 ylim,这样如果我提供 ylim,那么只考虑它,否则不考虑。我可以通过 if 语句来实现这一点,但我想看看是否还有其他更好的选择?
# function to plot variables turbine wise
plot_fun <- function(df, x_var, y_var, plot_title = "", ncol = 3, ylim = NA){
g <- list()
for(i in 1:length(unique(df$turbine_id))){
temp_df <- subset(df, turbine_id == unique(df$turbine_id)[i])
comp.manufact <- temp_df$comp.manufact[1]
g[[i]] <- ggplot(temp_df, aes_string(x = x_var, y = y_var)) +
geom_point(size = 0.5, color = ifelse(comp.manufact == "Winergy", "darkgreen", "black")) +
ggtitle(paste("Turbine", unique(temp_df$turbine_id))) +
theme(axis.title = element_blank()) +
ylim
}
grid.arrange(grobs = g, ncol = ncol, top = plot_title, left = y_var, bottom = x_var)
}
如果我提供 ylim,它应该考虑它,否则不会。
谢谢你。