Graphing Results: Bar Graphs and Pie Charts

Often when working with quantitative data, it is also useful to create graphs that allow you to see how your data is distributed more easily than in a list of numbers. For example, one can create a simple bar graph that shows the length of each play written by Sophocles with the commend barplot(trag.length[trag.length$Author == "Aeschylus", "Word.Count"]) . The graph generated by this command is shown to the right.

You can also add titles, legends, and colors to your graph either by adding options to the barplot() command or by issuing commands after generating the original graph. ((The following examples are drawn from, and The complete command reference for barplot is on-line at The options you might want to add include:

The secondary commands you might want to include are:

You can put all of these options together with the following three commands to generate the much easier to read graph shown below.

barplot (trag.length[trag.length$Author == "Aeschylus", "Word.Count"], col = rainbow(7), xlab = "Length in Words", horiz="true")
title("Length of Aeschylus' Tragedies")
legend("bottomright", title="Plays", as.vector(trag.length[trag.length$Author == "Aeschylus", "Play"]), fill=rainbow(length(trag.length[trag.length$Author == "Aeschylus", "Play"])), cex = .75)

To generate:

Graph Showing Length of Aeschylus' Tragedies in Words

The same steps are used in order to plot data such as the length of each segment in books nine through twelve of the Odyssey. However, it is necessary to first use the table command as described in the previous section to aggregate the numeric data and before issuing the barplot command barplot(table(odyssey.monsters[,"Segment"])).

A colorful graph with a title, labels on the x and y axes and a legend can be created with the commands barplot(as.vector(table(odyssey.monsters[,"Segment"])), col = rainbow(length(names(table(odyssey.monsters[, "Segment"])))), ylab = "Length in Words", xlab ="Episode") and then legend("topleft", title="Segments", names(table(odyssey.monsters[, "Segment"])), fill=rainbow(length(names(table(odyssey.monsters[, "Segment"])))),, cex = .25).

You can also generate a pie chart instead of bar chart using the R command pie. This command follows the syntax of barplot with many of the same options including the color and legend commands. The code below produces the following graph:

pie(trag.length[trag.length$Author == "Sophocles", "Word.Count"], labels=paste (labels=trag.length[trag.length$Author == "Sophocles", "Play"], labels=trag.length[trag.length$Author == "Sophocles", "Word.Count"]), col=rainbow(length(trag.length[trag.length$Author == "Sophocles", "Play"])), cex=.75) Pie Chart Showing Length of Sophocles Tragedies in Words

Finally, the commands above will create the graph in an interactive window of your R session. If you would like to save the image, users of the Macintosh and Windows versions of R can use the Save As... command to save the image as a PDF file. If you would like to create a PNG or JPEG file, these commands can be preceded with the PNG or JPEG command in conjunction with the file.choose() command. When issued on a Macintosh or Windows machine, the command PNG(png(filename=file.choose(new=TRUE))) will display a dialogue box that allows you to select a location and enter a name for the output file. After this command, issue the commands needed to create your graph as described above. Finally, the command is used to end output to your file and save all of your changes to disk.

<<-- Previous: Calculate Totals and Subtotals
Next: Averages, Range, Interquartile Measures, and Boxplots -->>