0

I'm trying to make a plot with the legend using ggplot in R. I got the graph with the legend, but the colours associated to each line/point are wrong. For instance, I get that GDP is green rather than black and the same for others lines. This is the code I'm using:

ggplot(GDP_pred, aes(x=quarters)) +
  geom_line(aes(y=GDP, color = "GDP")) +
  geom_point(aes(y=lasso, color = "lasso")) +
  geom_point(aes(y=ridge, color = "ridge")) +
  geom_point(aes(y=elnet, color = "elnet")) +
  geom_point(aes(y=rf, color = "rf")) +
  geom_point(aes(y=gbm, color = "gbm")) +
  scale_color_manual(name = "Method",
                     values = c("GDP" = "black", "lasso" = "blue", "ridge" = "red", "elnet" = "green", "rf" = "orange", "gbm" = "pink"),
                     labels = c("GDP", "Lasso", "Ridge", "Elastic Net", "Random Forest", "GBM")) +
  xlab("Quarters")+
  ylab("GDP (billions of chained 2012 dollars)") +
  labs(title = "Prediction of the GDP in the US",
       subtitle = "Q1/2014-Q4/2021")

What could be the problem? How can I solve it?

Thank you in advance.

AVR
  • 1
  • 1
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. If you want to post your data type `dput(NAME_OF_DATASET)` into the console and copy the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 10))` for the first ten rows of data. – stefan Nov 19 '22 at 11:38
  • This said: The issue is most likely that the wrong labels are assigned to the legend entries. Try with a named vector as you did for the colors, i.e. `labels = c(GDP= "GDP", lasso = "Lasso", ...)` – stefan Nov 19 '22 at 11:40
  • 1
    Thank you for your help @stefan . It's working now and it was enough just to change that little thing. The next time, I'll try to post a reproducible example. – AVR Nov 19 '22 at 11:55
  • also, check https://stackoverflow.com/a/3777592/7941188 - you can shorten your code and make it much more "ggplot-y" by making your data long and then using only one variable for y and one for color – tjebo Nov 19 '22 at 12:06

0 Answers0