I am attempting to label lines on a plot with the label for each line embedded in a short break of the line, such as; ----- 25 yr line ------ . I have searched the help files, Paul Murrell's book, and various libraries (Lattice, Hmisc(labcurve), ggplot, calibrate, and others) but I can not find anything. Interestingly, this is the default for labeling contours with the contour command (method="edge"). Any help would be much appreciated.
Thank you.
This in no way the easiest way to solve this, but I have done this sort of thing with a combination of using the "clip" function and "mtext".
The amount of script will obviously depend on the number of lines you have, but if you are clever at looping and functions - which I am not (unfortunately) this should be pretty straight forward.
One method for printing text over horizontal lines is to calculate the size of the text labels using the dimensions function and then plot a white rectangle with these dimensions using the symbol function and then print the text over the rectangle. The following example shows how the functions can be used.
Code:
y<-rnorm(10, mean=10, sd=1)
plot(y, ylim=c(0,20), las=1)
# line positions on y axis
h<-c(5,10,15)
# labels
txt<-c("5 yr line", "10 yr line", "15 yr line")
# expansion factor for rectangle
exf<-1.3
# calculate width and height of text
dimensions<-exf*sapply(txt, function(x) matrix(c(strwidth(x,cex=0.8),strheight(x, cex=0.8)),nrow=1))
for (i in 1:3){
abline(h=h[i])
# plot a white rectangle same size as text
symbols(x=2, y=h[i], rectangles=matrix(dimensions[,i], ncol=2), bg='white',fg='white',add=TRUE,inches=FALSE)
# print text over the rectangle
text(x=2, y=h[i], txt[i], cex=0.8)
}