More With Tables

Tables can also be used to support graphics in R. A traditional bar plot, showing the total count for each variable, is most easily made by plotting a one dimensional table.

Using the baseball player data from before:

barplot(table(tab$position))

positioncount

If you make a plot of a table that has two or more dimensions you get a mosaic plot, which is very difficult to read.

Tables can also be made multidimensional with the ftable() function, short for “flat table”. Like addmargins() and prop.table() this function only works when applied to an existing table. Here is a three dimensional flat table using the carstuff data.

t <- with(carstuff,(table(safety,class,buying)))
ftable(t)
             buying vhigh high med low
safety class                          
high   unacc           98   75  52  52
       acc             46   69  56  33
       good             0    0  10  20
       vgood            0    0  26  39
med    unacc          118  105  72  62
       acc             26   39  59  56
       good             0    0  13  26
       vgood            0    0   0   0
low    unacc          144  144 144 144
       acc              0    0   0   0
       good             0    0   0   0
       vgood            0    0   0   0

Without using ftable() you still get a similar output from the table, but in a form that’s extremely difficult to read.

Leave a comment