My data looks like this:
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
As you can see, each Trip_Set has a number of males or females (some just have one). I wish to make a separate pie chart for each trip set to show the ratio of males to females for each. This is just a snippet of my dataset (the real one is much larger with hundreds of Trip_Sets).
My code for the pie chart is this:
data = dframe2
library(ggplot2)
pie <- ggplot(dframe2, aes(x=1, y=Trip_Set, fill=sex)) +
geom_bar(stat="identity") +
ggtitle("Sex ratio per line")
pie <- pie + coord_polar(theta='y')
print(pie)
However, this just plots a pie chart of all of the data shown above- I wish to be able to make one pie chart for each Trip_Set. I have managed to do this manually by creating a subset and then running the piechart code using the code below:
Trip119_4<-subset(dframe2,Trip_Set=='119_4')
pie <- ggplot(Trip119_4, aes(x=1, y=Trip_Set, fill=sex)) +
geom_bar(stat="identity") +
ggtitle("Sex ratio per line")
pie <- pie + coord_polar(theta='y')
print(pie)
Is there a way I can do separate pie charts for each Trip_Set, without having to type in and rename hundreds of repeated subsets? Someone mentioned an aggregate function but for the life of me I cannot get it to work.
You may want to look into
facet_wrap
-- I think the following is the most effective if I understand the question correctly:You could also structure it this way: