2017-03-01 98 views
1

我有一个数据帧:标准化条形图

df<-data.frame(Pet=rep(c("Dog", "Cat", "Bird"), c(5,10,15)), Gen=rep(c("M", "F", "M", "F", "M", "F"), c(3,5,12,5,3,2))) 

正如我想象的男性的频率/女各动物我得到这个图:

ggplot(df, aes(Pet, group=Gen, fill=Gen)) + geom_bar(position="dodge", width=.5) 

如何使一个图表,会有相同高度的女性酒吧和相对高度与相应女性酒吧的男性酒吧?

事情是这样的: Something like this

回答

1

一个简单的解决将是首先标准化数据,然后做的情节:

t = table(df) 
as.data.frame.table(t/t[,'F']) %>% 
    ggplot(aes(x=Pet, y=Freq, group=Gen, fill=Gen)) + 
    geom_bar(position="dodge", width=.5, stat="identity") 

enter image description here