Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 - combining shape and color legend with common title

Tags:

r

ggplot2

I have a data frame that looks like this.

    lambda  lambdas mu  p
1   0.5 0.25 3.6 1.931105
2   0.5 0.25 3.8 2.150458
3   0.5 0.25 4.0 2.264805
4   0.5 0.25 4.2 2.337036
5   0.5 0.25 4.4 2.385832
6   0.5 0.25 4.6 2.420036
7   0.5 0.25 4.8 2.444610
8   0.5 0.25 5.0 2.462598
9   0.5 0.25 5.2 2.475974
10  0.5 0.25 5.4 2.486068
11  0.5 0.25 5.6 2.493801
12  0.5 0.25 5.8 2.499824
13  0.5 0.25 6.0 2.504604
14  0.5 0.25 6.2 2.508482
15  0.5 0.25 6.4 2.511708
16  0.5 0.25 6.6 2.514465
17  0.5 0.25 6.8 2.516892
18  0.5 0.25 7.0 2.519091
19  0.5 0.25 7.2 2.521137
20  0.5 0.25 7.4 2.523088
21  0.5 0.25 7.6 2.524984
22  0.5 0.25 7.8 2.526858
23  0.5 0.25 8.0 2.528729
24  0.5 0.3 4.0 1.453073
25  0.5 0.3 4.2 1.676078
26  0.5 0.3 4.4 1.769432
27  0.5 0.3 4.6 1.829259
28  0.5 0.3 4.8 1.871153
29  0.5 0.3 5.0 1.901801
30  0.5 0.3 5.2 1.924841
31  0.5 0.3 5.4 1.942502
32  0.5 0.3 5.6 1.956246
33  0.5 0.3 5.8 1.967078
34  0.5 0.3 6.0 1.975710
35  0.5 0.3 6.2 1.982661
36  0.5 0.3 6.4 1.988317
37  0.5 0.3 6.6 1.992968
38  0.5 0.3 6.8 1.996834
39  0.5 0.3 7.0 2.000085
40  0.5 0.3 7.2 2.002856
41  0.5 0.3 7.4 2.005248
42  0.5 0.3 7.6 2.007343
43  0.5 0.3 7.8 2.009207
44  0.5 0.3 8.0 2.010890
45  0.5 0.35 4.8 1.330792
46  0.5 0.35 5.0 1.415920
47  0.5 0.35 5.2 1.466734
48  0.5 0.35 5.4 1.502578
49  0.5 0.35 5.6 1.529478
50  0.5 0.35 5.8 1.550365
51  0.5 0.35 6.0 1.566948
52  0.5 0.35 6.2 1.580327
53  0.5 0.35 6.4 1.591256
54  0.5 0.35 6.6 1.600275
55  0.5 0.35 6.8 1.607783
56  0.5 0.35 7.0 1.614081
57  0.5 0.35 7.2 1.619400
58  0.5 0.35 7.4 1.623922
59  0.5 0.35 7.6 1.627789
60  0.5 0.35 7.8 1.631118
61  0.5 0.35 8.0 1.634000
62  0.5 0.4 6.0 1.093701
63  0.5 0.4 6.2 1.177399
64  0.5 0.4 6.4 1.214441
65  0.5 0.4 6.6 1.240465
66  0.5 0.4 6.8 1.260447
67  0.5 0.4 7.0 1.276454
68  0.5 0.4 7.2 1.289608
69  0.5 0.4 7.4 1.300606
70  0.5 0.4 7.6 1.309920
71  0.5 0.4 7.8 1.317887
72  0.5 0.4 8.0 1.324755

Using "scale_fill_discrete", I can combine the shape and color legends into one, but the title of the legend is not working. In other words:

plot = ggplot(data, aes(x = mu, y = p, color = lambdas, shape = lambdas))
plot = plot + geom_line()
plot = plot + geom_point(size = 2.5)
plot = plot + scale_fill_discrete(name = expression(lambda^{s}))
plot = plot + ylim(0,3)
plot = plot + ggtitle(expression(paste(p, ' when ', lambda, '= 0.5', sep = ''))) 
plot = plot + xlab(expression(mu)) + ylab(expression(p))
plot

this code gives the picture below. enter image description here

On the other hand, if I use "scale_shape_discrete" and "scale_color discrete", then the title works out, but the legends are now separated. In other words:

plot = ggplot(data, aes(x = mu, y = p, color = lambdas, shape = lambdas))
plot = plot + geom_line()
plot = plot + geom_point(size = 2.5)
plot = plot + scale_shape_discrete(name = expression(lambda^{s}))
plot = plot + scale_color_discrete(name = expression(lambda^{s}))
plot = plot + ylim(0,3)
plot = plot + ggtitle(expression(paste(p, ' when ', lambda, '= 0.5', sep = ''))) 
plot = plot + xlab(expression(mu)) + ylab(expression(p))
plot

this code gives the picture below. enter image description here

Is there any way to put the legends together AND have the title as in the second picture? Thank you.

like image 890
BrianDIngels Avatar asked Apr 23 '15 13:04

BrianDIngels


1 Answers

Well, originally ggplot uses combined legend if values and name are common for two or more aes. In your case this is so (you specify identical names), but presumably it's expression that messes things up, so that ggplot think legend titles are different.

Let's outsmart him by using l <- expression(lambda^{s}) and

plot + labs(color=l, shape=l)

omitting scale_shape and scale_color completely.

enter image description here

like image 189
tonytonov Avatar answered Nov 08 '22 03:11

tonytonov