I have a long title for a plot in R and it keeps extending outside the plot window. How can I wrap the title over 2 rows?
9 Answers
try adding "\n" (new line) in the middle of your title. For example:
plot(rnorm(100), main="this is my title \non two lines")

- 59,675
- 58
- 202
- 294
-
1Note that as the strings are centered, the first line of the title will be off centre because of the space before the `\n` in the example. This is clearly evident in the figure JD shows. – Gavin Simpson Sep 10 '11 at 09:18
-
I think my graphic might have been made with a space both before and after the `\n` – JD Long Sep 10 '11 at 19:33
-
This answer is not good. Throwing a break by hand in a string is not a solution for wrapping line. It's a hack. – gagarine Jul 04 '18 at 13:09
-
2@gagarine Looking forward to up-voting your better answer in the near future! – JD Long Jul 05 '18 at 11:09
You can use the strwrap
function to split a long string into multiple strings, then use paste
with collapse=\n
to create the string to pass to the main title argument. You might also want to give yourself more room in the margin using the par
function with the mar
argument.

- 48,497
- 6
- 83
- 110
-
8
-
5The above as a function that can be applied to a vector of strings: `wrap_strings <- function(vector_of_strings,width){sapply(vector_of_strings,FUN=function(x){paste(strwrap(x,width=width), collapse="\n")})}` – Etienne Low-Décarie Apr 24 '14 at 20:09
-
1@Etienne, your function is awesome. Made one enhancement, which makes this suitable for assigning back to a vector: `wrap_strings <- function(vector_of_strings,width){as.character(sapply(vector_of_strings,FUN=function(x){paste(strwrap(x,width=width), collapse="\n")}))}` – Amit Kohli Apr 07 '16 at 11:28
You can use strwrap
and paste
to automatically wrap the title of your graph. The width need to be adapted to your media width.
plot(rnorm(100), main = paste(
strwrap(
'This is a very long title wrapped on multiple lines without the need to adjust it by hand',
whitespace_only = TRUE,
width = 50
),
collapse = "\n"
))
R should do that automatically, nobody want cropped title.

- 4,190
- 2
- 30
- 39
-
The problem with this is that the width is set in characters of monospace text, whilst ideally the width would use the string width of the actual font used on the current device, or be in units related to the device units. So contrary to your assertion that this avoids adjustment by hand, it most certainly does require manual adjustment. Or are you claiming you knew the important value was 50 without any trial or error? – Gavin Simpson Jul 05 '18 at 18:17
-
Also, your answer replicates that of Greg Snow's; what is new here? It doesn't even split on word boundaries so is decidedly inferior to other answers. – Gavin Simpson Jul 05 '18 at 18:18
-
@GavinSimpson I should have completed Greg answer to give some real code, missed that. I will try to find a way to adapt on the media, but it would be easy to put this 50 in a global variable and set it to the media width I hop. – gagarine Jul 11 '18 at 17:20
-
By adding a line break:
plot(1:10, main=paste(rep("The quick brown fox", 3), sep="\n"))
This create a tile with three (identical) lines. Just use \n
between your substrings.

- 360,940
- 56
- 644
- 725
-
1Which is what I wrote: "three (identical) lines". If you need different ones, write different ones: `main=paste0("First line\nSecond line\nThird line")`. As I wrote: `use \n between your substring`. – Dirk Eddelbuettel Jul 04 '18 at 13:36
Include line break/newline (\n
) in the title string, e.g.:
strn <- "This is a silly and overly long\ntitle that I want to use on my plot"
plot(1:10, main = strn)

- 170,508
- 25
- 396
- 453
-
Downvoter - Care to explain what is wrong with @Dirk's and my Answers? – Gavin Simpson Sep 09 '11 at 22:23
-
2You should't have to add break to wrap a text. This should be automatic... I have to generate around 100 graphs, I don't want to split manually each title. – gagarine Jul 04 '18 at 12:57
This might be useful for any sentence, so that it splits on words:
wrap_sentence <- function(string, width) {
words <- unlist(strsplit(string, " "))
fullsentence <- ""
checklen <- ""
for(i in 1:length(words)) {
checklen <- paste(checklen, words[i])
if(nchar(checklen)>(width+1)) {
fullsentence <- paste0(fullsentence, "\n")
checklen <- ""
}
fullsentence <- paste(fullsentence, words[i])
}
fullsentence <- sub("^\\s", "", fullsentence)
fullsentence <- gsub("\n ", "\n", fullsentence)
return(fullsentence)
}
I'm sure there's a more efficient way to do it, but it does the job.

- 607
- 1
- 8
- 20
-
1Thanks very much for this contribution, it's really useful little function. – Konrad Feb 02 '16 at 13:14
-
BTW, I've encapsulated it in an additional loop: `for (j in 1:length(string)) { ... }` to facilitate creation of chart labels. – Konrad Feb 02 '16 at 13:30
-
This is a pretty good example of why so many people find R frsutrating hell. This functionality should have been built in with a wrap=TRUE param. Ho hum. – J.J Feb 10 '16 at 13:51
A little on the late side but I found this while I was trying to figure out how to remove the line breaks from my histogram title. Anyway, I think this seems like the easiest option to me. No extra space from \n either. If you want it without line breaks then use paste() instead of c().
plot(rnorm(100), main=c("this is my title","on two lines")) Example plot for evidence

- 71
- 1
- 3
In case you want to go with one of the "\n" solutions, keep in mind it's also possible for the title to get too tall and spill over the top. If your generating lines dynamically, you can keep track of the line count and solve the problem like this:
mar = par()$mar #get the current margins
mar[3] = mar[3] + line_count #increase the top margin if the title has too many lines
par(mar = mar) #set the new margins
hist(rnorm(100), main = title)

- 41
- 4
You can just wrap whatever text you want to plot in str_wrap and specify the characters in width argument. So you can use this in title, subtitle, caption etc.
library(stringr)
str_wrap(x, width = 15)

- 16,640
- 5
- 21
- 39
-
Good to have solutions using a range of functions & packages for the same task! – jsavn Oct 10 '22 at 16:06