Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the title of a ggplot2 legend?

Tags:

r

ggplot2

I was looking here but I can't figure it out.

How can I change the word "type" to something else?

like image 936
Brani Avatar asked May 06 '11 12:05

Brani


People also ask

How do I change the legend text in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do I add a title to a legend in R?

In case you need to add a title to the legend, in order to add some description of the elements of the legend, you can use the title argument. Note that you can customize the color of the text with the title. col argument and that you can make a horizontal adjustment of the title with the title. adj argument.


1 Answers

Add

+ labs(colour = "legend title")  

to your ggplot call. Great resource site is also google group for ggplot2.

edit: this assumes that colour is the aesthetic in the legend, e.g. qplot(x,y,colour=z). If another aesthetic is being shown in the legend, use that as the argument instead, e.g. + labs(fill = "legend title") for a raster/image plot.

More generally, if you specify an explicit scale such as scale_colour_continuous, you can set the scale_name argument (warning: the details of the arguments to scales may have changed in recent releases of ggplot2; this description is of version 0.9.2.1).

like image 197
Roman Luštrik Avatar answered Oct 04 '22 14:10

Roman Luštrik