by David Lillis, Ph.D.
Let’s create a simple bar chart in R using the barplot() command, which is easy to use.
First, we set up a vector of numbers. Then we count them using the table() command, and then we plot them.
The table() command creates a simple table of counts of the elements in a data set.
H <- c(2,3,3,3,4,5,5,5,5,6)
Now we count the elements using the table() command, as follows:
counts <- table(H)
2 3 4 5 6
1 3 1 4 1
Now we plot the counts.
The horizontal axis records the values in your data set, while the vertical axis gives the counts of each value. You will see that the barplot() command does not perform the count directly, so we use the table() command first.
You can plot your data directly if we omit the table() command. Now, the height of the bars matches the values in the data set. This is a useful technique if your data are already in the form of counts or if you wish to plot the magnitudes of each element.
B <- c(3, 2, 25, 37, 22, 34, 19)
Here we have one bar for each element, and the height gives the value of the element.
Now, let’s create a more complex barplot using various arguments, some of which you have already met in previous blogs.
barplot(B, main="MY NEW BARPLOT", xlab="LETTERS", ylab="MY Y VALUES", names.arg=c("A","B","C","D","E","F","G"),
border="red", density=c(90, 70, 50, 40, 30, 20, 10))
You have seen the following arguments before: main, xlab and ylab, but not the others. OK. What did the density parameter achieve? Try other values of the density parameter and see what you get. What does the command names.arg do?
OK. Let’s create a third, more complex, bar chart, based on several (four) variables. Let’s assume that you wish to plot the magnitudes of each value within each variable.
data <- structure(list(W= c(1L, 3L, 6L, 4L, 9L), X = c(2L, 5L,
4L, 5L, 12L), Y = c(4L, 4L, 6L, 6L, 16L), Z = c(3L, 5L,
6L, 7L, 6L)), .Names = c("W", "X", "Y", "Z"), class = "data.frame", row.names = c(NA, -5L))
W X Y Z
1 1 2 4 3
2 3 5 4 5
3 6 4 6 6
4 4 5 6 7
5 9 12 16 6
Now we wish to create side-by-side bar charts for the four variables, W, X, Y and Z. First, we set up some colours, one for each of the five values within each variable.
colours <- c("red", "orange", "blue", "yellow", "green")
Now, plot a bar chart of the four variables, with adjacent bars, using the as.matrix() command and the argument beside = T.
barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)
What did the argument beside = TRUE achieve?
Now we create a legend at the top-left corner. To create a legend without a frame, include: bty=”n”. The bty argument controls legend borders.
legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)
We used the topleft argument to position the legend towards the top left of the chart. Other options include:
"bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right", "center".
Try these for yourselves.
That wasn’t so hard! In then next post in this series, we will look at further plotting techniques in R.
About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.