percentage on y lab in a faceted ggplot barchart?

前端 未结 6 1329
孤独总比滥情好
孤独总比滥情好 2020-12-01 00:56

doing facets in ggplot I would often like the percentage to be used instead of counts.

e.g.

test1 <- sample(letters[1:2], 100, replace=T)
test2 &         


        
6条回答
  •  攒了一身酷
    2020-12-01 01:27

    Here is a within ggplot method, using ..count.. and ..PANEL..:

    ggplot(test, aes(test2)) + 
        geom_bar(aes(y = (..count..)/tapply(..count..,..PANEL..,sum)[..PANEL..])) + 
        facet_grid(~test1)
    

    As this is computed on the fly, it should be robust to changes to plot parameters.

提交回复
热议问题