Labelling A Bar Graph
Now we're going to add some labels to the bar graph, still using barplot()
. The first argument of barplot()
was a vector of the bar heights. Following this, we can add arguments to format the graph as necessary. For instance, barplot(height, argument1, argument2)
.
Here we are going to add a label to the y axis using the argument ylab = "name here"
, and x axis labels to the bars using the argument names.arg = "vector of names here"
.
This exercise is part of the course
Basic Statistics
Exercise instructions
- Make a vector of the names of the bars using the
c()
command. Assign this to the variablebarnames
. Remember that the first bar isautomatic
and the second ismanual
. - Add the
ylab =
andnames.arg =
commands to yourbarplot(height)
code - Label the y axis "number of cars" and use
barnames
to label the bars. - Remeber that arguments in a function are separated with a comma (e.g. function(argument1, argument2, argument3))
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# vector of bar heights
height <- table(mtcars$am)
# Make a vector of the names of the bars called "barnames"
# Label the y axis "number of cars" and label the bars using barnames