[ACCEPTED]-Is it possible to break axis labels into 2 lines in base graphics?-r
Accepted answer
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))
I'm 2 looking forward to seeing the base package 1 examples, too!
This is what I cooked up (before my ggplot2
days) using 1 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]
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.