Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 带计数的堆叠条形图中几何图形文本的百分比_R_Ggplot2_Geom Bar_Geom Text - Fatal编程技术网

R 带计数的堆叠条形图中几何图形文本的百分比

R 带计数的堆叠条形图中几何图形文本的百分比,r,ggplot2,geom-bar,geom-text,R,Ggplot2,Geom Bar,Geom Text,我想有一个堆叠的条形图,里面有基于计数的百分比。 我几乎达到了我想要的,但文本中的每个值都是100%,而不是实际的百分比。。。 我想我的代码中有一个小错误,但我找不到 ggplot( mtcars, aes(fill = factor(gear), x = factor(carb)) ) + geom_bar(stat = "count", position = "fill", color = "black",

我想有一个堆叠的条形图,里面有基于计数的百分比。 我几乎达到了我想要的,但文本中的每个值都是100%,而不是实际的百分比。。。 我想我的代码中有一个小错误,但我找不到


ggplot(
  mtcars,
  aes(fill = factor(gear), 
      x = factor(carb))
) + 
  geom_bar(stat = "count", 
           position = "fill", 
           color = "black",
           width = 0.5) + 
  geom_text(aes(label = scales::percent(..prop..)), 
            position = position_fill(vjust = 0.5), 
            stat = "count") + 
  coord_flip()

建立在

您可以使用以下选项:

ggplot(
mtcars,
aes(填充=系数(档位),
x=系数(carb))
) + 
几何图形条(stat=“count”,
position=“fill”,
color=“黑色”,
宽度=0.5)+
几何图形文本(aes(标签=比例::百分比(…计数../tapply(…计数..,…x..,总和)[…x..]),
位置=位置填充(vjust=0.5),
stat=“count”)+
coord_flip()

这是否回答了您的问题?否,因为我无法使用
stat=“identity”
。但是谢谢你!