0

I have a ggplot for the different forecasts I used. Can you please help me add labels for the different forecasts? I pasted a copy of the code and a picture of the current graph. Don't mind the title, caption, and subtitle.

ggplot(Forecast_df, aes(date)) + 
  geom_tile(aes(alpha = Recession, y = 1), 
            fill = "grey", height = Inf) +
  scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1" = "Recession"))+
  geom_line(aes(y = ERVOLY), col = "black", size = .8)+
  geom_line(aes(y = ERVOL_vanilla), col = "blue", size = .8)+
  geom_line(aes(y = ERVOL_twofactor), col = "green", size = .8)+
  geom_line(aes(y = ERVOL_threefactor), col = "orange", size = .8)+
  ylab('Compounded Returns Next 12 Months (%)')+
  labs(x = NULL, alpha = "Economic Cylcle", color = "Economic Variable",
   title = "Rising Inflation Expectations in 2021",
   caption = 'Cleveland Fed Model of 10-year Inflation Expectations',
   subtitle = '"Directionality Matters"')+
  theme(plot.background = element_blank(),
    panel.grid.minor = element_blank(),
    panel.grid.major= element_blank(),
    text = element_text( color = "#003366"))

ggplot

Here is a subset of the data (I hope this helps):

dput(head(Forecast_df, 10))


structure(list(INFEX = c(5.3755618, 5.2721824, 5.5661154, 5.407864, 
5.4896902, 5.1106146, 4.5055244, 4.6342849, 4.5102793, 4.6395631
), ERVOLY = c(NA, -1.0061220776063, -3.28324764731475, 
-2.69553459574718, 
-0.282378595190325, 0.452399878131127, -1.98818474608972, 
-2.02453229878801, 
-2.93519956175393, -3.3615903958241), date = structure(c(6695, 
6726, 6756, 6787, 6818, 6848, 6879, 6909, 6940, 6971), class = 
"Date"), 
Expansion = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1), Recession = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), ERVOLY_lag1 = c(NA, NA, -1.0061220776063, 
-3.28324764731475, -2.69553459574718, -0.282378595190325, 
0.452399878131127, -1.98818474608972, -2.02453229878801, 
-2.93519956175393), ERVOLY_lag2 = c(NA, NA, NA, -1.0061220776063, 
-3.28324764731475, -2.69553459574718, -0.282378595190325, 
0.452399878131127, -1.98818474608972, -2.02453229878801), 
ERVOL_vanilla = c(NA, NA, NA, 0.177969523019507, 1.0226601086044, 
2.95027193254105, 2.8634033844251, 0.549544852368316, 
0.594205304832909, 
-0.162404463789272), ERVOL_twofactor = c(NA, NA, NA, 
0.0942849089685699, 
0.91074093410051, 2.85616681306392, 2.81947921688057, 
0.512592472075962, 
0.537863435836189, -0.222954102109839), ERVOL_threefactor = c(NA, 
NA, NA, -0.313401798360953, 0.474416484910883, 2.44479892124736, 
2.49154716920072, 0.199652448822007, 0.23023289110162, 
-0.537285874249354
), ERVOL_AR2 = c(NA, NA, NA, -3.24926540322977, -2.50081867707103, 
-0.136146400989865, 0.424591722149989, -2.06171545794125, 
-1.92397323156163, -2.82956410641666), fe_vanilla = c(NA, 
NA, NA, -2.87350411876669, -1.30503870379473, -2.49787205440993, 
-4.85158813051482, -2.57407715115632, -3.52940486658684, 
-3.19918593203483), fe_twofactor = c(NA, NA, NA, -2.78981950471575, 
-1.19311952929083, -2.4037669349328, -4.80766396297029, 
-2.53712477086397, 
-3.47306299759012, -3.13863629371426), fe_threefactor = c(NA, 
NA, NA, -2.38213279738623, -0.756795080101207, -1.99239904311623, 
-4.47973191529044, -2.22418474761001, -3.16543245285555, 
-2.82430452157474), fe_AR2 = c(NA, NA, NA, 0.553730807482581, 
2.21844008188071, 0.588546279120992, -2.41277646823971, 
0.0371831591532463, 
-1.0112263301923, -0.532026289407435)), row.names = c(NA, 
10L), class = "data.frame")
chemdork123
  • 12,369
  • 2
  • 16
  • 32
MacroChair
  • 57
  • 5
  • Does this answer your question: [Add legend to ggplot2 line plot](https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot) – stefan Dec 06 '21 at 22:02
  • I would think so, but there is a piece of the puzzle I am missing if I counted these forecasts under the "Economic Variable" part of my code. Simply adding the scale_color_manual function with each color does not do the trick. – MacroChair Dec 06 '21 at 22:48
  • To help us to help you would you mind to provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste 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, 20))` for the first twenty rows of data. – stefan Dec 06 '21 at 22:56

1 Answers1

0

You can get the result you're looking for using the first method I'll outline here, although arguably a better way would be to first use gather() or pivot_longer() or melt() or reshape() to have a structure more resembling Tidy Data. However, in the interest of space and time, I'll show the direct way here.

The reason why adding scale_color_manual() to your plot doesn't help is because the scale_* functions only recolor/reshape an existing scale. Your original plot does not have a scale for color created, so you would be trying to recolor a scale that does not exist. :)

The only scale your plot created was one for alpha=, and the reason is because you specified that within aes(). This is the "secret" to creating a scale with ggplot2, because the plotting is setup to map an aesthetic attribute to a column in your dataframe. There are positional aesthetics (like x, y, width, etc..) and then there are graphical aesthetics (like color, fill, alpha, size, etc...). Positional aesthetics map your data to a location in the plot area, while the graphical aesthetics change the way a geom looks depending on your data. For graphical aesthetics, a legend is also created. So, to make a legend appear, you need your graphical aesthetics specified within aes().

Specifying a value in aes() will set the name of the aesthetic in the legend. Colors are then applied using the default scale... or via another scale you set using a scale_* command. Here's an example without scale_color_manual():

(Note: had to remove scale_alpha_continous(), since your dput() only has one value for the "Recession" column).

p <- ggplot(Forecast_df, aes(date)) + 
  geom_tile(aes(alpha = Recession, y = 1), 
            fill = "grey", height = Inf) +
  # scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("Expansion", "Recession"))+
  geom_line(aes(y = ERVOLY, col="ERVOLY"), size = .8)+
  geom_line(aes(y = ERVOL_vanilla, col="Vanilla ERVOL"), size = .8)+
  geom_line(aes(y = ERVOL_twofactor, col="Twofactor ERVOL"), size = .8)+
  geom_line(aes(y = ERVOL_threefactor, col="Threefactor ERVOL"), size = .8)+
  ylab('Compounded Returns Next 12 Months (%)')+
  labs(x = NULL, alpha = "Economic Cylcle", color = "Economic Variable",
       title = "Rising Inflation Expectations in 2021",
       caption = 'Cleveland Fed Model of 10-year Inflation Expectations',
       subtitle = '"Directionality Matters"')+
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major= element_blank(),
        text = element_text( color = "#003366"))
p

enter image description here

You see, a legend is created and the keys are labeled accordingly. You can then add the colors you want with scale_color_manual():

p + scale_color_manual(values=c(
    "ERVOLY" = "black",
    "Vanilla ERVOL" = "blue",
    "Twofactor ERVOL"="green",
    "Threefactor ERVOL"="orange"))

enter image description here

chemdork123
  • 12,369
  • 2
  • 16
  • 32