如何用ggplot绘制矢量的直方图/频率计数? [英] How to plot histogram/ frequency-count of a vector with ggplot?

查看:789
本文介绍了如何用ggplot绘制矢量的直方图/频率计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 ggplot 来从数值向量中绘制值的频率。使用 plot()非常简单,但我无法通过 ggplot 获得相同结果。

I want to plot with ggplot the frequency of values from a numeric vector. With plot() is quite straight forward but I can't get the same result with 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)

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

我应该为 ggplot()创建一个数据框来绘制我的向量吗?

Should I create a dataframe for ggplot() to plot my vector?

推荐答案

请查看帮助页面?geom_histogram 。从第一个例子中,你可能会发现这是有效的。

Please look at the help page ?geom_histogram. From the first example you may find that this works.

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

请看?ggplot 。你会发现数据必须是 data.frame

Also look at ?ggplot. You will find that the data has to be a data.frame

这篇关于如何用ggplot绘制矢量的直方图/频率计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆