How to match legend colors and plot colors in overlapping area plots in ggplot2

后端 未结 2 1124
不思量自难忘°
不思量自难忘° 2021-01-27 08:26

I have the following data:

head(MP_rates_dateformat) 
       Month repo revrepo bankrate CRR Callrate  WPI GDP  FED
1 2001-04-01 9.00    6.75        7 8.0     7.         


        
相关标签:
2条回答
  • 2021-01-27 09:12

    Here is a solution based on the idea of melting/gathering the data, using fill in ggplot combined with position = "identity". Note that the order of the columns is important as the smallest value revrepo should be plot after the first one repo.

    library("tidyr")
    df_gather <- gather(select(MP_rates_dateformat, 1:3), variable, value, -Month)
    
    library("ggplot2") 
    ggplot(data = df_gather, aes(x = Month)) +
      geom_area(aes(y = value, fill = variable), position = "identity") 
    
    0 讨论(0)
  • 2021-01-27 09:18

    Finally Got it!

    library("tidyr")
    
    long_DF<- MP_rates_dateformat[,1:3] %>% gather(variable, value, -Month)
    head(long_DF)
    
        Month variable value
    1 2001-04-01     repo  9.00
    2 2001-05-01     repo  8.75
    3 2001-06-01     repo  8.50
    4 2001-07-01     repo  8.50
    5 2001-08-01     repo  8.50
    6 2001-09-01     repo  8.50
    
    library("ggplot2") 
    
    ggplot(data = long_DF, aes(x = Month)) +
    geom_area(aes(y = value, fill = variable), position = "identity") +   labs(fill="") + xlab('\nYears') + ylab('LAF Rates (%)\n') + labs(title="Overlapping - Repo & Reverse Repo\n")
    

    0 讨论(0)
提交回复
热议问题