0

I tried to add custom legend into my R plot that uses same x value but two different y values. I used the approach from the following post but it does not work:

Custom legend for multiple layer ggplot

ggplot(combo_plot, aes(x))+
  geom_point(aes(y=y1),size=2,color="black")+
  geom_point(aes(y=y2),size=2,color="red")+
  coord_cartesian(xlim = c(0,1.25),ylim=c(200,450)) + 
  scale_x_continuous(breaks = seq(0,1.25,0.25), minor_breaks = seq(0,1.5,0.25),expand = c(0, 0)) +
  scale_y_continuous(breaks = seq(200,450,25), minor_breaks = seq(200,450,25),expand = c(0, 0)) +
  scale_colour_manual(name = 'the colour', 
                      values =c('black'='black','red'='red'), labels = c('c2','c1'))+
  theme(plot.title = element_text(family = "GE Inspira", color="#666666", face="bold", size=16, hjust=0.5))+
  theme(axis.title = element_text(family = "GE Inspira", color="#666666", face="bold", size=12, hjust=0.5))

Any idea why it does not work in this case?

stefan
  • 90,330
  • 6
  • 25
  • 51
nemox007
  • 29
  • 4
  • 2
    Unfortunately no, as we don't have your data to run your code nor any info about the error your get. Hence one can not even guess. I would therefore suggest to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan Dec 08 '22 at 10:02
  • Just from glancing at the code, I think you you should set `aes(y = y1, colour = 'c1')` and `aes(y = y2, colour = 'c2')` in your point layers and use `scale_colour_manual(values = c("black", "red"))` to get the legend you might want. – teunbrand Dec 08 '22 at 13:55

0 Answers0