Práctica 2: Series de tiempo

Objetivo: Función que grafique objetos de clase ts

ts se refiere a time series.

El primer parámetro, s_tiempo es justo la serie que se va a graficar, frecuencia es si el tiempo es en meses, cada cuatro meses o de manera anual, el último parámetro engine es el parámetro que le da la opción de elegir al usuario el gráfico que desea.

Grafica_ts<-function(s_tiempo, frecuencia, engine){
  
  if(class(s_tiempo)=="ts"){
    
    if(frecuencia=="month"){
      
      a<-paste(start(s_tiempo)[1],start(s_tiempo)[2],1,sep="/") %>% ymd()
      b<-paste(end(s_tiempo)[1],end(s_tiempo)[2],1,sep="/") %>% ymd()
      fechas<-seq.Date(from=a,to=b,by=frecuencia)
    }
    else if(freq=="quarter"){
      a<-paste(start(s_tiempo)[1],start(s_tiempo)[2],sep="/") %>% yq()
      b<-paste(end(s_tiempo)[1],end(s_tiempo)[2],sep="/") %>% yq()
      fechas<-seq.Date(from = a,to=b,by=frecuencia)
    }
    else if(freq=="year"){
      a<-paste(start(s_tiempo)[1],1,1,sep="/") %>% ymd()
      b<-paste(end(s_tiempo)[1],1,1,sep="/") %>% ymd()
      fechas<-seq.Date(from=a,to=b,by=frecuencia)
    }
    tabla<-data.frame(Fechas=fechas,inicio=as.numeric(as.vector(s_tiempo)))
    
  } else{
       ggplot()+
      annotate(geom = "text", x = 1, y = 1, label = "Error!\n Functionality not available, s_tiempo isn't ts. \n Please contact developer.", size = 12,
               colour = "#FF0000")+
      theme(axis.title = element_blank(),
            axis.text = element_blank(), axis.ticks = element_blank(),
            panel.background = element_blank())
  }
  if(engine=="grafica plot"){
    graf_1<-plot(x=tabla[,1],y=tabla[,2], main="Serie", xlab = "Tiempo", ylab = "Serie")
    points(x=c(1:length(s_tiempo)),s_tiempo, col="red")
    lines(x=tabla[,1],y=tabla[,2], col="darkorchid1")
    return(graf_1)
  }
  else if(engine=="grafica plotly"){
    graf_2<-ggplot(tabla)+geom_line(aes(x=Fechas,y=inicio), color="darkorchid4")+
      geom_point(aes(x=Fechas,y=inicio), color="darkred", size=1.5)+ggtitle("Serie")+
      labs(x="Tiempo", y="Serie")+
      theme_bw() + 
  theme(
    panel.background = element_rect(fill = "khaki"),
    panel.grid.minor = element_line(linetype = "solid"))
    return(ggplotly(graf_2))
    
  }
  else if(engine=="grafica ggplot"){
    graf_3<-ggplot(tabla)+geom_line(aes(x=Fechas,y=inicio), color="purple4")+
      geom_point(aes(x=Fechas,y=inicio), color="gold", size=1.4)+ggtitle("Serie")+
      labs(x="Tiempo", y="Serie")+theme_bw()+ 
  theme(
    panel.background = element_rect(fill = "lightblue"),
    panel.grid.minor = element_line(linetype = "solid")
  )
    return(graf_3)
  }
  else{
      ggplot()+
      annotate(geom = "text", x = 1, y = 1, label = "Error!\n Functionality not available. \n Please contact developer.", size = 12,
               colour = "#FF0000")+
      theme(axis.title = element_blank(),
            axis.text = element_blank(), axis.ticks = element_blank(),
            panel.background = element_blank())
  }
}

## NULL