2017-10-18 110 views
1

分组我目前正在读R for Data Science并试图创造一些图表。我知道要在条形图中获得比例,您需要使用group = 1。例如,下面的代码工作:绘图比例另一个变量

library(ggplot2) 

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color)) 

但我没有得到相同的比例图。

ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = color, y = ..prop.., group = 1)) 

我确实得到了比例,但不是color

+1

你不需要'tidyverse'软件包这一 –

+1

'diamonds'是ggplot2'的'一部分,你唯一需要的包为了这。 – Gregor

回答

1

这里有一个办法做到这一点使用..count..

require(ggplot2) 

ggplot(diamonds,aes(cut,..count../sum(..count..),fill=color))+ 
    geom_bar()+ 
    scale_y_continuous(labels=scales::percent) 

enter image description here