Hi All, I have a problem of putting long titles on a graph: for example, x= seq(1:100) y=seq(1:100) plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") R seems not able to print the whole title. The title content might be changing and thus I don't know how long it is beforehand. Is there a way to measure the length and then put the title into different lines accordingly? Thanks! Hua
Dear Hua, Try using "\n" in the title or the "cex.main" argument. See ?par for details. # First attempt x= seq(1:100) y=seq(1:100) plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,\n AED-E=10,DE-F=3,AB-J=4,AC-J=10,\n ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") # Second attempt - using cex.main plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0, AED-E=10,DE-F=3,AB-J=4,AC-J=10, ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4",cex.main=.5) HTH, Jorge On Tue, Jun 10, 2008 at 12:07 PM, Hua Li <hualihua@yahoo.com> wrote:> Hi All, > > I have a problem of putting long titles on a graph: > > for example, > > x= seq(1:100) > y=seq(1:100) > > plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") > > R seems not able to print the whole title. The title content might be > changing and thus I don't know how long it is beforehand. Is there a way to > measure the length and then put the title into different lines accordingly? > > Thanks! > > Hua > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
#try this cex.main=1.1 This is a par parameter ?par plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4", cex.main=1.1) On Tue, Jun 10, 2008 at 12:07 PM, Hua Li <hualihua@yahoo.com> wrote:> Hi All, > > I have a problem of putting long titles on a graph: > > for example, > > x= seq(1:100) > y=seq(1:100) > > plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") > > R seems not able to print the whole title. The title content might be > changing and thus I don't know how long it is beforehand. Is there a way to > measure the length and then put the title into different lines accordingly? > > Thanks! > > Hua > > ______________________________________________ > R-help@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. >-- Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis [[alternative HTML version deleted]]
Here is one approach:> tmp <- "p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4" > tmp2 <- gsub(',',', ',tmp) > tmp3 <- strwrap(tmp2, 30) > par(mar=c(5,4,10,1)+.1) > plot(1:10, main=paste(tmp3, collapse="\n"))Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Hua Li > Sent: Tuesday, June 10, 2008 10:08 AM > To: r-help at r-project.org > Subject: [R] the title is too long for a graph > > Hi All, > > I have a problem of putting long titles on a graph: > > for example, > > x= seq(1:100) > y=seq(1:100) > plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D > =1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B> 10,CD-S=10,AM-C=4") > > R seems not able to print the whole title. The title content > might be changing and thus I don't know how long it is > beforehand. Is there a way to measure the length and then put > the title into different lines accordingly? > > Thanks! > > Hua > > ______________________________________________ > 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. >
Hua Li wrote:> Hi All, > > I have a problem of putting long titles on a graph: > > for example, > > x= seq(1:100) > y=seq(1:100) > plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") > > R seems not able to print the whole title. The title content might be changing and thus I don't know how long it is beforehand. Is there a way to measure the length and then put the title into different lines accordingly? >There's a strwidth() function that gives the width of a string on the current plot. Note you have to do the plot first so that R knows the coordinates of the plot, since strwidth returns the width in plot coordinates. So: t="This is a very long title and I would like to split it somewhere, but if I split it in the wrong place bad things will happen" x=(1:10)*100 y=1:10 plot(x,y) # x axis is 1:1000 strwidth(t) gives: [1] 2002.939 but plot(y,x) # x axis is 1:10 strwidth(t) [1] 20.02939 Now, once you've done your plot you can get the width with: diff(par()$usr[1:2]) [1] 9.72 and then if strwidth if your title is bigger than that you'll need to split it into N chunks. Your splitting algorithm will have to be clever if you want to break a sentence at word spaces though! Hopefully this is enough to get you going. Barry
Instead of placing this information in the title we could consider placing it in a legend. s <- "a=1,b=2,c=3" plot(1:10) legend("topleft", legend = strsplit(s, ",")[[1]], ncol = 1, cex = 0.5, bty = "n") On Tue, Jun 10, 2008 at 12:07 PM, Hua Li <hualihua at yahoo.com> wrote:> Hi All, > > I have a problem of putting long titles on a graph: > > for example, > > x= seq(1:100) > y=seq(1:100) > plot(x,y,main="p=0.05:A-B=3,C-D=10,D-E=100,A-F=2,AFR-E=3,ACE-D=1,ADEF-M=0,AED-E=10,DE-F=3,AB-J=4,AC-J=10,ED-F=1,ED-B=4,AF-B=10,CD-S=10,AM-C=4") > > R seems not able to print the whole title. The title content might be changing and thus I don't know how long it is beforehand. Is there a way to measure the length and then put the title into different lines accordingly? > > Thanks! > > Hua > > ______________________________________________ > 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. >