r - 如何在不使用 scale = free 的情况下在 facet wraps 中填充整个饼图

标签 r scale pie-chart fill facet-wrap

关于上一个问题 How to subset or aggregate large amounts of data so I can make separate pie charts

我已经制作了下面的饼图,这很棒,但是有人知道我可以根据 Trip_Set 填充饼图的方法吗?即,大多数 Trip_Set 只有一只雄性或一只雌性,因此它们应该完全是粉红色或蓝色。因为一个有 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

这是我的代码:

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) 

enter image description here

最佳答案

看看这是否是您要找的:

# 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)

enter image description here

如果你不想要坐标等,你可以试试下面的代码

# 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")

关于r - 如何在不使用 scale = free 的情况下在 facet wraps 中填充整个饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27446511/

相关文章:

r - 错误在R中找不到函数 “pull”

android - 如何根据设备屏幕尺寸将百分比相对布局中的 ImageView 设置为以下比例,16 :9, 1 :1, 3 :2, 2 :1, 4:3

python - 如何从 tkinter 比例小部件显示中删除比例值?

r - 创建一个新列,其公式取决于另一行的单元格值

r - 如何合并 R 中的两个日期向量?

r - 应用、数据框和 bool 值不能一起工作?

html - 在类 :hover 中使用纯 css 调整背景图像的大小

r - 如何从具有相对频率的表中绘制 R 中的饼图?

graph - 极地面积图

javascript - 使用 Highcharts 在饼图中对图例进行分组