2013-09-26 56 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") 
代码