Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

faceted piechart with ggplot2

I've done a faceted piechart with ggplot2 like this:

qplot(x=factor(1), data=mtcars, fill=factor(cyl)) + 
    geom_bar(width=1) +  
    coord_polar(theta="y") + 
    facet_grid(~gear)

enter image description here

but as all the piecharts share the y axis scale, some of them doesn't cover the full circle. I've tried with facet_grid(~gear, scales="free") but it doesn't work.

How could I get full circles for all the piecharts?

like image 897
Alfredo Sánchez Avatar asked Dec 11 '22 11:12

Alfredo Sánchez


1 Answers

I think you just want position = 'fill':

ggplot(mtcars,aes(x = factor(1),fill=factor(cyl))) + 
    facet_wrap(~gear) + 
    geom_bar(width = 1,position = "fill") + 
    coord_polar(theta="y")

For future reference, from the Details section of geom_bar:

By default, multiple x's occuring in the same place will be stacked a top one another by position_stack. If you want them to be dodged from side-to-side, see position_dodge. Finally, position_fill shows relative propotions at each x by stacking the bars and then stretching or squashing to the same height.

like image 72
joran Avatar answered Dec 22 '22 00:12

joran