0

I am trying to produce a legend with math symbols. I tried different options but none works. I don't find a way to assign labels directly to the guide_legend and ggplot doesn't seem to understand expressions in factor levels. Also giving label_bquote to factor levels fails (in any case, the ggplot2 documentation says it works for facets strips).

In the example below I want to change L1, L2 and Linf to L's with subscripts 1, 2, and the infinity symbol, respectively.

a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)

df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
                norm = factor(rep(1:3, each = 3), labels =  expression(L[1], L[2], L[inf])))

ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) + 
geom_line() + geom_point() 

plot

Marco Stamazza
  • 836
  • 9
  • 15
  • I vote to reopen because this legend is for *two* aesthetics while the other question is for one aesthetic only.. Here one has to do something like `scale_color_manual(values=c("red", "blue", "green"), labels=labels) + scale_shape_manual(values=c(1,2,3), labels=labels)` with `labels <- expression(L[1], L[2], L[infinity])`. – Stéphane Laurent Sep 01 '18 at 13:27
  • @StéphaneLaurent It seems extremely straightforward (i.e. trivial) to adapt the solution from the linked post to multiple aesthetics. I don't really see how this is significantly different. But I'm happy to re-open if there is more support for a re-open call. – Maurits Evers Sep 01 '18 at 13:38
  • @MauritsEvers For me it was not straightforward. I had to search a certain time to find this solution (cause I didn't use ggplot for a certain time too). If the question is re-opened one should mention this difference in the title. – Stéphane Laurent Sep 01 '18 at 13:43
  • @MauritsEvers The answer in the original questions works but one needs to explicitly assign the labels to both scales, as Stéphane pointed out. What if I change the title and you reopen the question? – Marco Stamazza Sep 01 '18 at 13:46
  • @MarcoStamazza Done – Maurits Evers Sep 01 '18 at 13:49

1 Answers1

2

I had overlooked that a similar question had already been asked and the answers give the right hint: assign the labels via the scales, as shown below. Note that the both scales must be defined and that both must have the same name and labels, in order to avoid producing two legends.

a1 = exp(seq(1, 3))
a2 = exp(seq(1, 3) + 0.2)
a3 = exp(seq(1, 3) + 0.4)

df = data.frame(coefficients = c(a1, a2, a3), order = rep(1:3, 3),
                  norm = factor(rep(1:3, each = 3), labels = c("L1", "L2", "Linf")))

ggplot(df, aes(x = order, y = coefficients, colour = norm, shape = norm)) + 
  geom_line() + geom_point() + 
scale_colour_manual(name = "norm", values = c("blue", "red", "green"), 
                     labels = expression(L[1], L[2], L[infinity])) + 
scale_shape_discrete(name = "norm", 
                      labels = expression(L[1], L[2], L[infinity]))

enter image description here

Marco Stamazza
  • 836
  • 9
  • 15