Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing string variable facet_wrap() in ggplot using R [duplicate]

Tags:

r

ggplot2

I have a defined a variable named response. this variable will be passed to facet_wrap() in ggplot package

 response<-"job" 

When i specify variable directly in facet_wrap()

e.g

   ggplot(data,aes(job,fill=class )) + geom_bar() +facet_wrap(~job) 

it gives required plot

But when i specifying response variable in facet_wrap()

 ggplot(data,aes(job,fill=reponse))+ geom_bar() +  facet_wrap(~get(paste(response))) 

i get error

  At least one layer must contain all variables used for facetting 

Is there way where facet_wrap can accept variable name from response variable instead writing variable name directly in it

like image 937
Sunny Sunny Avatar asked Jun 14 '12 07:06

Sunny Sunny


1 Answers

(Turning @kohske's comment into an answer so that it can be accepted and "closed"):

facet_wrap(as.formula(paste("~", response))) 
like image 180
Brian Diggs Avatar answered Oct 02 '22 13:10

Brian Diggs