48

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

4
  • 3
    Could you make your example reproducible? Commented Jun 14, 2012 at 7:27
  • 38
    try as.formula(paste("~", response)) Commented Jun 14, 2012 at 7:42
  • See here as well: stackoverflow.com/questions/11028353/… Commented Jan 14, 2014 at 11:49
  • 1
    @PaulLemmens Your link just refers to this question again. Commented Feb 23, 2014 at 7:01

1 Answer 1

91

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

facet_wrap(as.formula(paste("~", response)))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.