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)

However, I want to do the following:
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:

Please how can I fix this? Thanks!
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)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With