further to a previous question How to subset or aggregate large amounts of data so I can make separate pie charts
I have managed to produce the below pie charts, which is great, however does anyone know a way that I may have the pie charts filled up according to Trip_Set? i.e., most of the Trip_Sets have just one male or one female so they should be totally pink or blue. Because one has 3 (271_6), it's taken that (the highest) as default so the other pies have the empty space. I know that if I were doing this in bar charts I could add in scales = "free"
but this doesn't work with with a non-cartesian coord system...
Here is my data:
Trip_Set sex
119_4 hembra
119_4 hembra
119_7 hembra
161_7 macho
193_8 hembra
255_7 macho
271_6 hembra
271_6 macho
271_6 hembra
328_7 hembra
403_3 hembra
428_2 hembra
655_4 hembra
Here is my code:
pie <- ggplot(dframe2, aes(x=1, y='Trip_Set', fill=sex)) +
geom_bar(width = 1, stat="identity") +
ggtitle("Sex ratio per line") +
coord_polar(theta='y') +
facet_wrap(~Trip_Set, ncol = 5)
See if this is what you're looking for:
# transform the data
library(dplyr)
dframe2_new = dframe2 %>% group_by(Trip_Set, sex) %>% tally()
# to plot
ggplot(data=dframe2_new, aes(x=factor(1), y=n, fill=sex)) +
geom_bar(stat="identity", position=position_fill(width=1)) +
coord_polar(theta="y") +
facet_wrap(~Trip_Set, ncol=5)
If you don't want coordinates etc., you could try the code below
# to plot
ggplot(data=dframe2_new, aes(x=factor(1), y=n, fill=sex)) +
geom_bar(stat="identity", position=position_fill(width=1)) +
coord_polar(theta="y") +
facet_wrap(~Trip_Set, ncol=5) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank()) +
xlab("") +
ylab("Trip_Set")