库(Ggmosaic)库(Tidyverse)
我仍然在尝试用标准评估来学习编程的一些细微差别。对于这个问题,我尝试使用ggmosaic中的"happy“数据集创建一个函数,以创建变量"health”和"happy“的基本马赛克图。
下面是我想用来创建一个使用标准求值的函数的代码。我希望能够从happy数据集中输入任意两个分类变量,并创建一个基本的马赛克图,如代码中所述。
happy%>%
na.omit()%>%
count(happy,health)%>%
ggplot()+
geom_mosaic(aes(weight=n,x=product(health),fill=health))但是,我不能完全正确地理解代码。我以前也问过类似的问题,但我仍然在努力理解何时何地使用.dots参数,以及如何在标准计算中指定输入。下面是我使用过的代码的错误版本之一……
Mosaic<-function(product="health",fill="happy"){
happy%>%na.omit()%>%
count_(c(product,fill))%>%
ggplot()+
geom_mosaic(aes_string(weight="n",x=product(product),fill=fill))
}任何能让它正常工作的建议都将不胜感激,特别是关于如何用SE正确编码的建议。
发布于 2017-01-28 16:41:17
你可以这样做:
Mosaic<-function(var_product="health",fill="happy"){
happy%>%
na.omit()%>%
count_(c(var_product,fill))%>%
ggplot(aes(weight=n))+
geom_mosaic(aes_string(x=paste0("product(", var_product, ")"),fill=fill))
}示例:
Mosaic("sex","degree")

https://stackoverflow.com/questions/41907182
复制相似问题