2017-10-18 80 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