4

I am trying to create a plot containing two lines with different shapes and colour.

I have checked a number of similar questions online but I have not been successful. I have been able to do the following so far

library(reshape2)
library(ggplot2)
library(latex2exp)
v1 <-c(0.000120,-0.000085,-0.000018,0.000005)
v2 <- c(0.000164,0.000041,-0.000032,0.000031)
v3 <- c(500,1000,5000,10000)
dfr <- data.frame(rate1=v1,rate2=v2,quantity=v3)
dfr <- melt(dfr,id='quantity',value.name="res")
ggplot(dfr, aes(x=quantity, y=res,group=variable,shape=variable)) +
  geom_line(size=1, aes(linetype=variable,colour=variable)) + 
  geom_point( size=4,aes(colour=variable))+ coord_cartesian(ylim = c(-0.0001,0.0002)) + 
  scale_x_continuous(breaks=c(500,1000,5000,10000))+
  scale_linetype_manual(values=c("solid", "longdash"))+
  geom_hline(yintercept = 0,linetype="dotted",size=1)

First plot

However, I want to do the following:

  1. Replace the legend texts/labels: rate1 and rate2 with two Greek lambda symbols.
  2. Finally, hide the legend title variable.

When I try to include this: scale_colour_manual( values=c('#F8766D','#00BFC4'),labels = unname(TeX(c(" $\\lambda_1$", "$\\lambda_2$")))), so as to change the legend text, I get an extra legend below:

second plot with extra legend

Please how can I fix this? Thanks!

Phil
  • 7,287
  • 3
  • 36
  • 66
dms
  • 69
  • 4

1 Answers1

2

The issue is that by changing the labels in scale_linetype but not for the other scales (color and shape) ggplot2 will no longer merge them into one legend. Hence you have the change the labels for the other scales as well. However, using Tex() I did not manage to make this work. But following this post using bquote worked fine. Finally, to get rid of the legend title simply use labs() to set the title for all three scales to NULL

EDIT Thanks to @mischva for checking and pointing out that using labels <- unname(TeX(c(" $\\lambda_1$", "$\\lambda_2$"))) will also work fine. Interestingly it does not work if one puts it directly into the three scales functions. That's what I tried.

library(reshape2)
library(ggplot2)
library(latex2exp)
v1 <-c(0.000120,-0.000085,-0.000018,0.000005)
v2 <- c(0.000164,0.000041,-0.000032,0.000031)
v3 <- c(500,1000,5000,10000)
dfr <- data.frame(rate1=v1,rate2=v2,quantity=v3)
dfr <- melt(dfr,id='quantity',value.name="res")

labels <- c(bquote(lambda[1]), bquote(lambda[2]))

ggplot(dfr, aes(x=quantity, y=res,group=variable,shape=variable)) +
  geom_line(size=1, aes(linetype=variable,colour=variable)) + 
  geom_point( size=4,aes(colour=variable))+ coord_cartesian(ylim = c(-0.0001,0.0002)) + 
  scale_x_continuous(breaks=c(500,1000,5000,10000))+
  scale_linetype_manual(values=c("solid", "longdash"), labels = labels)+
  scale_shape_discrete(labels = labels)+
  scale_colour_discrete(labels = labels) +
  labs(color = NULL, linetype = NULL, shape = NULL) +
  geom_hline(yintercept = 0,linetype="dotted",size=1)

Phil
  • 7,287
  • 3
  • 36
  • 66
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 2
    I was on the same track, but you were faster. You can change your `labels` to `labels <- unname(TeX(c(" $\\lambda_1$", "$\\lambda_2$")))`, then it will work like OP suggested with any `LaTex` code through `latex2exp`. – mischva11 Jun 26 '20 at 13:21
  • @stefan thanks a lot, problem solved! I learnt quite a few new things from your explanation. – dms Jun 26 '20 at 13:59
  • 1
    @mischva11 thanks for your contribution. `labels <- unname(TeX(c(" $\\lambda_1$", "$\\lambda_2$")))` also works just fine too. – dms Jun 26 '20 at 14:01