How to create charts using time series showing every month on the x-axis in R?

Asked

Viewed 397 times

-1

Suppose I have the following time series:

PREVISAO_VL_TR = ts(PREVISAO_VL_TR, start = c(2019,1), end = c(2019,12), frequency = 12 )

And that now I would like to create a chart of this time series:

plot(PREVISAO_VL_TR, col="red", lty = 2, type = "b",ylim = c(0,35000000),
     ylab = "Valor Transação", xlab = "Meses",main="Valor Transação")

inserir a descrição da imagem aqui

The problem is that my x-axis graph doesn’t exactly match the months of my time series. I would like you to have the year/month at each point of the chart.

I do not know if it is possible to make this change, I saw some articles that made the modification, but I was not successful. I also tried to use the ggplot2, but I couldn’t apply with the time series.

What should I do to create charts using time series showing the months on the x-axis in R?

Follow the data to help answer:

dput(PREVISAO_VL_TR)
structure(c(26784859.5861874, 27528503.7932623, 29404228.6092765, 
28924310.4063269, 28776524.1089006, 28258468.9552768, 24918740.8746702, 
27708496.9694671, 26311039.5724641, 25659967.9734825, 23610019.2328564, 
21111679.3716086, 23121492.473788), .Tsp = c(2019.08333333333, 
2020.08333333333, 12), class = "ts")

1 answer

1


The package ggfortify adds to ggplot the ability to recognize class objects ts, among others. Scale detection by ggplot is usually good (in this case it will display months and years automatically), but you can use scale_date to customize.

library(ggplot2)
library(ggfortify)

autoplot(PREVISAO_VL_TR) +
  scale_x_date(date_labels = "%B/%Y", date_breaks = "2 months")

inserir a descrição da imagem aqui

See the package vignette on time series to see more options.

  • Thank you very much for the reply and for the material Carlos.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.