[R] using text on the x axis ticks rather than numbers

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Sep 7 21:01:24 CEST 2004


On Tue, 2004-09-07 at 12:52, Rajarshi Guha wrote:
> Hello,
>   is there a way in which I can use text labels rather than numbers on
> the x axis ticks? I basically have a vector of (say) 8 points and I want
> to plot these sequentially. Thus the x axis would have ticks at 1 .. 8.
> Rather than having the labels 1 .. 8 I would like to have some arbitrary
> text labels.
> 
> Ideally I would like the labels to be rotated (say at 45 degrees) so
> that they don't overlap with each other.
> 
> Is this possible?
> 
> Thanks,


Here is an example. For the axis labels, you need to use text() and not
mtext() as the latter does not allow for text rotation:

# Set margins to make room for x axis labels
par(mar = c(7, 4, 4, 2) + 0.1)

# Create plot with no x axis and no x axis label
plot(1:8, xaxt = "n",  xlab = "")

# Set up x axis with tick marks alone
axis(1, labels = FALSE)

# Create arbitrary text
labels <- paste("arbitrary text", 1:8, sep = " ")

# plot x axis labels using:
# par("usr")[3] - 0.25 as the vertical placement
# srt = 45 as text rotation angle
# adj = 1 to place right end of text at tick mark
# xpd = TRUE to allow for text outside the plot region
text(1:8, par("usr")[1] - 0.25, srt = 45, adj = 1,
     labels = labels, xpd = TRUE)

# plot x axis label at line 6 (of 7)
mtext(1, text = "X Axis Label", line = 6)


You can adjust the value of the '0.25' offset as required to move the x
axis labels up or down relative to the x axis.

HTH,

Marc Schwartz




More information about the R-help mailing list