I have following simple function but its ggplot command does not work. The command works all right when given from command line:
> testfn <- function(gdf, first, second){
library(ggplot2)
print(ggplot(gdf, aes(first, second)) + geom_point())
}
>
> testfn(mydataf, vnum1, vnum2)
Error in eval(expr, envir, enclos) : object 'second' not found
>
> ggplot(mydataf, aes(vnum1, vnum2)) + geom_point()
> (plots graph without any error)
I tried to use aes_string
instead of aes
; and also using x=first, y=second
. Things improve and one point is plotted! X and Y axes show numbers related to that point as the label. Only the first row is being plotted. Where is the problem. Thanks for your help.
dim(gdf)
. How many points do you think your x,y-series have? Also show usdput(gdf)
, or at least truncate out a snippet of the x,y-series of say length 10. – Transducerggplot(gdf, aes(first, second))
would work fine. But inside your function, you're also passing infirst,second
as string variables. So yes you would needaes_string(first,second)
inside your function testfn, since you're now passing variable names indirectly, through the string-variablesfirst,second
. – Transducerggplot()
command working on the command-line, before you then try to wrap it inside a function. Make your debugging life easy. Don't try to solve five problems at once. – Transducer