I'm having real trouble trying to write some functions with ggplot() inside.
Here's a simplified version of what I'm trying to do. This works as expected:
testdata <- matrix(1:10,
nrow = 5)
ggplot() + geom_point(aes(testdata[,1],
testdata[,2]))
Putting the same code in a function:
mygg <- function(data){
ggplot() + geom_point(aes(data[,1],
data[,2]))
}
mygg(testdata)
Gives the error:
Error in data[, 1] : object of type 'closure' is not subsettable
And when I replace aes
with aes_string
, only the first row of my matrix is plotted.
It does work when I rename my matrix 'data', but the whole point is that I want to plot a bunch of matrices with different names.
I've done my best to search the forums, and realise that my question is basically a duplicate, but I've been completely unable to find a solution to my specific issue.