Wurschti
Wurschti

Reputation: 33

R - Adding legend to ggplot graph for regression lines

I do a Multiple Linear Regression in R, where I want to add a simple legend to a graph (ggplot). The legend should show the points and fitted lines with their corresponding colors. So far it works fine (without legend):

ggplot() +
  geom_point(aes(x = training_set$R.D.Spend, y = training_set$Profit),
             col = 'red') +
  geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor, newdata = training_set)),
            col = 'blue') +
  geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor_sig, newdata = training_set)),
            col = 'green') +
  ggtitle('Multiple Linear Regression (Training set)') +
  xlab('R.D.Spend [k$]') + 
  ylab('Profit of Venture [k$]')

enter image description here

How can I add a legend here most easily?

I tried the solutions from similar question, but did not succeed (add legend to ggplot2 | Add legend for multiple regression lines from different datasets to ggplot)

So, I appended my original model like this:

ggplot() +
  geom_point(aes(x = training_set$R.D.Spend, y = training_set$Profit),
             col = 'p1') +
  geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor, newdata = training_set)),
            col = 'p2') +
  geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor_sig, newdata = training_set)),
            col = 'p3') +
  scale_color_manual(
    name='My lines',
    values=c('blue', 'orangered', 'green')) +
  ggtitle('Multiple Linear Regression (Training set)') +
  xlab('R.D.Spend [k$]') + 
  ylab('Profit of Venture [k$]')

But here I am getting the error of "Unknown colour name: p1". which makes somewhat sense, as I do not define p1 above. How can I make the ggplot recognise my intended legend?

Upvotes: 0

Views: 1289

Answers (1)

LMc
LMc

Reputation: 18692

Move col into the aes and then you can set the color using scale_color_manual:

library(ggplot2)
set.seed(1)
x <- 1:30
y <- rnorm(30) + x

fit <- lm(y ~ x)
ggplot2::ggplot(data.frame(x, y)) + 
  geom_point(aes(x = x, y = y)) + 
  geom_line(aes(x = x, y = predict(fit), col = "Regression")) + 
  scale_color_manual(name = "My Lines",
                     values = c("blue"))

enter image description here

Upvotes: 1

Related Questions