Hello, I have a simple question that I could not really figure out. I am plotting labels within a graph using the text function. I first plot the first label by specifying the x and y coordinates on the graph. Then to plot the second label next to it, I am using te strwidth function to get the width of the previous label in user coordinate then add the maximum width to the x value. However, this is not working: Here is an example: lab1<-c("Hocine & Ruba 2000", "Yasmine 2004","Ziad was born in 2006") lab2<-c(20,22,45) lab3<-c(54677,10900,200) plot(-10:5,1:6) text(par("usr")[1],2:4,lab1,adj=c(0,0.5)) text(par("usr")[1]+max(strwidth(lab1)),2:4,lab1,adj=c(0,0.5)) However, this is not working OK. I would like to get the max width of lab1 as it appears in the figure then add that amount to plot lab2 and so forth. I would like to leave only one blank column between successive labels. Thanks! Hocine [[alternative HTML version deleted]]
On 2010-04-14 19:57, Tighiouart, Hocine wrote:> Hello, > > I have a simple question that I could not really figure out. I am plotting labels within a graph using the text function. I first plot the first label by specifying the x and y coordinates on the graph. Then to plot the second label next to it, I am using te strwidth function to get the width of the previous label in user coordinate then add the maximum width to the x value. However, this is not working: > > Here is an example: > > lab1<-c("Hocine& Ruba 2000", "Yasmine 2004","Ziad was born in 2006") > lab2<-c(20,22,45) > lab3<-c(54677,10900,200) > plot(-10:5,1:6) > text(par("usr")[1],2:4,lab1,adj=c(0,0.5)) > text(par("usr")[1]+max(strwidth(lab1)),2:4,lab1,adj=c(0,0.5)) > > However, this is not working OK. I would like to get the max width of lab1 as it appears in the figure then add that amount to plot lab2 and so forth. I would like to leave only one blank column between successive labels.Does this give you what you have in mind: text(par("usr")[1] + max(strwidth(paste(lab1, ""))), 2:4,lab1,adj=c(0,0.5)) -Peter Ehlers> > Thanks! > > Hocine > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Peter Ehlers University of Calgary
Thanks Peter. This worked fine. I guess my problem was that when I maximize the graph window everything seems to expand, this is why I was having trouble with it. -----Original Message----- From: Peter Ehlers [mailto:ehlers at ucalgary.ca] Sent: Thursday, April 15, 2010 4:54 AM To: Tighiouart, Hocine Cc: r-help at r-project.org Subject: Re: [R] graphic question On 2010-04-14 19:57, Tighiouart, Hocine wrote:> Hello, > > I have a simple question that I could not really figure out. I amplotting labels within a graph using the text function. I first plot the first label by specifying the x and y coordinates on the graph. Then to plot the second label next to it, I am using te strwidth function to get the width of the previous label in user coordinate then add the maximum width to the x value. However, this is not working:> > Here is an example: > > lab1<-c("Hocine& Ruba 2000", "Yasmine 2004","Ziad was born in 2006") > lab2<-c(20,22,45) > lab3<-c(54677,10900,200) > plot(-10:5,1:6) > text(par("usr")[1],2:4,lab1,adj=c(0,0.5)) > text(par("usr")[1]+max(strwidth(lab1)),2:4,lab1,adj=c(0,0.5)) > > However, this is not working OK. I would like to get the max width oflab1 as it appears in the figure then add that amount to plot lab2 and so forth. I would like to leave only one blank column between successive labels. Does this give you what you have in mind: text(par("usr")[1] + max(strwidth(paste(lab1, ""))), 2:4,lab1,adj=c(0,0.5)) -Peter Ehlers> > Thanks! > > Hocine > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > >-- Peter Ehlers University of Calgary