J Walt
J Walt

Reputation: 141

R GGPLOT2 lapply and function not finding object?

I hope I can get a contextual clue as to what may be wrong here without providing data frame, but can if necessary, but ultimately I want to utilize lapply to create multiple boxplots across multiple Ys and same X, but get the following error, but Termed is definitely in my CMrecruitdat data.frame:

Error in aes_string(x = Termed, y = RecVar, fill = Termed) : object 'Termed' not found

RecVar <- CMrecruitdat[,c("Req.Open.To.System.Entry", "Req.Open.To.Hire", "Tenure")]

BP <- function (RecVar){
  require(ggplot2)
  ggplot(CMrecruitdat, aes_string(x=Termed, y=RecVar, fill=Termed))+
     geom_boxplot()+
     guides(fill=false)
}

lapply(RecVar, FUN=BP)

Upvotes: 0

Views: 138

Answers (1)

MrFlick
MrFlick

Reputation: 206586

If you use aes_string, you should pass strings rather than vectors and use strings for all your fields.

RecVar <- CMrecruitdat[,c("Termed", "Req.Open.To.System.Entry", "Req.Open.To.Hire", "Tenure")]

BP <- function (RecVar){
  require(ggplot2)
  ggplot(RecVar, aes_string(x="Termed", y=RecVar, fill="Termed"))+
     geom_boxplot()+
     guides(fill=false)
}

lapply(names(RecVar), FUN=BP)

Upvotes: 1

Related Questions