0

I have below ggplot:

library(ggplot2)
library(quantmod)

Val1 = rnorm(10, 10, 1)
Data1 = data.frame('Name' = 'Name1', 'Date' = as.yearqtr(rep(seq(as.Date('2002-01-01'), length.out = 10, by = '95 day'), 3)), val = Val1, val_low = Val1 - 10, val_up = Val1 + 10)
Val2 = rnorm(10, 20, 1)
Data2 = data.frame('Name' = 'Name2', 'Date' = as.yearqtr(rep(seq(as.Date('2002-01-01'), length.out = 10, by = '95 day'), 3)), val = Val2, val_low = Val2 - 10, val_up = Val2 + 10)

Data = rbind(Data1, Data2)

ggplot() +
geom_line(data = Data1, mapping = aes(x=Date, y=val), color = '#ffb703', linetype='solid') +
geom_line(data = Data2, mapping = aes(x=Date, y=val),color = 'green', linetype='solid')

With this, I can generate below plot:

enter image description here

While this is fine, but I want to create custom legend for Name1 & Name2 with respective colours. I also want that legend to be placed in top middle position.

Is there any way to achieve this?

Brian Smith
  • 740
  • 1
  • 10
  • Brian these are very common questions in the ggplot2 tag on stack overflow. I recommend a google search before asking questions - or check the suggested duplicates that are shown before you post the question. It saves you and us a lot of time and you will also learn more by trying to apply solutions to your problem. Moreover, I believe you've been on stack overflow for a while - ideally don't ask more than one question at a time – tjebo Mar 23 '22 at 15:43

0 Answers0