I am trying to write a function that seems like it should be very simple but I am having problems with it. I want to write a function that takes in three arguements: a dataframe, x-axis variable and y-axis variable. Based on these, I want it to return a scatterplot in which the x-axis variable and y-axis variable can be changed. This is the very basic function I wrote:
scatter_plot <- function(dataframe, x_input, y_input) {
plot <- ggplot(data = dataframe) +
geom_point(mapping = aes(x = x_input, y = y_input),
)
}
For reproducibility, consider the dataset midwest
that is in the ggplot2
package. The code I wrote does not produce errors when I run it, but when I try to pass arguments into it, such as
scatter_plot(midwest, percollege, percpovertyknown)
the function returns
"Error in FUN(X[[i]], ...) : object 'percollege' not found"
It seems like it does not recognize the variables in the argument, but I have been playing around with the function for quite some time and I can't seem to figure it out. Can someone help me with how to fix this so my function works correctly?