1. ## Labels and Lines

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.

2. ## Re: Labels and Lines

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.

Have you looked at plotrix and "grid" for this?

3. ## Re: Labels and Lines

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
# print text over the rectangle
text(x=2, y=h[i], txt[i], cex=0.8)
}``````

4. ## Re: Labels and Lines

Thanks Mike, very useful.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts