Desktop Survival Guide
by Graham Williams


A barplot displays data as bars, each bar being proportional to the data being plotted. In R a barplot is built using the barplot function. We can use a barplot, for example, to illustrate the distribution of entities in a dataset across some variable. With the wine dataset Type is a categoric variable with three levels: 1, 2, and 3. A simple bar plot illustrates the distribution of the entities across the three Types. The summary function is used to obtain the data we wish to plot (59, 71, and 48).

We place the actual counts on the plot with the text function. The trick here is that the barplot function returns the bar midpoints, and these can be used to place the actual values. We add $2$ to the Roption[]y values to place the numbers above the bars. Also note that Roption[]xpd is set to TRUE to avoid the highest number being chopped (because it, 71, is actually outside the plot region).

bp <- barplot(summary(Type), xlab="Type", ylab="Frequency")
text(bp, summary(Type)+2, summary(Type))

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010