Graphics Reference
In-Depth Information
Creating a Bar Graph
Problem
You want to make a bar graph.
Solution
To make a bar graph of values ( Figure 2-5 ), use barplot() and pass it a vector of values for the
height of each bar and (optionally) a vector of labels for each bar. If the vector has names for the
elements, the names will automatically be used as labels:
barplot(BOD$demand, names.arg = BOD$Time)
Figure 2-5. Left: bar graph of values with base graphics; right: bar graph of counts
Sometimes “bar graph” refers to a graph where the bars represent the countof cases in each cat-
egory. This is similar to a histogram, but with a discrete instead of continuous x-axis. To generate
the count of each unique value in a vector, use the table() function:
table(mtcars$cyl)
4 6 8
11 7 14
# There are 11 cases of the value 4, 7 cases of 6, and 14 cases of 8
Search WWH ::




Custom Search