<facet_wrap(vars()) or facet_wrap(~)>
> ggplot(data=UCBA,aes(x=Freq,y=Dept,fill=Gender)) +geom_bar(stat="identity",position="dodge") |
> ggplot(data=UCBA,aes(x=Freq,y=Dept,fill=Gender)) +geom_bar(stat="identity",position="dodge") +facet_wrap(vars(Gender)) |
|
|
<EXAMPLE 1>
> table(mpg$class)
2seater compact midsize minivan pickup subcompact
5 47 41 11 33 35
suv
62
> mpg_<-mpg%>%filter(class=="compact"|class=="suv")%>%group_by(manufacturer)%>%mutate(mean_cty=mean(cty))
> table(mpg2$class)
compact suv
47 62
> ggplot(data=mpg_,aes(x=mean_cty,y=manufacturer,fill=class))+geom_bar(stat="identity",position="dodge")
> ggplot(data=mpg_,aes(x=mean_cty,y=manufacturer,fill=class))+geom_bar(stat="identity",position="dodge")+facet_wrap(vars(class))
<EXAMPLE 2>
> View(UCBAdmissions)
> UCBA<-as.data.frame(UCBAdmissions)
> ggplot(data=UCBA,aes(x=Freq,y=Dept,fill=Gender))+geom_bar(stat="identity")
> ggplot(data=UCBA,aes(x=Freq,y=Dept,fill=Gender))+geom_bar(stat="identity",position="dodge")
> ggplot(data=UCBA,aes(x=Freq,y=Dept,fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(vars(Gender))
<EXAMPLE 3>
> ggplot(data=UCBA,aes(x=Freq,y=interaction(Admit,Dept),fill=Gender))+geom_bar(stat="identity",position="dodge")
> ggplot(data=UCBA,aes(x=Freq,y=interaction(Admit,Dept),fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(vars(Gender))
<EXAMPLE 4>
> ggplot(data=UCBA,aes(x=Freq,y=Admit,fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(~Dept)
> ggplot(data=UCBA,aes(x=Freq,y=Admit,fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(~Dept)+geom_text(aes(label=Freq),position=position_dodge(0.9))
> ggplot(data=UCBA,aes(x=Freq,y=Admit,fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(~Dept)+geom_text(aes(label=Freq),position=position_dodge(0.9),size=3)
> ggplot(data=UCBA,aes(x=Freq,y=Admit,fill=Gender))+geom_bar(stat="identity",position="dodge")+facet_wrap(~Dept)+geom_text(aes(label=Freq),position=position_dodge(0.9),hjust=-0.2,size=3)