0

I cannot figure out how to manually set up a legend for this plot. I just want to make dot, dashed, line color legend.

enter image description here

The current code looks like this:

x1 <- data[[paste(event_list[i],"_1",sep="")]]
x2 <- data[[paste(event_list[i],"_2",sep="")]]
x3 <- data[[paste(event_list[i],"_3",sep="")]]
  
x = (x1 + x2 + x2)/ 3
y <- data[[paste(event_list[i],"_AI",sep="")]]  
df <- data.frame(x,y)
fit <- deming(x ~ y,data = df)
ggplot(data.frame(x, y), aes(x = x, y = y)) +

geom_point() +
geom_abline(intercept = 0, slope = 1, color = "black", linetype = "dashed") +
geom_abline(intercept = fit$coefficients[1], slope = fit$coefficients[2], color = "red") +
geom_abline(intercept = fit$ci[3], slope = fit$ci[4], color = "blue", linetype = "dashed") +
geom_abline(intercept = fit$ci[1], slope = fit$ci[2], color = "blue", linetype = "dashed") +
scale_color_manual(name = "Linear Regression", values = c("black", "red", "blue"), labels = c("Identity Line", "Deming Coef", "Confidence Interval")) +
scale_linetype_manual(name = "Linear Regression", values = c("dashed", "solid", "dashed"), labels = c("Identity Line", "Deming Coef", "Confidence Interval")) +
labs(x = "X", y = "Y")+
ggtitle(paste(event_list[i],"Deming Regression")) +
xlab("Variables") +
ylab("Reference") 
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
호야무
  • 11
  • 1
  • Take a look here, that example exactly illustrates what you want to achieve. https://www.statology.org/ggplot-manual-legend/ – Merijn van Tilborg Mar 23 '23 at 08:22
  • There are many questions like this on stack overflow. Here is one that has exactly the same title. https://stackoverflow.com/questions/17148679/construct-a-manual-legend-for-a-complicated-plot?rq=1 – tjebo Mar 23 '23 at 08:42

0 Answers0