Home > Blockchain >  Issues with ggplot line charts
Issues with ggplot line charts

Time:12-08

I have no idea why I am getting these intermittent lines in my grey bars in this ggplot and why the legend is not in the same order for each chart. Can you please help me fix this? Here is my code and a sub-sample of the data. There are no missing values used in the variables used for the ggplot.

Forecast_plot_vanilla <- 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="Actual Returns"), size = .8) 
  ylab('Compounded Returns Next 12 Months (%)') 
  labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
       title = "Rising Inflation Expectations in 2021",
   subtitle = '"Actual Returns vs. AR(2) Model"') 
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major= element_blank()) 
  scale_color_manual(values=c(
    "Actual Returns" = "black"))

Forecast_plot_vanilla 

Here is a subset of the data

dput(head(Forecast_df, 10))

structure(list(INFEX = c(2.9961298, 3.0273917, 2.9937871, 3.1838587, 
3.3951941, 3.3770226, 3.4259232, 3.5322081, 3.3882517, 3.4201416
), ERVOLY = c(-8.77748883028093, -14.8416404279317, -9.02974052805671, 
-16.8303572233488, -18.7945822035285, -14.8777818020646, -14.519413479622, 
-15.6444594874336, -16.4584536211307, -19.3396158494671), date = structure(c(10926, 
10957, 10988, 11017, 11048, 11078, 11109, 11139, 11170, 11201
), 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(-1.73631437680128, 
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488, 
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336, 
-16.4584536211307), ERVOLY_lag2 = c(0.826035972924055, -1.73631437680128, 
-8.77748883028093, -14.8416404279317, -9.02974052805671, -16.8303572233488, 
-18.7945822035285, -14.8777818020646, -14.519413479622, -15.6444594874336
), ERVOL_vanilla = c(-0.86668297866284, -7.49039800107863, -12.8520494772519, 
-6.48762453981438, -14.3713769094631, -15.6549081560956, -11.6216205302654, 
-11.4929227320897, -12.7635844039787, -13.4320398169549), ERVOL_twofactor = 
c(-0.827189244172975, 
-7.46356815233837, -12.8829528152664, -6.60151291973358, -14.4185578850936, 
-15.7770171682792, -11.7743725647707, -11.6110549705504, -12.8692222977468, 
-13.5488959720254), ERVOL_threefactor = c(-0.923203527477895, 
-7.4933399582189, -12.8706459303638, -6.71159009749288, -14.4375623857157, 
-15.8077186223044, -11.8668964436481, -11.7032745502137, -12.9273687852697, 
-13.6070975934917), ERVOL_AR2 = c(-1.83716914438012, -8.676457359149, 
-14.2220911732806, -7.99373494394322, -16.1873977312507, -17.5901523147866, 
-13.5440099520562, -13.4658503981688, -14.6133744990458, -15.3449421615658
)), row.names = 140:149, class = "data.frame")

Messed up Recession bars

Messed up legends

CodePudding user response:

The issue is that is that geom_tile chooses the same width for each tile. However, because months differ in the number of days the width is based on the number of days in February, i.e. you get a width of 28 days. For all other months you end up with some blank space of up to 3 days which are your grey lines.

One option to get rid of the the grey lines would be to switch to geom_rect where I make use lubridate::ceiling_date to get the end date for each "tile":

library(ggplot2)

Forecast_df$Recession[3:5] <- 1

ggplot(Forecast_df, aes(date))   
  geom_rect(aes(xmin = date, xmax = lubridate::ceiling_date(date, "month"), 
                ymin = -Inf, ymax = Inf, alpha = Recession), 
            fill = "grey")  
  geom_line(aes(y = ERVOLY, col="Actual Returns"), size = .8)  
  scale_alpha_continuous(range = c(0, 1), breaks = c(0, 1), labels = c("0" = "Expansion", "1" = "Recession")) 
  ylab('Compounded Returns Next 12 Months (%)') 
  labs(x = NULL, alpha = "Economic Cylcle", color = "Model Forecast",
       title = "Rising Inflation Expectations in 2021",
       subtitle = '"Actual Returns vs. AR(2) Model"') 
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        panel.grid.major= element_blank()) 
  scale_color_manual(values=c("Actual Returns" = "black"))

  • Related