0

I have a data frame with two columns. The main one is GDP, the other is the quarter. I make the gdp graph is ok. However, when I add the moving average, it doesn't respect color or order.

graf <- data %>% ggplot(aes(x = TRIMESTRE)) +
geom_line(aes(y = PIB, colour = "black")) +
geom_line(aes(y = rollmean(PIB, 6, na.pad = TRUE), colour = "red"))
graf

graf

Link of file: https://drive.google.com/drive/folders/1PHfkkaBRznQ8H1eMwLqg8ivFLjuw90BG?usp=share_link

It's a question because it's not right, because there are other ways to make the graph... it's more to know where I'm really going wrong.

I appreciate any help

I believe it should be with the colors as entered in the code...

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Try `+ scale_color_manual(values = c(red = "red", black = "black"))` to set your desired colors. – stefan Nov 06 '22 at 20:23

1 Answers1

0

This is probably a duplicate, but you need to put your colo(u)r specifications *outside of the aes() call, i.e.

... +
geom_line(aes(y = PIB), colour = "black") +
geom_line(aes(y = rollmean(PIB, 6, na.pad = TRUE)), colour = "red")

(look carefully, the only thing that changed is the location of two close-parens)

This question raises the same issue (but is unanswered except in a comment).

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453