I am trying to write a function that creates plots and ran into an error that I don't understand. Here is a simplified example.
Reproducible example:
library (ggplot2)
# Sample data
xdata = 0:20
ydata = 20:40
dat <- data.frame(xdata, ydata)
# This works
line_p <- ggplot(dat, aes(x = xdata, y = ydata, group = NULL, color = NULL)) + geom_line()
line_p
I expected the following to work, but get an aesthetics error, but in this case the x and y variables are the same length. The issues seems to be having a default value of NULL for group and color. I tried explicitly passing NULL as well with aes_group and aes_color as function variables but this also did not work.
# Using a function doesn't work:
# create function
line_function <- function(mydata = dat,
xinput = x,
yinput = y,
aes_group = NULL,
aes_color = NULL,
...) {
lineplot <- ggplot(dat, aes(x = xinput, y = yinput, group = aes_group, color = aes_color)) + geom_line()
}
# test the function
line_test_p <- line_function(
mydata = dat,
xinput = xdata,
yinput = ydata
)
line_test_p
Testing with explicit inputs
# test the function again with explicit NULL inputs
line_test2_p <- line_function(
mydata = dat,
xinput = xdata,
yinput = ydata,
aes_group = NULL,
aes_color = NULL
)
line_test2_p
Is it not possible to write a generic function where ggplot will interpret the NULL values as in the example that works without a function, or am I missing something else?
Thanks!