简体   繁体   English

来自频率表的 R 直方图

[英]R histogram from frequency table

So I've figured out how to drill my data down to a frequency table -所以我想出了如何将我的数据下钻到频率表 -

        Overall.Cond Freq
235            1    0
236            2    0
237            3    1
238            4    1
239            5    9
240            6    1
241            7    1
242            8    1
243            9    1

I want to plot a histogram from this, but when I do hist(dataFrameName) I get this error我想从中绘制直方图,但是当我执行 hist(dataFrameName) 时出现此错误

Error in hist.default(veenker) : 'x' must be numeric

Why is that happening and how do I get around it?为什么会发生这种情况,我该如何解决?

EDIT: For those suggesting barplot as a solution (which is not the question), please consider this example of why barplot would not be a good solution.编辑:对于那些建议将barplot作为解决方案(这不是问题)的人,请考虑这个示例,说明为什么barplot不是一个好的解决方案。

This sample data这个样本数据

dt = data.frame(vals = c(1.1, 1.2, 1.3, 2.0, 3.4, 26, 35, 45),
                freq = c(  2,   3,   4,   3,   2, 15, 17, 14)) 

Using barplot(dt$freq, names.arg = dt$vals) would produce this very misleading barplot:使用barplot(dt$freq, names.arg = dt$vals)会产生这个非常具有误导性的条形图: 在此处输入图片说明

However, converting the data to a vector format would make much more sense using this code hist(as.vector(rep(dt$val, dt$freq))) :但是,使用以下代码将数据转换为矢量格式更有意义hist(as.vector(rep(dt$val, dt$freq))) 在此处输入图片说明

Do you really want a histogram or a bar chart?你真的想要直方图还是条形图? If you insist on a histogram, you are lacking the upper boundary of your topmost bin;如果您坚持使用直方图,则您缺少最顶层 bin 的上限; I will assume it is 10.我假设它是10。

The solution provided by user2030503 is somewhat wasteful, as it re-creates the data set from the frequency table. user2030503 提供的解决方案有点浪费,因为它从频率表中重新创建数据集。 Since you already have your frequency table computed, you can use it directly in construction of your histogram object.由于您已经计算了频率表,因此可以直接在构建直方图对象时使用它。 The latter is essentially a list in R.后者本质上是 R 中的一个列表。

Overall.Cond <- 1:10
Freq <- c(0,0,1,1,9,1,1,1,1)
myhist <-list(breaks=Overall.Cond, counts=Freq, density=Freq/diff(Overall.Cond),
              xname="Overall Cond")
class(myhist) <- "histogram"
plot(myhist)

As the bin width is 1, calculation of density could be simplified in this case;由于bin宽度为1,在这种情况下可以简化密度的计算; I just put it for the sake of generality.我只是为了一般性而提出它。

Rebuild your data frame:重建你的数据框:

df= as.data.frame(cbind(Overall.Cond= 1:9, Freq= c(0,0,1,1,9,1,1,1,1)))
df

Result:结果:

  Overall.Cond Freq
1            1    0
2            2    0
3            3    1
4            4    1
5            5    9
6            6    1
7            7    1
8            8    1
9            9    1

Then make a vector of observations and plot it:然后制作一个观察向量并绘制它:

df.freq= as.vector(rep(df$Overall.Cond, df$Freq))
hist(df.freq)

在此处输入图片说明

You can simply do你可以简单地做

myfreq=table(df$columnofinterest)
plot(myfreq)

我认为它应该是一个条形图,如下所示:

barplot(dt$Freq, names.arg = dt$Overall.Cond)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM