It's been some time since I last wrote tidyeval
code, and since I remember that the API was evolving quite quickly, I want to make sure I'm not missing anything. Look at this function:
plot_predictions <- function(dataframe, x_var, y_var, y_pred, y_lwr, y_upr, color_var,
plot_title, target_days = NULL){
x_var <- enquo(x_var)
y_var <- enquo(y_var)
y_pred <- enquo(y_pred)
y_lwr <- enquo(y_lwr)
y_upr <- enquo(y_upr)
color_var <- enquo(color_var)
p <- ggplot(dataframe, aes(x = !! x_var, y = !! y_var, color = !! color_var)) +
geom_point() +
geom_line(aes(y = !! y_pred)) +
geom_line(aes(y = !! y_lwr), linetype = "dashed") +
geom_line(aes(y = !! y_upr), linetype = "dashed") +
labs(title = plot_title)
if (!rlang::is_null(target_days)){
y_max <- max(select(dataframe, !! y_upr))*1.1
day_2 <- target_days$day_2
day_3 <- target_days$day_3
p <- p +
annotate(geom = "vline",
x = c(day_2, day_3),
xintercept = c(day_2, day_3),
linetype = c("dashed", "solid")) +
annotate(geom = "text",
label = c(as.character(day_2), as.character(day_3)),
x = c(day_2, day_3),
y = c(y_max, y_max),
angle = 90,
vjust = 1)
}
return(p)
}
There are six variables which need to be generic across different dataframes, and which I capture with enquo
, then use with the !!
operator. Is this still the right procedure? Or should I do something different?
PS I didn't provide a full reprex
to keep the post short (the function works already, it's not like I need to fix a bug). However, if you prefer I can add one.