You are on page 1of 2

Loading Today’s Data

For this activity, we won’t be using or manipulating any new data, we will just been turning some
data into pie charts and bar graphs. You can feel free to load the data from previous activities
(e.g. the Titanic data set), or just make up a few numbers to use for the different charts.
Pie Charts
To start with we will make a simple pie chart. Lets imagine we have the following data from a
survey of New Yorker’s favorite baseball team: Mets: 453, Yankees: 591, Other: 221. Now
the most basic pie chart we can make is done like so:
pie(c(453, 591, 221)) remember that c(x, y, ...) means combine all the values together
Just like with histograms, we can add a lot of additional features to our graph. For example we
can change the main title by adding a parameter for title called main:
pie(c(453, 591, 221), main = "Baseball Fandom")
We can also add labels to the pie chart sections to override the default labels of 1, 2 and 3 like so:
pie(c(453, 591, 221), main = "Baseball Fandom", labels = c("Mets", "Yankees",
"Other")) the order of the labels should match the order of the data e.g Mets is first since we
put the 453 Mets fans first in our pie chart
Additionally we can change the colors of the pie chart by combining three (or fewer) colors together:
pie(c(453, 591, 221), main = "Baseball Fandom", labels = c("Mets", "Yankees",
"Other"), col = c("chocolate1", "darkblue", "red")) once again the order of the colors
should match the order of the data
One last thing we can do with pie charts is to turn off the labels by setting labels to be empty like so:
pie(c(453, 591, 221), main = "Baseball Fandom", labels = "", col =
c("chocolate1", "darkblue", "red"))
Legends
A legend is a way of adding some explanation to our graphical display. We might use a legend
if we don’t want to label the individual sections of a pie chart, or if we want additional ways of
labeling (e.g. labeling what a color represents). To add a legend, we first need to create a chart
and then use the legend function to add a legend to the chart we just made. Like this:
pie(c(453, 591, 221), main = "Baseball Fandom", labels = "", col =
c("chocolate1", "darkblue", "red"))
legend("topleft", c("Mets", "Yankees", "Other"), fill = c("chocolate1",
"darkblue", "red"))
The first parameter in legend is the location. You could also try "bottomright" or some other
locations. The second parameter is the text we want to display in the legend. Finally the fill
value is the colors that will be associated with each text value (e.g. Mets will have the chocolate1
color associated with it).
Often legends will overlap with a pie chart or other display. It’s usually a good idea to resize the
window of the chart before adding the legend.
Bar Graphs
Creating a bar graph in R is very similar to making a pie chart. We will use the same sample
data from before. Lets start with a basic bar graph:
barplot(c(453, 591, 221))
Just like with any other display we can add a main title, like so:
barplot(c(453, 591, 221), main = "Baseball Fandom")

1
We can also label the x-axis (or y-axis) the same way we did for histograms:
barplot(c(453, 591, 221), main = "Baseball Fandom", xlab = "Teams")
Labeling the individual bars is a little different than pie charts, but still fairly straightforward:
barplot(c(453, 591, 221), main = "Baseball Fandom", xlab = "Teams", names.arg =
c("Mets", "Yankees", "Other"))
We can also color the bars in the same way we did for pie charts, by adding values for col:
barplot(c(453, 591, 221), main = "Baseball Fandom", xlab = "Teams", names.arg =
c("Mets", "Yankees", "Other"), col = c("chocolate1", "darkblue", "red"))
Alternatively we could use a legend for our bar graph instead of labeling the x-axis
barplot(c(453, 591, 221), main = "Baseball Fandom", col = c("chocolate1",
"darkblue", "red"))
legend("topright", c("Mets", "Yankees", "Other"), fill = c("chocolate1",
"darkblue", "red"))
Segmented Bar Graphs
In order to make a segmented bar graph, we need a table (aka matrix) of data values. Each
column in the table would represent one bar, and the value in the first row would correspond to
the bottom segment on the bar. First lets create a sample matrix that we can use to make some
examples of segmented bar graphs.
mat <- matrix(c(1,2,3,4,5,6), ncol = 3) this will give a matrix with 3 columns with 1 and
2 in the first column, 3 and 4 in the second etc.
Then we can turn that matrix into a bar graph using the barplot
command: barplot(mat) This will create a segmented bar graph which uses different shades
of gray to distinguish between the segments. We can label the bars in the same way as a regualr
bar graph (keeping in mind that there are three total bars)
barplot(mat, names.arg = c("x", "y", "z"))
We can also add colors to the bars, however they way coloring works in different from a non-
segmented bar graph. The list of colors we give will correspond to each segment rather than the
individual bars. Since our sample data only has two segments, we only need two colors:
barplot(mat, names.arg = c("x", "y", "z"), col = c("red", "blue")) since red is first it
will be matched with the bottom section of the bar
One last thing we might want to do is add a legend to indicate what the different colors mean:
barplot(mat, names.arg = c("x", "y", "z"), col = c("red", "blue"))
legend("topleft", c("Type1", "Type2"), fill = c("red", "blue"))
You can find a lot more options for bar graphs, pie charts and legends at the links below (or just
by googling):
Pie charts: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/pie.html
Bar graphs: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/barplot.html
Legends: https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/legend.html

You might also like