Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to break axis labels into 2 lines in base graphics?

Tags:

r

graphics

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])
like image 727
Eduardo Leoni Avatar asked Sep 08 '09 17:09

Eduardo Leoni


1 Answers

Here's one possibility with the ggplot2 package.

N <- 10
labs <- factor(1:N,labels=paste("This is \n observation",1:N))
dnow <- data.frame(x=1:N, y=runif(N), labels=labs)
qplot(labels,y,data=dnow) + 
      opts(axis.text.x=theme_text(angle=-45,hjust=0))

alt text

I'm looking forward to seeing the base package examples, too!

like image 55
Christopher DuBois Avatar answered Nov 07 '22 06:11

Christopher DuBois