DataScience+ An online community for showcasing R & Python tutorials. It operates as a networking platform for data scientists to promote their talent and get hired. Our mission is to empower data scientists by bridging the gap between talent and opportunity.
Visualizing Data

Mastering R plot – Part 2: Axis

This is the second part of the Mastering R plot series.

The standard plot function in R allows extensive tuning of every element being plotted. There are, however, many possible ways and the standard help file are hard to grasp at the beginning. In this article we will see how to control every aspects of the axis (labels, tick marks …) in the standard plot function.

Axis title and labels

Create some data and create a plot with default settings.

#some data
#a usual plot with per default settings
#changing the axis title is pretty straightforward
plot(x,y,xlab="Index",ylab="Uniform draws")

Here is the plot:

The settings of the plot are usually controlled by the par function (see ?par for the many possible arguments), once the arguments are set in par they apply to all subsequent plots. Some arguments in par (for example cex.axis) can also be set in other plot functions like axis or text. When these arguments are set in these other functions they will then apply only to the current plot. One can then control if he/she wants all plots to be affected by the change or only the current one.

#change the sizes of the axis labels and axis title
op<-par(no.readonly=TRUE) #this is done to save the default settings 
plot(x,y,xlab="Index",ylab="Uniform draws")
#if we want big axis titles and labels we need to set more space for them
plot(x,y,xlab="Index",ylab="Uniform draws")

Here is the plot:

A handy function to gain deeper control into the axis is the axis function which can control among other things at which values the tick marks are drawn, what axis labels to put under the tick marks, the line type and width of the axis line, the width of the tick marks, the color of the tick marks and axis line.

#we can further control the axis using the axis function
par(op) #re-set the plot to the default settings
plot(x,y,xaxt="n") #this prevent the plotting of x-axis labels 
axis(side=1,at=c(5,50,100)) #force the tick marks to be drawn at x=5, 50 and 100
#one can also specify the labels at the tick marks
#the axis function also control the axis line and tick marks
#some time you may want to remove the box around the plot and only show the axis lines

Here is the plot:

Also note that an R plot has four sides, starting on the bottom and going clockwise (ie side=3 correspond to the top of the graph).

Tick marks

Let’s turn now to the tick marks, they can also be controlled either from par or from axis.

#tick marks finer control goes through par or axis
par(tcl=0.4,mgp=c(1.5,0,0)) #tcl control the length of the tick marks
#positive values will make the tick being drawn inside the plot
#negative values will make tick go outside
#mgp takes three values, the first one control how much line between plot and axis title
#the second between plot and axis labels and the third between plot and axis line
#another example using axis
mtext(side=1,text="X axis",line=1.5) #cannot set the axis title with axis so need to use mtext
mtext(side=2,text="Numbers taken randomly",line=2.2)

Here is the plot:

Here we saw a third additional function, mtext, which allow one to write text outside of the plotting area (I guess that mtext stands for “margin text”). We have to specify how far from the plotting region one wants to write the text with the line argument. This concept of lines is important to understand how to control spaces around the plotting region. We controlled it earlier in this post when I used the mar argument which sets how many lines are available on each sides of the plot. Let’s look at this in more details:

#understanding the lines
for(i in 0:4){
  mtext(side=1,text=paste0("Line ",i),line=i)
for(i in 0:3){
  mtext(side=2,text=paste0("Line ",i),line=i)
#of course this can be changed with the mar argument in par
for(i in 0:6){
  mtext(side=1,text=paste0("Line ",i),line=i)
for(i in 0:1){
  mtext(side=2,text=paste0("Line ",i),line=i)

Here is the plot:

From this last graph it is easy to grasp that on each side of the plot there are a certain number of lines, have a look at par()$mar for the default numbers. Using this, one can control how much space to create around the plot but also where to write axis labels or titles. Next time we’ll extend this concept of margins by talking about the outer margins from the plot, until then happy plotting!