Get startedGet started for free

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

View Course

Exercise instructions

  • Make a vector of the names of the bars using the c() command. Assign this to the variable barnames. Remember that the first bar is automatic and the second is manual.
  • Add the ylab = and names.arg = commands to your barplot(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
Edit and Run Code