r - ggplot: relative frequencies of two groups -


i want plot except each facet sums 100%. right group m 0.05+0.25=0.30 instead of 0.20+0.80=1.00.

df <- rbind(     data.frame(gender=c(rep('m',5)), outcome=c(rep('1',4),'0')),     data.frame(gender=c(rep('f',10)), outcome=c(rep('1',7),rep('0',3))) )  df  ggplot(df, aes(outcome)) +     geom_bar(aes(y = (..count..)/sum(..count..))) +     facet_wrap(~gender, nrow=2, ncol=1)  

(using y = ..density.. gives worse results.)

i precalculating values outside of ggplot2 , using stat = "identity":

df1 <- melt(ddply(df,.(gender),function(x){prop.table(table(x$outcome))}),id.vars = 1)  ggplot(df1, aes(x = variable,y = value)) +     facet_wrap(~gender, nrow=2, ncol=1) +     geom_bar(stat = "identity") 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -