背景:我是 R 和 ggplot2 的新手,希望得到一个简单的答案,在相同的情况下可以让其他人受益。
问题:在 ggplot2 中使每个条形加起来为 100% 并在每个条形内显示百分比标签的简单方法是什么?
目标: 我想要的样子(每列总计 100%,每列内带有百分比标签)。
当前图表:这是我目前的条形图;基于“mtcars”数据集(它目前的样子)。下面我有“当前图表”:
library(ggplot2) # loads ggplot2
data <- mtcars # gets the mtcars dataset
#INFO: A simple barchart with X-axis being Nr of Cylinders, fill color indicating whether or not the car model has an Automatic gearbox..
# ..and Y-axis showing the count of cars by Nr of Cylinders
ggplot(mtcars, aes(x=cyl, fill = factor(am))) +
geom_bar() +
stat_count(geom …Run Code Online (Sandbox Code Playgroud)