I'm using aes_string()
in a function to create some graphs. I'm using cowplot for theming and this scales the axes to the maximum values of the data provided, cutting off the top or side of the points at the maximum as in the example below.
I therefore want to add 5% to the max data for the column to add a little space. If I were not writing a function I could do something like
scale_y_continuous(expand = c(0,0),
limits = c(0, max(y_var) * 1.05))
However, I don't know how to do this with aes_string()
. Can anyone explain how to do this with aes_string()
?
library(cowplot)
library(ggplot2)
fig_fun <- function(data, var){
ggplot(data, aes_string(x = "wt", y = var)) +
geom_line() +
geom_point() +
scale_y_continuous(expand = c(0,0),
limits = c(0, NA))
}
data("mtcars")
p <- fig_fun(data = mtcars, var = "mpg")
p