## 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)) 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) 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)
Werbung
Dieser Beitrag wurde unter R veröffentlicht. Setze ein Lesezeichen auf den Permalink.