K Owen
K Owen

Reputation: 1250

ggplot iterate several columns

  lapply(7:12, function(x) ggplot(mydf)+geom_histogram(aes(mydf[,x])))

will give an error Error in [.data.frame(mydf, , x) : undefined columns selected.

I have used several SO questions (e.g. this) as guidance, but can't figure out my error.

Upvotes: 0

Views: 470

Answers (1)

jlhoward
jlhoward

Reputation: 59425

The code below works with the mtcars dataset. Just replace mtcars with mydf.

library(ggplot2)
lapply(1:3,function(i) {
  ggplot(data.frame(x=mtcars[,i]))+
    geom_histogram(aes(x=x))+
    ggtitle(names(mtcars)[i])
  })

Notice how the reference to i (the column index) was moved from the mapping argument (the call to aes(...)), to the data argument.

Your problem is actually quite subtle. ggplot evaluates the arguments to aes(...) first in the context of your data - e.g. it looks for column names in mydf. If that fails it jumps to the global environment. It does not look in the function's environment. See this post for another example of this behavior and some discussion.

The bottom line is that it is a really bad idea to use external variables in a call to aes(...). However, the data=... argument does not suffer from this. If you must refer to a column number, etc., do it in the call to ggplot(data=...).

Upvotes: 1

Related Questions