[R] visualizing frequencies

Gabor Grothendieck ggrothendieck at gmail.com
Sun Jun 26 15:16:09 CEST 2005


On 6/26/05, Mag. Ferri Leberl <ferri.leberl at gmx.at> wrote:
> Dear everybody,
> 
> In our game-theory lesson we have run several classroom-experiments where the
> students had to decide for a natural number between one and seven. I have
> troubles now to visualize the results: be a the vector of answers.
> 
> hist(a) will not assume natural numbers as answers, but rational. It will make
> the brakes exactly at the natural numbers,  which is difficult to interpret,
> as only natural numbers may be employed.
> 
> barplot(a) or barplot(a,1:7) will not aggregate the answers. If three students
> returned the number seven, it will show three bars to the size of seven
> instead one bar to the size of three on index seven.
> 
> barplot(1:7,a), in this case, will show the bar at index seven, wut it will be
> to the hight of seven and to the width of three.
> 
> I also wanted to show the results of the different versions of the experiment
> in ONE plot. As the number of participants varied I googled around in the
> R-Archives and got to recognize plot.edf. As a pitty, this function seems to
> set the index the wrong way round: the function starts with the number of
> students deciding for seven, but indexes them with one.
> 

Make sure that the data are factors so that numbers with 0 frequency still
show up and then tabulate frequencies using 'table'.

# test data
x1 <- c(1, 1, 1, 2, 5, 6)
x2 <- c(1, 3, 4, 5, 4)

# tabulate frequencies ensuring that 0 frequencies are included
x1t <- table(factor(x1, lev = 1:7))
x2t <- table(factor(x2, lev = 1:7))

# plot.table of x1t, barplot of x1t and barplot of both
plot(x1t)
barplot(x1t)
barplot(rbind(x1t, x2t),beside = TRUE)




More information about the R-help mailing list