Skip to content

Fitting large titles in a plot

10 messages · Svempa, Jim Price, Bert Gunter +5 more

#
I want to fit a fairly long main title for a plot, supposedly by changing row
after a while. As for now it starts way outside the picture margin at the
left and continues way out right passed the right margins.
just about half of it.")

Any suggestions? Shouldn't be that hard.
#
I wrote a little utility function for exactly this reason, which I use with
long titles. You may want to add calls to par to adjust the upper margin if
you are using raw graphical functionality (plot et al) - but lattice adjusts
the upper margin automatically so you wouldn't need to add anything else.


PrettyString <- function(theString, maxLength, collapse = "\n")
{
 	words <- unlist(strsplit(theString, " "))
 	wordLengths <- unlist(lapply(strsplit(words, ""), length))

 	if(max(wordLengths) > maxLength) 
		stop("maxChar must be increased due to string length")

 	count = wordLengths[1]
 	results = vector()
 	currentLine = words[1]

 	for(i in 2:length(words))
 	{
  		if((count + wordLengths[i] + 1) > maxLength)
  		{
   			results = c(results, currentLine)
   			currentLine = words[i]
   			count = wordLengths[i]
  		}
  		else
  		{
   			currentLine = paste(currentLine, words[i])
   			count = count + wordLengths[i] + 1
  		}
 	}
 	if(length(currentLine))
		results <- c(results, currentLine)

 	paste(results, collapse = collapse)
}


Knowing the R list, someone can probably reduce this function to 2 lines of
code.
Jim
Svempa wrote:
just about half of it.")

  
    
#
Try This:

plot(A,main=paste("This is my really long title and","\n","it's so long that
I can see
just about half of it.", sep = " "))

-- Bert Gunter
Genentech
#
Try inserting \n into the title where you would like it to start on a
new line, e.g.:

plot(A,main="This is my really long title\nand it's so long\nthat I can
see just about half of it.")

You may need to give yourself more room in the margin for multi-line
titles (see ?par and the mar entry) and using the title function (rather
than in the plot) will let you specify the position of the title (line
argument).

Hope this helps,
#
\n to start a new line 

plot(1:10,main="This is my really long title \n and
it's so long that I can see \n just about half of
it.")
--- Svempa <fempa at yahoo.com> wrote:

            
http://www.nabble.com/Fitting-large-titles-in-a-plot-tf4956510.html#a14193900
#
On Thu, 2007-12-06 at 07:16 -0800, Svempa wrote:
You can insert newline characters ('\n') in the title:


plot(A, main = "This is my really long title\nand it's so long that I
can see\njust about half of it.")


The likelihood is that you will need to alter the vertical position of
the title to accommodate the line wrapping.

To do that, you can either use mtext() in lieu of the 'main' argument,
or consider adjusting par("mgp"), the first value of which is the
position of the plot title. See ?mtext and ?par.

You might also want to look at ?strwrap for a more general way of
wrapping long lines of text.

HTH,

Marc Schwartz
#
try this:


plot(0, main=paste(strwrap("This is my really long title and it's so
long that I can see just about half of it.", width=50),
collapse="\n"))
On Dec 6, 2007 7:16 AM, Svempa <fempa at yahoo.com> wrote:

  
    
#
On Thu, 2007-12-06 at 09:28 -0800, Jim Price wrote:
Would you believe one line?

  paste(strwrap(theString, width = maxLength), collapse = "\n")

;-)

See ?strwrap as I noted previously.

HTH,

Marc Schwartz
#
I have learned something new - thanks for the strwrap info.

The problem with posting from Nabble is that by the time your post actually
gets to the list (2 hours after you posted it in this case) and you've
written some line like "Knowing the R list, someone can probably reduce this
function to 2 lines of code" half a dozen people have already shown exactly
how you can do that, making me look totally incompetent.

Ah well, such is life :)

Jim.
Marc Schwartz wrote:

  
    
3 days later
#
On Thu, 06-Dec-2007 at 07:16AM -0800, Svempa wrote:
|> 
|> I want to fit a fairly long main title for a plot, supposedly by changing row
|> after a while. As for now it starts way outside the picture margin at the
|> left and continues way out right passed the right margins.
|> 
|> >plot(A,main="This is my really long title and it's so long that I can see
|> just about half of it.")
|> 
|> Any suggestions? Shouldn't be that hard.

Something like this will probably be close enough:

main="This is my really long title and it's so long that I can see\njust about half of it."

HTH