Tell me more ×
Cross Validated is a question and answer site for statisticians, data analysts, data miners and data visualization experts. It's 100% free, no registration required.

I have a plot I'm making in ggplot2 to summarize data that is from a 2 x 4 x 3 celled dataset. I have been able to make panels for the 2 leveled variable using facet_grid(. ~ Age) and to set the x and y axes using aes(x=4leveledVariable, y=DV). I used aes(group=3leveledvariable, lty=3leveledvariable) to produce the plot so far. This gives me a visualization that is paneled by the 2 level variable, with the X axis representing the 4 leveled variable and different lines ploted within the panels for the 3 leveled variable. But the key for the 3 leveled variable is titled with the 3 leveled variable's name and I want it to be a title that has a character space in it. How can I rename the title of the legend? I've done quite a few google searches and looked through the ggplot2 manual but I haven't managed to solve this problem yet, so I'm turning to the CrossValidated community.

Things I've tried that don't seem to work (where abp is my ggplot2 object):

 abp <- abp + opts(legend.title="Town Name")
 abp <- abp + scale_fill_continuous("Town Name")
 abp <- abp + opts(group="Town Name")
 abp <- abp + opts(legend.title="Town Name")

Example data: ex.data <- data.frame(DV=rnorm(2*4*3),V2=rep(1:2,each=4*3),V4=rep(1:4,each=3),V3=1:3)

Thanks

share|improve this question
2  
@drknexus - if you can share your data, that would be helpful, or point to a similar dataset within R that has the same shape as your data. – Chase Nov 29 '10 at 21:09
@Chase: Added an example dataset. – Russell S. Pierce Nov 29 '10 at 21:30
@drknexus - can you post the code as you currently have it? – Chase Nov 29 '10 at 21:34
Can someone with the editing powers hit that nifty 101010 button and format the code above? – Chase Nov 29 '10 at 23:00
1  
Everybody here seems very knowledgeable but I know for ggplot related questions I find their google group to be incredibly helpful. groups.google.com/group/ggplot2 – Dason Dec 6 '10 at 19:48
show 2 more comments

closed as off topic by Andy W, gung, cardinal, Macro, Peter Flom Jan 29 at 12:38

Questions on Cross Validated are expected to relate to statistics within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

3 Answers

up vote 17 down vote accepted

You can change the title of the legend by modifying the scale for that legend. Here's an example using the CO2 dataset

library(ggplot2)

p <- qplot(conc, uptake, data = CO2, colour = Type) + scale_colour_discrete(name = "Fancy Title")
p <- p + facet_grid(. ~ Treatment)
p 

EDIT:

Using the example data from above, here is a working solution. I think this mimics the plot that @drknexus is trying to create. As a side note, if anyone can explain why we have to treat V3 as a factor for it to be mapped to the legend, I'd appreciate it.

p <- qplot(V4, DV, data = ex.data, geom = "line", group = V3, lty = factor(V3)) 
p <- p + scale_linetype_discrete(name = "Fancy Title") + facet_grid(. ~ V2)
p 

alt text

share|improve this answer
I think what I'm missing is a function like scale_color_discrete that ties into the "group" or "lty" specification in ggplot(data=ex.daata, aes(x=V4, y=DV, group=V3, lty=V3)) – Russell S. Pierce Nov 29 '10 at 21:31
1  
Ah I found it: scale_linetype_discrete(name="bob") – Russell S. Pierce Nov 29 '10 at 21:35
@Chase: Edit with the scale_linetype_discrete bit and I'll accept your answer. – Russell S. Pierce Nov 29 '10 at 21:36
1  
@drknexus - the issue with that is in my working example, the appropriate command is scale_colour_discrete() and the code you are working with is obviously a bit different. I don't know what that code is... – Chase Nov 29 '10 at 22:09
3  
@Chase Re: "why we have to treat V3 as a factor" scale_linetype_discrete expects a variable with discrete values (factor or character, from plyr::is.discrete), check out the src code on Github, j.mp/ejaRRT. Nice response (+1). – chl Nov 29 '10 at 23:29
show 1 more comment

Besides the well known and excellent documentation on Hadley's page, the not-so-known reference on Git Hub really helps with these kind of issues.

share|improve this answer
2  
Alternatively, theme_get() provides you with the same reference in the console. – Brandon Bertelsen Jun 20 '11 at 4:55
Thx Brandon, spread the message! Nice SO post btw. – hans0l0 Jun 20 '11 at 6:37
I'm trying! That function, once I found it (trial and error) saved me hours searching through the ggplot2 mailing list. – Brandon Bertelsen Jun 20 '11 at 7:17

Another option is to use

p + labs(aesthetic='custom text')     

For example, Chase's example would look like:

library(ggplot2)

ex.data <- data.frame(DV=rnorm(2*4*3),V2=rep(1:2,each=4*3),V4=rep(1:4,each=3),V3=1:3)
p <- qplot(V4, DV, data=ex.data, geom="line", group=V3, linetype=factor(V3)) + facet_grid(. ~ V2)
p + labs(linetype='custom title')

and yield the figure: enter image description here

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.