In this demonstration, we’ll need to load the openintro and lattice packages. Use the require
command to do this first.
require(openintro)
require(lattice)
Let’s look at the cars data. The command below will show you the first 10 rows.
cars[1:10,]
## type price mpgCity driveTrain passengers weight
## 1 small 15.9 25 front 5 2705
## 2 midsize 33.9 18 front 5 3560
## 3 midsize 37.7 19 front 6 3405
## 4 midsize 30.0 22 rear 4 3640
## 5 midsize 15.7 22 front 6 2880
## 6 large 20.8 19 front 6 3470
## 7 large 23.7 16 rear 6 4105
## 8 midsize 26.3 19 front 5 3495
## 9 large 34.7 16 front 6 3620
## 10 midsize 40.1 16 front 5 3935
It appears that we have two qualitative variables in the data set, type
and driveTrain
. Notice the spelling/punctuation on each variable. You’ll need to copy this exactly in your commands.
We can use the barchart()
command in the lattice package to make a simple frequency barchart for the variable type
. If you just want a quick glimpse at the variable, this will usually do the trick.
barchart(cars$type)
If you want to dress the chart up for presentation, you can use some additional arguments to the barchart command as shown below. Notice that there is a comma separating each argument, and parentheses enclosing all the arguments.
barchart(cars$type, #This specifies the dataset and the variable
horizontal = FALSE, #Turn the bars so they are vertical
main = "Cars in 1993", #Give your chart a title
xlab = "Type of Car", #Label the x axis
ylab = "Frequency", #Label the y axis
col = "darkgreen") #change the color of the bars
If you want to make either a relative frequency bar chart or a class percentage bar chart, I recommend you use the barplot()
command instead of barchart()
. The arguments are called slightly differently for barplot()
, but it allows you to modify how the frequencies are reported.
To make a relative frequency bar chart, use the command below.
barplot(table(cars$type)/nrow(cars), #divide the frequency counts by the total
main = "Cars in 1993", #Give your chart a title
xlab = "Type of Car", #Label the x axis
ylab = "Relative Frequency", #Label the y axis
col = "darkgreen")
To make a percentage bar chart, use the command below.
barplot(table(cars$type)/nrow(cars)*100, #convert relative frequencies to percentages
main = "Cars in 1993", #Give your chart a title
xlab = "Type of Car", #Label the x axis
ylab = "Percentage", #Label the y axis
col = "darkgreen")
As an exercise, modify the code on this page to create a bar chart for the variable driveTrain
. You should be able to do this by replacing type
with driveTrain
wherever you see it in the code above and by modifying the x-axis label with the xlab
argument.