简体   繁体   中英

Overlay two different datasets using ggplot in r

I have a manually simulated GARCH term that I am trying to plot alongside the original dataset using ggplot. The generated plot should look like this

在此处输入图片说明

Combining the datasets using geom_line and sec_axis does not work as it only returns the scaled axis of the red term.

Any idea regarding how I can achieve this desired plot? thanks

I solved this already. I scaled up the second(red line) dataset by a factor(880000 in this case) and scaled-down the second axis by the same factor using the trans function from sec.axis . The code is given below:

geom_line(aes(y= SPX.Index, col= "SPX.Index"), size= 1.1)+ 
  geom_line(aes(y= Model*880000, col="local Vol Model"), size=1.1)+ 
  scale_y_continuous(sec.axis=sec_axis(name= "Local Vol Model", trans= ~./880000))+ 
  scale_color_manual(values = c("#990000", "#006633" ))+ 
  theme_minimal()+theme(legend.position='bottom')+ 
  theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM