1

I have a time series like this one from 1979 to 2018

        Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec
1979 15.414 16.175 16.342 15.447 13.857 12.530 10.311  8.041  7.051  8.748 10.943 13.336
1980 14.862 15.955 16.041 15.429 13.793 12.205 10.100  7.984  7.667  9.183 11.383 13.593
1981 14.910 15.604 15.632 15.010 13.802 12.430 10.271  7.844  7.138  8.856 10.929 13.341
1982 15.177 15.974 16.044 15.466 13.973 12.476 10.367  8.139  7.302  9.421 11.627 13.642
1983 14.942 16.006 16.085 15.172 13.491 12.296 10.570  8.186  7.395  9.334 11.461 13.299
1984 14.473 15.299 15.584 15.015 13.577 12.152  9.977  7.771  6.805  8.561 10.842 12.989

I can convert the serie to a single one column by using t(df) and tidyr::gather(df) getting a result like:

   key  value
1 1979 15.414
2 1979 16.175
3 1979 16.342
4 1979 15.447
5 1979 13.857
6 1979 12.530

My problem arises when trying to use ggplot2: I would like to get a very nice graph from my series but this is not possible because I don't know how to configure x-axis to have a plain index. My series is a dense sinusoidal type.

ggplot(df, aes(key, value)) + geom_line(aes(group=1), colour="#000099") 

This cannot represent correctly the series. Coud anyone help me to get a good df to represent my data?

On the other hand I'm triying to represent it by quarterly. I found this from zoo::as.yearqtr but is not working properly. E.g: ts(df,start=c(as.yearqrt("1979-1",1)),frequency=4)

I found also this time series plot with x axis in "year"-"month" in R but I rather prefer to use ggplot2, even in the same way if it was possible.

Thank you in advance. All helpful comments will be rewarded!

RLave
  • 8,144
  • 3
  • 21
  • 37
fina
  • 429
  • 4
  • 12
  • 2
    You'll need to create a correct `Date` column. You'll be more likely to receive a useful answer if your example is actually reproducible. – Axeman Apr 23 '19 at 23:19

1 Answers1

3

Your date is currently stored in two places using the row and the column. We can gather the column so that Year and Month are in separate columns available for each data point. To create date data from month, and year, I like lubridate. (In this case I assign to the near middle of each month with the 15.)

df %>%
  gather(Month, val, -Year) %>%
  mutate(date = lubridate::ymd(paste(Year, Month, 15))) %>%
  ggplot(aes(date, val)) + 
  geom_line()

enter image description here

df <- read.table(
  header = T, 
  stringsAsFactors = F,
  text = "Year        Jan    Feb    Mar    Apr    May    Jun    Jul    Aug    Sep    Oct    Nov    Dec
1979 15.414 16.175 16.342 15.447 13.857 12.530 10.311  8.041  7.051  8.748 10.943 13.336
1980 14.862 15.955 16.041 15.429 13.793 12.205 10.100  7.984  7.667  9.183 11.383 13.593
1981 14.910 15.604 15.632 15.010 13.802 12.430 10.271  7.844  7.138  8.856 10.929 13.341
1982 15.177 15.974 16.044 15.466 13.973 12.476 10.367  8.139  7.302  9.421 11.627 13.642
1983 14.942 16.006 16.085 15.172 13.491 12.296 10.570  8.186  7.395  9.334 11.461 13.299
1984 14.473 15.299 15.584 15.015 13.577 12.152  9.977  7.771  6.805  8.561 10.842 12.989")
Jon Spring
  • 55,165
  • 4
  • 35
  • 53