反映ggplot2中两个系列之间的细微差别

时间:2017-03-14 12:06:30

标签: r ggplot2 time-series

我想比较两个系列,即AR1和MA2。他们的价值观非常接近, AR1:

var a = 0xab7878;
var b = 0xda9933;

while(b > a)
{
  a = a + 0xFF;
  console.log(a.toString(16));
}

MA2:

 [1] 3.811470 3.841913 3.885921 3.838163 3.793046 3.781066 3.739327 3.749122
 [9] 3.683452 3.716993 3.748613 3.710490 3.680737 3.693812 3.655051 3.652718
[17] 3.677618 3.672693 3.626546 3.625033 3.616690 3.580183 3.575149 3.595116
[25] 3.614835 3.638431 3.689595 3.729338 3.717809 3.690031 

我的代码是:

 [1] 3.811370 3.842327 3.886117 3.838458 3.792824 3.780851 3.739286 3.748885
 [9] 3.683550 3.716546 3.748810 3.710678 3.680557 3.693668 3.655137 3.652521
[17] 3.677604 3.672808 3.626537 3.624852 3.616692 3.580163 3.575021 3.595102
[25] 3.614860 3.638472 3.689649 3.729415 3.717882 3.690009

我的图表:

enter image description here

是否有可能以更重要的方式反映系列之间的这些微小差异?

1 个答案:

答案 0 :(得分:2)

这是我们可以做到的一种方式,因此我们不仅可以绘制两个系列的值,还可以绘制系列之间的差异(使用@discipulus建议)。我们使用 grid.arrange 包中的gridExtra来制作两个面板图。

library(ggplot2)
library(gridExtra)

p1 <- ggplot(dat, aes(x = Date))+
  geom_line(aes(y = AR1, colour = 'AR1'))+
  geom_line(aes(y = MA2, colour = 'MA2'))+
  scale_colour_manual(name = 'Series',
                      values = c('AR1' = 'blue','MA2' = 'red'))+
  ylab('Values')+
  ggtitle('Plots of AR1 and MA2')

p2 <- ggplot(dat, aes(x = Date, y = AR1 - MA2))+
  geom_line()+
  ggtitle('Difference Plot')


grid.arrange(p1, p2, nrow = 1)

enter image description here

数据

AR1 <- c(3.81147, 3.841913, 3.885921, 3.838163, 3.793046, 3.781066, 
         3.739327, 3.749122, 3.683452, 3.716993, 3.748613, 3.71049, 3.680737, 
         3.693812, 3.655051, 3.652718, 3.677618, 3.672693, 3.626546, 3.625033, 
         3.61669, 3.580183, 3.575149, 3.595116, 3.614835, 3.638431, 3.689595, 
         3.729338, 3.717809, 3.690031)

MA2 <- c(3.81137, 3.842327, 3.886117, 3.838458, 3.792824, 3.780851, 
         3.739286, 3.748885, 3.68355, 3.716546, 3.74881, 3.710678, 3.680557, 
         3.693668, 3.655137, 3.652521, 3.677604, 3.672808, 3.626537, 3.624852, 
         3.616692, 3.580163, 3.575021, 3.595102, 3.61486, 3.638472, 3.689649, 
         3.729415, 3.717882, 3.690009)

dat <- data.frame(
  Date = seq_along(AR1),
  AR1, MA2
)