Suppose I have the following function:
my_function <- function(date1, date2, variable, quota, monthly_business_days) {
value <- deparse(substitute(variable))
my_data %>%
filter(between(DATE, ymd(date1), ymd(date2))) %>%
summarize(total = sum()) %>%
add_row(total = quota, .before = 1) %>%
rbind(.$total[[2]]/bizdays(date1, date2)*monthly_business_days) %>%
mutate(indicator = if(value == 'UNITS') c("Quota (Units)", "Sales (Units)", "Forecast (Units)")
else c("Quota (USD)", "Sales (USD)", "Forecast (USD)")) %>%
mutate(indicator = as_factor(indicator)) %>%
mutate(across(where(is.numeric), ~round(., 1))) %>%
ggplot(aes(x = indicator, y = total)) +
geom_col(fill = "aquamarine3", color = "black") +
geom_label(aes(label = total), size = 8) +
if(value == UNITS) {
labs(x = "", y = "Units") +
} else if(value == SALES) {
labs(x = "", y = "USD") +
} else {
labs(x = "", y = "") +
}
theme(axis.text = element_text(size = rel(1)))
}
I would like to include labels in my plot from ggplot2 depending on the function's input. Please bear in mind that this input is being used to select a specific column from "my_data" data frame. I'm not sure whether to use "variable" or "value". None of those seem to work.
Thanks!
Aucun commentaire:
Enregistrer un commentaire