0

I've made a graph using the code on Historical Variance Error Decomposition plot in R and got a good looking graph. And I would like to add the original data line to this graph. First one is the one I got and second one is the one I'm trying to make. I've tried melting it into the dataframe but then it does not give me a line but gives me bars. Any help is appreciated!

Mine

I want my to look like this

  ex <- HD[,,1] 
  ex1 <- as.data.frame(ex) # transforming the HD matrix as data frame #
  ex2 <- ex1[3:216,1:3] # taking our the first 2 rows as they are N/As #
  colnames(ex2) <- c("Inflation", "Interest", "Unemployment") # renaming columns #
  ex2$Period <- 1:nrow(ex2) # creating an id column #
  col_id <- grep("Period", names(ex2)) # setting the new variable as id #
  ex3 <- ex2[, c(col_id, (1:ncol(ex2))[-col_id])] # moving id variable to the first column #
  molten.ex <- melt(ex3, id = "Period") # melting the data frame #
  
  molten.ex$variable <- factor(molten.ex$variable, levels = c("Inflation","Interest","Unemployment"))
  
  ggplot(molten.ex, aes(x = Period, y = value, fill = variable)) + 
    geom_bar(stat = "identity", width = 0.6) + 
    guides(fill = guide_legend(reverse = TRUE)) +
    # Making the R plot look more like excel for comparison... 
    scale_y_continuous(limits = c(-6,8), breaks = seq(-6,8, by = 2)) +
    scale_fill_manual(name = NULL, 
                      values = c(Inflation = "#FFc000",  # yellow
                                 Interest = "#9932cc",  # grey
                                 Unemployment = "#5E99CE")) + # orange
    theme(rect = element_blank(),
          panel.grid.major.y = element_line(colour = "#DADADA"),
          legend.position  = "bottom",
          axis.ticks = element_blank(),
          axis.title = element_blank(),
          legend.key.size = unit(3, "mm"))

0 Answers0