r - ggplot2 - combining shape and color legend with common title -


i have data frame looks 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", can combine shape , color legends one, title of legend 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 picture below. enter image description here

on other hand, if use "scale_shape_discrete" , "scale_color discrete", title works out, legends 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 picture below. enter image description here

is there way put legends , have title in second picture? thank you.

well, ggplot uses combined legend if values , name common 2 or more aes. in case (you specify identical names), presumably it's expression messes things up, ggplot think legend titles different.

let's outsmart him using l <- expression(lambda^{s}) ,

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

omitting scale_shape , scale_color completely.

enter image description here


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

utf 8 - split utf-8 string into bytes in python -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -