使用R在ggplot中传递字符串变量facet_wrap() [英] Passing string variable facet_wrap() in ggplot using R

查看:288
本文介绍了使用R在ggplot中传递字符串变量facet_wrap()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

$ $ $ $ $ $ c $ response $ lt; $ job

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

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

它给出了需要的绘图

但是当我指定响应变量in facet_wrap()

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

我得到错误

 至少有一个图层必须包含用于构建切面的所有变量

有没有办法,facet_wrap可以从响应变量接受变量名,而不是直接写变量名。

@ kohske的评论,以便它可以被接受和关闭):

  facet_wrap(as.formula(paste(〜,response)))


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

解决方案

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

facet_wrap(as.formula(paste("~", response)))

这篇关于使用R在ggplot中传递字符串变量facet_wrap()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆