使用R在ggplot中传递字符串变量facet_wrap()

Sun*_*nny 34 r ggplot2

我有一个名为response的变量.这个变量将传递给ggplot包中的facet_wrap()

 response<-"job"
Run Code Online (Sandbox Code Playgroud)

当我直接在facet_wrap()中指定变量时

例如

   ggplot(data,aes(job,fill=class )) + geom_bar() +facet_wrap(~job)
Run Code Online (Sandbox Code Playgroud)

它给出了必要的情节

但是当我在facet_wrap()中指定响应变量时

 ggplot(data,aes(job,fill=reponse))+ geom_bar() +  facet_wrap(~get(paste(response)))
Run Code Online (Sandbox Code Playgroud)

我收到错误

  At least one layer must contain all variables used for facetting
Run Code Online (Sandbox Code Playgroud)

是否有方法facet_wrap可以从响应变量接受变量名称,而不是直接在其中编写变量名称

Bri*_*ggs 62

(将@ kohske的评论转化为答案,以便可以接受并"关闭"):

facet_wrap(as.formula(paste("~", response)))
Run Code Online (Sandbox Code Playgroud)