[R] How to annotate a graph with non-transparent math labels?

Luis A Escobar luis at lsu.edu
Wed Jan 17 17:08:24 CET 2007


I do not know the precise language to describe the situation. But here
it is what I want to do.

I need to annotate a graph that contains two or more curves with labels
that contain math symbols. The label must go on top of the curve. 

The problem is that when I annotate the plot, I can see the curve
behind the label. Here it is an example using a simple straight line.

x<-c(0,1)
plot(x,x,type='l')
text(0.5,0.5,expression(theta),cex=3)

The line is visible inside the theta symbol. I would like to avoid that.

Following the solution to problem 3.2 in the ISwR book, for a related
problem,  I used the symbols package to write a rectangle (it can a
circle, etc)
with white bf and white fg and then write the label on top of that
rectangle.

Here is it is the code

x<-c(0,1)
plot(x,x,type='l')
text(0.5,0.5,expression(theta),cex=3)
xmax<-max(abs(x))
dimensions<-matrix(c(xmax/24,xmax/24),nrow=1)
symbols(0.5,0.5,rectangle=dimensions,bg='white',fg='white',add=TRUE,inch
es=FALSE)
text(0.5,0.5,expression(theta),cex=3)

It seems to work, but I am not happy with this solution. The symbols
statement above has fixed dimensions and does not work well with
multiple labels of different sizes. With multiple curves the symbols
statement might peel off other curves, axis, etc. Also if one changes
the size of the label (using cex), the size of the "rectangle" might
be too big or too small and it has to be adjusted.

What I would like to have is a simple way to write the label over the
line such
that the label only wipes out what is needed to avoid seeing the curve
behind. It is like I need a rectangles with rubbery dimensions to
contain just the label I am writing.

I searched the FAQ questions for related problems but I did not find
something related. 

My R skills are very rudimentary and I would not surprised the
solution is trivial.

Thank you for any help.

Luis Escobar



More information about the R-help mailing list