-2

I am plotting two regression models in one graph by overlaying over each other. But i am unable to display the legend. I am using a code:

ggplot(df, aes(x)) +                    # basic graphical object
    geom_smooth(aes(y = predict(regressor1)), colour = "black") +
    geom_smooth(aes(y = predict(regressor2)), colour = "blue") +
    labs(x = "Distance from the scanner", y = "RMSE (m)") +
    scale_colour_manual(name = "", values = c("black" = "Moo", "blue" = "Coo"), 
                        guide = 'legend') +
    guides(colour = guide_legend(override.aes = list(linetype = c(1, 0), 
           shape = c(NA, 16)))) +
    theme_classic() +
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.title.x = element_text(size = 16),
          axis.title = element_text(size = 16),
          axis.text.x = element_text(size = 14, colour = "black"),
          axis.text.y = element_text(size = 14, colour = "black"))

How can I display the legend manually, here?

alistaire
  • 42,459
  • 4
  • 77
  • 117
james
  • 15
  • 3
  • You are disabling the legend by manually setting `colour` in `aes`. Set colour to the respective variable instead and then sent the colour manually such as `scale_color_manual(values = c("black","blue"))`. See this SO for more detials https://stackoverflow.com/questions/40967101/why-ggplot2-legend-not-show-in-the-graph – Amar May 14 '18 at 01:37
  • First off, welcome to SO! :) I've posted an answer, but generally it's much more helpful when small example data-sets are provided for people to use. In your case, the fact that you're predicting is almost besides the point. You could provide a sample of points you want to plot and ask (just as you did) about how to get the legend to display for the various colours. – LachlanO May 14 '18 at 01:55

1 Answers1

0

The trick to ggplot2 is normally to just have one dataframe everything plots from. You'd be better off combining regressor1 and regressor2 as one column in a dataframe, and setting another one which says which group they're in.

Something like this

regressor_frame <- data.frame(Predictions = c(predict(regressor1), predict(refressor2)),
                              Regressor = c(rep(1, length(regressor1)), rep(2, length(regressor2))
                              )

So now we have a dataframe where one column stores your prediction, and the other stores which regressor that prediction is from!

Then just bind those columns to your current dataframe

df <- cbind(df, regressor_frame)

This will work as they'll have the same number of rows!

Then in the geom_smooth argument we can add a new variable called colour which will plot them separately. Replace those two geom_smooth lines with just

geom_smooth(aes(y=Predictions, colour = Regressor)) 

Then your colour scale argument can look something like this

scale_colour_manual(values = c("Black", "Blue"))

That should give you what you desire.

More generally, if you want something to be depend on a property of your data, then it should be in an aes function. In this case which regressor it comes from affects it, so you should be using it.

Also, ggplot2 is very good at plotting what you give it. Normally you're always better off combining things into one dataframe with a particular column which distinguishes things.

LachlanO
  • 1,152
  • 8
  • 14