我正在绘制一个分类变量,而不是显示每个类别值的计数.
我正在寻找一种方法来ggplot显示该类别中的值的百分比.当然,有可能用计算的百分比创建另一个变量并绘制一个变量,但我必须做几十次,我希望在一个命令中实现它.
我正在尝试类似的东西
qplot(mydataf) +
stat_bin(aes(n = nrow(mydataf), y = ..count../n)) +
scale_y_continuous(formatter = "percent")
Run Code Online (Sandbox Code Playgroud)
但我必须错误地使用它,因为我有错误.
为了轻松重现设置,这里有一个简化的例子:
mydata <- c ("aa", "bb", NULL, "bb", "cc", "aa", "aa", "aa", "ee", NULL, "cc");
mydataf <- factor(mydata);
qplot (mydataf); #this shows the count, I'm looking to see % displayed.
Run Code Online (Sandbox Code Playgroud)
在实际情况中,我可能会使用ggplot而不是qplot,但使用stat_bin的正确方法仍然无法使用.
我也试过这四种方法:
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent');
ggplot(mydataf, aes(y = (..count..)/sum(..count..))) +
scale_y_continuous(formatter = 'percent') + geom_bar();
ggplot(mydataf, aes(x = levels(mydataf), y = …Run Code Online (Sandbox Code Playgroud)