El objetivo de esta práctica es realizar una función que grafique objetos de clase ts
Esta función realiza 4 tipos de graficos unicamente para objeto de la clase ts (Time-Series)
Esta función recibe 3 parametros
Objeto 1
st <- Objeto de la clase ts
Objeto 2
frecuencia <- la frecuencia que presenta la ST, la función esta configuradas para
frecuencia = "mes"
frecuencia = "anio"
frecuencia = "trimestre"
Objeto 3
tipoGraf<- en esta variable se seleccionará el tipo de grafico que desee observar, las opciones son:
tipoGraf= "ploty"
tipoGraf= "ggplot"
tipoGraf= "plotly"
tipoGraf= "highchart"
En resumen
Unicamente hay que llamar la funcion y darle sus respectivos parametros, esta recibe el nombre de plot_seriesT
plot_seriesT (st,frecuencia, tipoGraf)
plot_seriesT<-function(st,frecuencia, tipoGraf){
if(class(st)=="ts")
{
if(frecuencia =="mes"){
inicio<-paste(start(st)[1],start(st)[2],1,sep="/") %>% ymd()
final<-paste(end(st)[1],end(st)[2],1,sep="/") %>% ymd()
fecha<-seq.Date(from = inicio, to = final, by = "month")}
else if(frecuencia=="anio"){
inicio<-paste(start(st)[1],1,1,sep="/") %>% ymd()
final<-paste(end(st)[1],1,1,sep="/") %>% ymd()
fecha<-seq.Date(from=inicio,to=final,by="year")}
else if(frecuencia=="trimestre"){
inicio<-paste(start(st)[1],start(st)[2],sep="/") %>% yq()
final<-paste(end(st)[1],end(st)[2],sep="/") %>% yq()
fecha<-seq.Date(from= inicio,to= final,by="quarter") }
df<-data.frame(Fechas_ST=fecha, Valor_ST=st %>% as.vector() %>% as.numeric() ) }
else {stop('class(st) no es de tipo ts') }
if(tipoGraf=="plot"){
plot(x=df[,1],y=df[,2], col="blue", xlab = "Tiempo", ylab = "Serie",main="Serie de tiempo",lwd=2.3,pch=16)
lines(x=df[ ,1],y=df[ ,2], col="red", lwd=2.6) }
else if(tipoGraf=="highchart"){
highchart(type = "stock") %>% hc_add_series(st, type = "line", color="purple")}
else if(tipoGraf=="ggplot"){
ggplot(df)+
geom_line(aes(x=Fechas_ST,y=Valor_ST), color="#FF9900")+
geom_point(aes(x=Fechas_ST,y=Valor_ST), color="#FF6600")+
ggtitle("Serie de tiempo")+
labs(x="Tiempo", y="Serie")+
theme_update()}
else if(tipoGraf=="plotly"){
ggplotly(ggplot(df)+geom_line(aes(x=Fechas_ST,y=Valor_ST), color="#66CC00")+
geom_point(aes(x=Fechas_ST,y=Valor_ST), color="#006633")+
ggtitle("Serie de tiempo")+
labs(x="Tiempo", y="Serie")+
theme_bw())}
else {stop('Gráfico no disponible') }
}
st<-ts(series[92,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=c(1980,1) )
plot_seriesT(st,frecuencia="mes", tipoGraf = "plot")
st<-ts(series[99,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=c(1990,1) )
plot_seriesT(st,frecuencia="mes", tipoGraf = "ggplot")
st<-ts(series[84,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=c(1960,1) )
plot_seriesT(st,frecuencia="mes", tipoGraf = "plotly")
st<-ts(series[101,-c(1:7)] %>% t() %>% na.omit(),frequency = 12, start=c(1950,1) )
plot_seriesT(st,frecuencia="mes", tipoGraf = "highchart")
st<-series[10,-c(1:7)] %>% t() %>% na.omit()
plot_seriesT(st,frecuencia="mes", tipoGraf = "highchart")
## Error in plot_seriesT(st, frecuencia = "mes", tipoGraf = "highchart"): class(st) no es de tipo ts
class(st)
## [1] "matrix"