How to subset or aggregate large amounts of data so I can make separate pie charts

非 Y 不嫁゛ 提交于 2020-01-15 12:18:05

问题


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.


回答1:


You may want to look into facet_wrap -- I think the following is the most effective if I understand the question correctly:

ggplot(data=df, aes(x=sex, fill=sex)) +
  geom_bar(stat="bin") +
  facet_wrap(~ Trip_Set)

You could also structure it this way:

ggplot(data=df, aes(x=Trip_Set, fill=sex)) +
  geom_bar(stat="bin")


来源:https://stackoverflow.com/questions/27429184/how-to-subset-or-aggregate-large-amounts-of-data-so-i-can-make-separate-pie-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!