39

Whilst producing graphs using ggplot2, I have some long legend names which I wish to wrap over two lines. For example:

a <- (1:10)
b <- c(1,1.5,2,4,5,5.3,7,9,9.5,9.8)
places = c("Birmingham","Chester-le-street","Cambridge", "Newcastle-upon-Tyne","Peterborough","Cambridge", "Newcastle-upon-Tyne","Peterborough","Liverpool","Stratford-upon-Avon")
df1 = data.frame(a,b,places)
library(ggplot2)
i = ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3) + opts(legend.position="bottom")

How would I go about wrapping the legend items when the box is set to be at the bottom - say in 2 or 3 lines? At the moment the seven items of the legend are next to each other. I would prefer that they are displayed in two rows (with say four towns on the top row and three towns on the second row).

Many thanks in advance.

KT_1
  • 8,194
  • 15
  • 56
  • 68
  • I would split your question into two separate questions. One of which would be answered by my response (wrapping the text as was apparent in the original question) and a second for how to create multicolumn legends with ggplot. PS: the location of the legend should not change much for the response to your question). – Etienne Low-Décarie Apr 26 '12 at 14:23

3 Answers3

39

Ok, given your edits, you probably wanted this:

library(scales)
i + guides(colour = guide_legend(nrow = 2))

But you may find that you still want to employ the text wrapping technique as well, to get it to fit.

joran
  • 169,992
  • 32
  • 429
  • 468
24

From your example:

df1$places<-sub("-", "- \n ", df1$places)  

i = ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3)

enter image description here

Notes: - You can use gsub to replace all the "-" with "- \n "

Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • Thanks @Etienne Low-Décarie, but this is not quite what I want. I still want the legend at the botton, but the legend text is very long. So instead of the 7 labels on one long row, I want them on two rows (keeping the graph a similar shape to how it is on your example). – KT_1 Apr 26 '12 at 12:51
  • @KatieT You need to be clearer about whether you want to wrap the _text labels_ in the legend across 2 lines, or the _legend items_ themselves, or both. You question only mentioned wrapping the 'legend names', which is what this answer addresses. – joran Apr 26 '12 at 13:52
  • 1
    Do you know how to do this with the legend's title? –  Feb 09 '14 at 01:47
13

Another way to wrap the legend labels which I found very handy is the following (all credits to https://sites.google.com/site/simonthelwall/home/r/ggplot2#TOC-Wrapping-legend-labels):

a <- (1:10)
b <- c(1,1.5,2,4,5,5.3,7,9,9.5,9.8)
places = c("Birmingham","Chester-le-street","Cambridge", "Newcastle-upon-Tyne","Peterborough","Cambridge", "Newcastle-upon-Tyne","Peterborough","Liverpool","Stratford-upon-Avon")
df1 = data.frame(a,b,places)
library(ggplot2)
i = ggplot(df1, aes(x=a, y=b)) + geom_point(aes(colour = places), size=3) 

i + scale_colour_discrete(labels = function(x) str_wrap(x, width = 5))

enter image description here

User2321
  • 2,952
  • 23
  • 46