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

后端 未结 2 1128
不思量自难忘°
不思量自难忘° 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") 
    

提交回复
热议问题