1
votes

I am looking for advice on the best way to represent the frequency counts for the range in a column from my dataframe.

Example:

my_table<- data.frame('xcat' = c(1,1,1,2,2,2,5,10,10,10,11,11,11,14,14,14,
                             15,15,15, 17,17,17, 18,18,18,20,20,20))

In the above dataframe, I want to plot the frequencies from 1 to 20. One way to do it would be using hist(), setting bin size to 1, i.e.:

my_hist<- hist(my_table$xcat,
   breaks=seq(from = 0.5, to= (max(my_table$xcat)+0.5),
                               by =1))

As you can see, I have added an offset of 0.5, so each bin will be showed on the correspondent integer. Another way to do it would be to create a table with the counts, then generate intervals:

my_tablecut = cut(my_table$xcat, breaks= seq(from =0.5, to =20.5,
                                         by = 1), right=FALSE)
my_tablefreq = as.data.frame(table(my_tablecut))
my_tablefreq$pos<- 1:nrow(my_tablefreq)
plot(my_tablefreq$pos, my_tablefreq$Freq, type = 'l')

As I am writing, I realize it may be more correct to use a bar chart, rather than a histogram; in this case though, I would still need to generate the categories with frequency 0, probably still using the 'cut' function, and then going for 'barplot' instead of 'plot'.

I wonder if there is any downside to any of this methods, or what would be a more proper way to show what I want. Please let me know if my purpose is not clear, or I need to add more details.

2

2 Answers

2
votes
> table(my_table)
my_table
 1  2  5 10 11 14 15 17 18 20 
 3  3  1  3  3  3  3  3  3  3 

The downside is that table does not allow any adjustment of the bin size. In your case the fit with requirements is perfect, since you asked for a bin size of 1. cut allows the specification of bin-boundaries and even lets you choose whether it is the left or right boundary that is close (or open). My preference is for left-closed which is not the default.

hist returns the breaks and the counts in the breaks( and even more) while barplot just plots:

> my_hist
$breaks
 [1]  0.5  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.5 11.5 12.5 13.5 14.5 15.5 16.5
[18] 17.5 18.5 19.5 20.5

$counts
 [1] 3 3 0 0 1 0 0 0 0 3 3 0 0 3 3 0 3 3 0 3

$density
 [1] 0.10714286 0.10714286 0.00000000 0.00000000 0.03571429 0.00000000 0.00000000 0.00000000
 [9] 0.00000000 0.10714286 0.10714286 0.00000000 0.00000000 0.10714286 0.10714286 0.00000000
[17] 0.10714286 0.10714286 0.00000000 0.10714286

$mids
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

$xname
[1] "my_table$xcat"

$equidist
[1] TRUE

attr(,"class")
[1] "histogram"
1
votes

I prefer to use this:

plot(density(my_table$xcat))

enter image description here

This also makes it easier to overlay other frequencies, e.g.:

my_table$xcatNew <- c(1,1,1,1,1,1,1,1,1,1,11,12,14,14,14,14,
                      15,15,15, 17,17,17, 18,18,18,18,20,20)

plot(density(my_table$xcat))
lines(density(my_table$xcatNew), col = "red")

enter image description here