2013-09-26 47 views
3

我想用ggplot繪製來自數值向量的值的頻率。與plot()是相當直接,但我不能與ggplot得到相同的結果。如何使用ggplot繪製矢量的直方圖/頻率計數?

library(ggplot2)  
dice_results <- c(1,3,2,4,5,6,5,3,2,1,6,2,6,5,6,4)  
hist(dice_results) 

enter image description here

ggplot(dice_results) + geom_bar() 
# Error: ggplot2 doesn't know how to deal with data of class numeric 

我應該創建一個ggplot()數據框繪製矢量我?

回答

8

請看幫助頁?geom_histogram。從第一個例子中,你可能會發現這是有效的。

qplot(as.factor(dice_results), geom="histogram") 

也期待在?ggplot。你會發現,數據必須是一個data.frame

7

嘗試以下

library(ggplot2)  
dice_results <- c(1,3,2,4,5,6,5,3,2,1,6,2,6,5,6,4,1,3,2,4,6,4,1,6,3,2,4,3,4,5,6,7,1) 
ggplot() + aes(dice_results)+ geom_histogram(binwidth=1, colour="black", fill="white") 
代碼