[R] plot error

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Sep 23 12:40:47 CEST 2008

2008/9/23 glaporta <glaporta at freeweb.org>:
> HI there,
> why these lines of code are correct
> plot(count~spray, data = InsectSprays)
> plot(InsectSprays$count)
> but this return an error:
> plot(count, data = InsectSprays);
> "data" method is not implemented in plot?!

 The 'plot' function, like many functions in R, does different things
depending on what its first parameter is...

 For plot(count~spray, data=InsectSprays) the first parameter is a
formula, so what is really called is 'plot.formula'. This gets the
names from the formula (count~spray) finds them in the 'data'
parameter and does a plot.

 For plot(InsectSprays$count) you've taken the 'count' vector out of
the data frame, so the ordinary plot function is called.

 If you do plot(count, data=InsectSprays), then 'count' here is just a
bare word, so R looks up 'count' as an object, but doesn't find it (as
long as there's no 'count' object in your workspace).

 You can do plot(~count, data=InsectSprays), but you get a slightly
different plot than plot(count$InsectSprays). Compare and contrast the

 > is=data.frame(count=runif(10),spray=runif(10))
 > plot(count,data=is)
 Error in plot(count, data = is) : object "count" not found
 > plot(~count,data=is)
 > plot(count~1,data=is)
 > plot(is$count)


More information about the R-help mailing list