x 中每个组的 geom_bar 百分比

uma*_*ani 5 r ggplot2

当我尝试计算geom_barwith 中的观察百分比时y = (..count..)/sum(..count..)*100,它没有提供x. 下面是一个例子:

ggplot(data = mtcars)+
  geom_bar(aes(x = factor(cyl), 
               y = (..count..)/sum(..count..)*100,
               fill = factor(gear)),
           position = "dodge")  
Run Code Online (Sandbox Code Playgroud)

此处,给定因子 (cyl) 的所有条形的百分比总和不是 100%;相反,当您添加所有组中所有条形的百分比时,您会达到 100%。例如,对于factor(cyl)==4,比例是 1/11、8/11 和 2/11:9% +73% + 18% = 100%,但是上面的代码显示了百分比:3.12%、25% 和 6.25%(总和不等于组的 100%)。如何让它按组显示百分比?