Luis A Escobar
2007-Jan-17 16:08 UTC
[R] How to annotate a graph with non-transparent math labels?
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
jim holtman
2007-Jan-17 22:30 UTC
[R] How to annotate a graph with non-transparent math labels?
try using strwidth & strheight x<-c(0,1) plot(x,x,type='l') dimensions<-matrix(c(strwidth(expression(theta),cex=5),strheight(expression(theta), cex=5)),nrow=1) symbols(0.5,0.5 ,rectangle=dimensions,bg='white',fg='white',add=TRUE,inches=FALSE) text(0.5,0.5,expression(theta),cex=5) On 1/17/07, Luis A Escobar <luis@lsu.edu> wrote:> > 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 > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]