1

I have been trying for hours to adjust the title of my legend in a ggplot2 line plot, but when I do I get a 'ghost legend' appearing below the real one. I'm using the following code to adjust parts of my legend (coluor, line type and symbol in order):

scale_color_manual(values = c("#FF6600", "green4", "#0099FF")) +                 
  scale_linetype_manual(values=c("solid", "solid", "solid")) +  
  scale_shape_manual(values = c(16, 16, 16)) +   

This comes up with a lovely legend matching my graph. However, when I try to add a title with this code:

labs(linetype='title')+

I get another legend appear below (so both are on the plot at the same time), with the correct title but none of the previous formatting. Obviously I'm coding a separate legend, but I can't fix it!

Also, the real title I want to give my legend requires a subscript '2' for CO2, and I've used the following to account for that in other titles:

expression(CO[2]~concentration~(ppm))  

Thought I'd mention that just in case it changes anything.

Aaaaand while I'm at it, I also want to change the legend orientation to being horizontal rather than a vertical list, and shift it to a corner of the plot space (tried legend.position = c(0.9, 0.8) which has worked before but not in this case).

My problem is that as I try each new thing, the errors compound and I can't move forward.

I realise this post is far from elegant, but any help would be greatly appreciated. I'm sorry I have not provided any example code, I'm an R newbie and totally swimming.
Thanks in advance!

steveb
  • 5,382
  • 2
  • 27
  • 36
Amanda
  • 107
  • 2
  • 10
  • 1
    Please provide a reproducible data & code http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Tung Aug 18 '16 at 06:28
  • 1
    Please make a reproducible example - but your first problem is probably because if you want legends to be merged, they all need to have the same name, ie labs(linetype = "title", colour = "title", ...) – Richard Telford Aug 18 '16 at 09:10
  • Welcome to SO, if there ever was a case where it was important to **show us** what you're referring to instead of **telling us**, it's this one. – Pierre L Aug 18 '16 at 13:37

1 Answers1

1

You may be too new to create a reproducible example, so here's a try and an answer:

library(ggplot2)
set.seed(100)
df1 <- subset(diamonds, cut %in% (levels(diamonds$cut)[1:3]))[sample(100),]
p <- ggplot(df1, aes(x=carat, y=price, colour=cut, shape=cut, linetype=cut, group=cut))
p <- p + geom_line() 
p + scale_color_manual(values = c("#FF6600", "green4", "#0099FF")) +                 
  scale_linetype_manual(values=c("solid", "solid", "solid")) +  
  scale_shape_manual(values = c(16, 16, 16)) +
  labs(linetype='title')

enter image description here

Is this the "ghost legend"?

Here's a workaround. The title should also be placed for the other aesthetics:

p + scale_color_manual(values = c("#FF6600", "green4", "#0099FF")) +                 
  scale_linetype_manual(values=c("solid", "solid", "solid")) +  
  scale_shape_manual(values = c(16, 16, 16)) +
  labs(linetype=expression(CO[2]~concentration~(ppm)),
       colour=expression(CO[2]~concentration~(ppm)),
       shape=expression(CO[2]~concentration~(ppm)))

enter image description here

The extra legend is gone. This happened because of the differing names. We repeated the name for all of the aesthetics.

A shorter option is to add guide="none" to the other scales:

p + scale_color_manual(name=expression(CO[2]~concentration~(ppm)),
                       values = c("#FF6600", "green4", "#0099FF")) +                 
  scale_linetype_manual(guide="none",values=c("solid", "solid", "solid")) +  
  scale_shape_manual(guide="none",values = c(16, 16, 16))
Pierre L
  • 28,203
  • 6
  • 47
  • 69
  • Oh my goodness @Pierre Lafortune you interpreted my poor explanation perfectly and have solved my problem, thank-you so very much, I really appreciate your patience! Everyone else, thanks for providing the opportunity for me to add more to my question, but I'm afraid Pierre was right and I just didn't know how to provide an example. I've taken note of what Pierre did, and will try to do so in the future. What a wonderful end to my week :-D – Amanda Aug 19 '16 at 04:38