+ Reply to Thread
Results 1 to 4 of 4

Thread: Labels and Lines

  1. #1
    Points: 1,277, Level: 19
    Level completed: 77%, Points required for next Level: 23

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #2
    Super Moderator
    Points: 14,566, Level: 78
    Level completed: 29%, Points required for next Level: 284
    bugman's Avatar
    Posts
    1,490
    Thanks
    88
    Thanked 138 Times in 107 Posts

    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?
    The earth is round: P<0.05

  3. #3
    TS Contributor
    Points: 5,565, Level: 48
    Level completed: 8%, Points required for next Level: 185

    Location
    St Albans, UK
    Posts
    257
    Thanks
    0
    Thanked 7 Times in 5 Posts

    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
    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)
    }

  4. #4
    Super Moderator
    Points: 14,566, Level: 78
    Level completed: 29%, Points required for next Level: 284
    bugman's Avatar
    Posts
    1,490
    Thanks
    88
    Thanked 138 Times in 107 Posts

    Re: Labels and Lines


    Thanks Mike, very useful.
    The earth is round: P<0.05

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 12-21-2012, 08:42 AM
  2. Replies: 3
    Last Post: 02-06-2011, 04:32 PM
  3. labels for 3d ordination plot
    By bugman in forum R
    Replies: 4
    Last Post: 09-14-2009, 06:07 PM
  4. R labels in the plot
    By Tart in forum R
    Replies: 4
    Last Post: 06-13-2008, 08:31 AM
  5. Data Labels in SPSS Charts?
    By Remo in forum SPSS
    Replies: 3
    Last Post: 07-19-2006, 05:35 AM

Posting Permissions

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








Advertise on Talk Stats