2017-06-13 573 views
0

我的實際數據集非常大,需要一些時間來處理它。於是我寫了一個C程序來計算每個可能值的頻率。 (也就是說,在數據集中的可能值是0,1,2,3)所以我有(用於演示的緣故)的頻率分佈如下:只有頻率時用ggplot2繪製直方圖

0.1 0.4 0.3 0.2 

如果我使用geom_histogram養活這些數據來ggplot2,我沒有得到正確的直方圖。那麼如何繪製一個具有上述頻率分佈的直方圖呢?

+2

可能嘗試'geom_col'初學者。 – joran

回答

3

您需要在geom_bar調用中使用stat = 'identity'

library(ggplot2) 

dat <- data.frame(x = c(0, 1, 2, 3), y = c(0.1, 0.4, 0.3, 0.2)) 

ggplot(dat) + 
    geom_bar(mapping = aes(x = x, y = y), stat = "identity") 

enter image description here

+0

迴應上面的@ joran的評論,'geom_col'避免輸入'stat =「identity」' – bouncyball

+0

是的,'geom_col'是一個非常有用的圖層。請注意,它是在ggplot2版本2.2.0中引入的。 – Peter

+0

x軸標籤沒有意​​義 – Al14

0

我沒有創建一個額外的數據幀的方法。在x軸上,你可以找到你的頻率數

library(ggplot2) 
x<-c(0.1, 0.4, 0.3, 0.2) 
ggplot(data.frame(x), aes(y=x, x=1:length(x)))+ 
    geom_bar(stat = "identity")