Data visualization in R: Axis tick marks

You can use the axis function to customize the tick marks of your plot. The parameter of this function will allow you to define the tick values, their labels and the drawing direction and size of the tick marks.

Let us start with a standard plot. The following code creates a plot and uses the standard axes.

x <- c(1,2,3,4,5)
y <- c(1.5,3.7,2.2,5.4,3.0)
plot(x, y, ylim=c(0,6))
 

R14_a

 
Next we want to modify the y-axis a little bit. Let us add some more ticks and label at other positions. Furthermore the tick marks with labels should be larger than the other ones. As we want to show tick marks of different sizes we have to call the axis function twice. The second function call is used to create the larger tick marks. Therefore the second axis does not contain any labels.

The following code shows the example plot with the changed y-axis.

x <- c(1,2,3,4,5)
y <- c(1.5,3.7,2.2,5.4,3.0)
 
pos <- c(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6)
label <- c(‚0‘, “, “, ‚1.5‘, “, “, ‚3‘, “, “, ‚4.5‘, “, “, ‚6‘)
pos2 <- c(0, 1.5, 3, 4.5, 6)
 
plot(x, y, ylim=c(0,6), yaxt=’n‘)
axis(side=2, at=pos, labels=label, tck=-0.015)
axis(side=2, at=pos2, labels=FALSE, tck=-0.03)
 

R14_b

 
The axis function of the example code contains the following parameters:

  • at: contains a vector with the tick mark positions
  • labels: contains a vector with the text for the labels
  • tck: is the length of the tick marks as a fraction of the plotting region (a negative number is outside graph, a positive number is inside, 0 suppresses the ticks, 1 creates gridlines)
Advertisements
Dieser Beitrag wurde unter R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s