Unknown colour name in guide_legend

I'm trying to build a legend for a plot which shows in different colours the actual values of the time series to predict, the fit on the training set and the prediction on the validation set (with prediction intervals) . The legend should have the entries Data, Fit on training, Prediction on validation. I've managed to do it for a simplified version (1st version) with only the first 2 variables, which came from the same dataframe (training). However, the code for 2nd version returns ERROR: unrecognized colour name "Data". Is it because it expects "Data" to be a variable in the dataframe df?

future_df <- 
  forecast(fit_best, new_data = validation)%>%

#1st version OK
ob %>%
  ggplot(aes(x = Time)) +
  geom_point(aes(y = dependentVariable, colour = "Data")) +
  geom_point(aes(y = .fitted, colour = "Fitted")) +
  scale_colour_manual(values=c("Data"="black","Fitted"="#D55E00")) +
  guides(colour = guide_legend(title = NULL))

#2nd version ERROR: unrecognized colour name "Data"
p <-  ggplot() +
  geom_point(df,mapping =aes(x=Time, y=dependentVariable),colour ="Data") +
  geom_point(ob,mapping=aes(x=Time, y=.fitted),colour= "Fit on training")+
  geom_point(future_df,mapping=aes(x=Time, y=.mean),colour= "Prediction on validation")+
  geom_errorbar(future_df,mapping=aes(x=Time,ymin = future_df$"95%"$lower, ymax= future_df$"95%"$upper),colour="Prediction on validation")+
  scale_colour_manual(values=c("Data"="black","Fit on training"="blue","Prediction on validation"="red")) +
  guides(colour = guide_legend(title = NULL))

The colour aesthetic has to be a maped inside aes() for the manual legend to work.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.