Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R histogram from frequency table

Tags:

r

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

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.

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: enter image description here

However, converting the data to a vector format would make much more sense using this code hist(as.vector(rep(dt$val, dt$freq))): enter image description here

like image 787
praks5432 Avatar asked Mar 14 '14 00:03

praks5432


4 Answers

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; I will assume it is 10.

The solution provided by user2030503 is somewhat wasteful, as it re-creates the data set from the frequency table. 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.

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; I just put it for the sake of generality.

like image 171
peterlin Avatar answered Oct 17 '22 04:10

peterlin


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)

enter image description here

like image 38
user2030503 Avatar answered Oct 17 '22 03:10

user2030503


You can simply do

myfreq=table(df$columnofinterest)
plot(myfreq)
like image 2
Herman Toothrot Avatar answered Oct 17 '22 03:10

Herman Toothrot


I think it should be a barplot, like so:

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

like image 2
wordsforthewise Avatar answered Oct 17 '22 02:10

wordsforthewise