Skip to content
Prev 55327 / 398500 Next

using text on the x axis ticks rather than numbers

On Tue, 2004-09-07 at 12:52, Rajarshi Guha wrote:
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