R 带计数的堆叠条形图中几何图形文本的百分比
我想有一个堆叠的条形图,里面有基于计数的百分比。 我几乎达到了我想要的,但文本中的每个值都是100%,而不是实际的百分比。。。 我想我的代码中有一个小错误,但我找不到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",
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”
。但是谢谢你!