Add custom tick mark labels to a plot in R software


In this article you will learn how to customize tick marks using R statistical software : changing the interval between tick marks; color and the font size for tick mark labels (texts); rotation of tick mark labels.

Color, font style and font size of tick mark labels :

For this end, the following argument can be used :

  • col.axis : the color to be used for tick mark labels
  • font.axis : an integer specifying the font style; possible values are :
    • 1: normal text
    • 2: bold
    • 3: italic
    • 4: bold and italic
    • 5 : symbol font
  • cex.axis : the size for tick mark labels; default value is 1.
x<-1:10; y<-x*x
# Simple graph
plot(x, y)
# Custom plot : blue text, italic-bold, magnification
plot(x,y, col.axis="blue", font.axis=4, cex.axis=1.5)

How to add tick marks to a plot wit R softwareHow to add tick marks to a plot wit R software

Orientation of tick mark labels

To change the style of the tick mark labels, las argument can be used. The possible values are :

  • 0: the labels are parallel to the axis (default)
  • 1: always horizontal
  • 2 : always perpendicular to the axis
  • 3 : always vertical
plot(x, y, las=0) # parallel
plot(x, y, las=1) # horizontal
plot(x, y, las=2) # perpendicular

How to add tick marks to a plot wit R softwareHow to add tick marks to a plot wit R softwareHow to add tick marks to a plot wit R software

Hide tick marks

To hide or to show tick mark labels, the following graphical parameters can be used :

  • xaxt : a character specifying the x axis type; possible values are either “s” (for showing the axis) or “n” ( for hiding the axis)
  • yaxt : a character specifying the y axis type; possible values are either “s” (for showing the axis) or “n” ( for hiding the axis)

These two arguments are very useful to take the control of the rotation angle for tick mark labels. Changing the rotation angle is not something easy in R but we’ll see how to do it in the next section.

# Hide x and y axis
plot(x, y, xaxt="n", yaxt="n")

How to add tick marks to a plot wit R software

Change the string rotation of tick mark labels

The following steps can be used :

  1. Hide x and y axis
  2. Add tick marks using the axis() R function
  3. Add tick mark labels using the text() function

The argument srt can be used to modify the text rotation in degrees.

# Suppress the axis
plot(x, y, xaxt="n", yaxt="n")
# Changing x axis
xtick<-seq(0, 10, by=5)
axis(side=1, at=xtick, labels = FALSE)
text(x=xtick,  par("usr")[3], 
     labels = xtick, srt = 45, pos = 1, xpd = TRUE)
# Changing y axis
ytick<-seq(0, 100, by=50)
axis(side=2, at=ytick, labels = FALSE)
text(par("usr")[1], ytick,  
     labels = ytick, srt = 45, pos = 2, xpd = TRUE)

How to add tick marks to a plot wit R software

Use the par() function

The par() function can be used to permanently apply the changes to all of the graphs that will be created in the current session.

par(col.axis="blue", font.axis=4, cex.axis=1.5)
plot(x,y)

How to add tick marks to a plot wit R software

Infos

This analysis has been performed using R statistical software (ver. 3.1.0).


Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!





This page has been seen 250012 times