CDa*_*Dav 3 r scale fill facet-wrap pie-chart
进一步回答上一个问题如何对大量数据进行子集或聚合,以便我可以制作单独的饼图
我已经成功地制作了下面的饼图,这很棒,但是有人知道我可以根据 Trip_Set 填充饼图的方法吗?即,大多数 Trip_Sets 只有一名男性或一名女性,因此它们应该完全是粉红色或蓝色的。因为其中一个有 3 (271_6),所以将其(最高的)作为默认值,以便其他饼有空白空间。我知道,如果我在条形图中这样做,我可以添加scales = "free",但这不适用于非笛卡尔坐标系统......
这是我的数据:
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
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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)
Run Code Online (Sandbox Code Playgroud)

看看这是否是您正在寻找的:
# 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)
Run Code Online (Sandbox Code Playgroud)

如果你不需要坐标等,你可以尝试下面的代码
# 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")
Run Code Online (Sandbox Code Playgroud)