tags:

views:

294

answers:

2

I am trying to have the x-axis labels to be split into two lines. I would also like the labels to be rotated 45 degrees. How can I do this?

What I have so far:

N <- 10
dnow <- data.frame(x=1:N, y=runif(N), labels=paste("This is observation ",1:N))
with(dnow, plot(x,y, xaxt="n", xlab=""))
atn <- seq(1,N,3)
axis(1, at=atn, labels=labels[atn])
+2  A: 
Christopher DuBois
@chris - I would like to accept your question (all the cool kids use ggplot2 these days). But, could you fix the x-axis before hand (ggplot orders character vectors alphabetically, so you have to convert it to a factor.) Thanks!
Eduardo Leoni
Good suggestion. I switched it to be a factor, but the right hand side is still being cut off. I'll try and fix that.
Christopher DuBois
A: 

This is what I cooked up (before my ggplot2 days) using base graphics:

## data
N <- 10
dnow <- data.frame(x=1:N, y=runif(N), labels=paste("This is \nobservation ",1:N))
## make margins wide
par(mfrow=c(1,1), mar=c(10,10,6,4))
## plot without axix labels or ticks
with(dnow, plot(x,y, xaxt="n", xlab=""))
## the positions we ant to plot
atn <- seq(1,N,3)
## the label for these positions
lab <- dnow$labels[atn]
## plot the axis, but do not plot labels
axis(1, at=atn, labels=FALSE)
## plot labels
text(atn, ## x position
     par("usr")[3]-.05, ## position of the low axis
     srt=45, ## angle
     labels=lab, ##labels
     xpd=TRUE, ## allows plotting outside the region 
     pos=2)
## par("usr")[3]
Eduardo Leoni