library(dplyr) #Devel version, soon-to-be-released 0.6.0
library(tidyr)
library(ggplot2)
library(forcats) #for gss_cat data
我试图写一个从即将要发布的dplyr devel的版本tidyr组合在一起quosures功能使用开发人员版本dplyr的enquo和quo_name的一个功能:: gather和ggplot2。到目前为止,它似乎与tidyr一起工作,但我在绘图时遇到了麻烦。如何使用tidyr和GGPLOT2
下面的功能似乎与tidyr的收集工作:
GatherFun<-function(gath){
gath<-enquo(gath)
gss_cat%>%select(relig,marital,race,partyid)%>%
gather(key,value,-!!gath)%>%
count(!!gath,key,value)%>%
mutate(perc=n/sum(n))
}
但我不能想出如何使该地块的工作。我尝试用ggplot2使用!! gath,但它不起作用。
GatherFun<-function(gath){
gath<-enquo(gath)
gss_cat%>%select(relig,marital,race,partyid)%>%
gather(key,value,-!!gath)%>%
count(!!gath,key,value)%>%
mutate(perc=n/sum(n))%>%
ggplot(aes(x=value,y=perc,fill=!!gath)+
geom_col()+(facet_wrap(~key, scales = "free") +
geom_text(aes(x = "value", y = "perc",
label = "perc", group = !!gath),
position = position_stack(vjust = .05))
}
我要回答的更近的问题适用于:使用dplyr SE与GGPLOT2(https://stackoverflow.com/questions/45279287/use-dplyr-se-with-ggplot2/47044567#47044567)。简而言之,我建议你使用aes_(x =〜value,y =〜perc,fill = gath)。当你已经有quards在使用时,语法比使用aes_string更清晰。 – Stanwood