En la siguiente entrada se utiliza la base de datos tipo wide de nombre EuStockMarkets, disponible en R, la misma cuenta con los índices asociados a los siguientes activos financieros (solo se presenta como obtener los datos sin profundizar sobre la forma de interpretación de los mismos):
library(tidyverse)
head(EuStockMarkets)
head(EuStockMarkets)
Time Series:
Start = c(1991, 130)
 End = c(1991, 135)
 Frequency = 260
             
DAX    SMI    CAC  
FTSE
1991.496
1628.75 1678.1 1772.8 2443.6
1991.500
1613.63 1688.5 1750.5 2460.2
1991.504
1606.51 1678.6 1718.0 2448.2
1991.508
1621.04 1684.1 1708.1 2470.4
1991.512
1618.16 1686.6 1723.1 2484.7
1991.515
1610.61 1671.6 1714.3 2466.8
Transformamos la data a versión wide, porque al hacer un by_group.
wide_data
<- data.frame(EuStockMarkets) %>%
  mutate(fecha = time(EuStockMarkets)) %>%
  gather(id, value, -fecha)
 
 wide_data
%>%  head()
 
 >
wide_data %>%  head()
     fecha 
id   value
1 1991.496
DAX 1628.75
2 1991.500
DAX 1613.63
3 1991.504
DAX 1606.51
4 1991.508
DAX 1621.04
5 1991.512
DAX 1618.16
6 1991.515
DAX 1610.61
Gráfico resaltando un evento determinado:
wide_data
%>%
  ggplot(aes(x = fecha, y = value)) +
  geom_line(aes(color = id), size = 1)+
  theme_minimal() +
   facet_wrap(~id, scales='free_y',ncol=1)+
  theme(legend.position = "none")+
  geom_vline(xintercept=1997,linetype=4)
Agregando promedio alrededor de un evento
wide_data
%>%
   dplyr::mutate(dummyT =
(as.numeric(fecha)>1997)*1) %>%
   group_by(id,dummyT) %>%
   mutate(medias = mean(value, na.rm = T))
%>%
   ggplot(aes(x=fecha,y=value))+
  geom_line(aes(color = id))+
  geom_line(aes(x=fecha,y=medias,color = id))+
  facet_wrap(~id, scales='free_y',ncol=1)+
   theme(legend.position = "none")+
  geom_vline(xintercept=1997,linetype=4)+
  theme_minimal()+
  theme(legend.position = "none")
Agregando promedio alrededor de varios eventos
