Search code examples
rtimegraphseriesspiral

How to create a time series spiral graph using R


I would like to create a time series portrayed visually as a spiral graph like this one. I would like for the ticks to be in months instead of hours. Each spiral will represent years instead of days. I would like to do the option of having the main ticks to be broken into four minor ticks (represented by weeks) or no minor ticks and just have the main ticks of months only.

Time-Spiral Graph

I have included a sample of mock data. The daily temperature means could be binned into four bins (as represented by weeks).

Year    Month   Day Temperature
1993    January 1   9
1993    January 2   6
1993    January 3   6
1993    January 4   5
1993    January 5   5
1993    January 6   5
1993    January 7   8
1993    January 8   9
1993    January 9   6
1993    January 10  5
1993    January 11  7
1993    January 12  10
1993    January 13  7
1993    January 14  10
1993    January 15  5
1993    January 16  5
1993    January 17  7
1993    January 18  7
1993    January 19  10
1993    January 20  8
1993    January 21  9
1993    January 22  8
1993    January 23  9
1993    January 24  9
1993    January 25  5
1993    January 26  6
1993    January 27  7
1993    January 28  6
1993    January 29  8
1993    January 30  8
1993    January 31  10
1993    February    1   8
1993    February    2   9
1993    February    3   9
1993    February    4   6
1993    February    5   5
1993    February    6   9
1993    February    7   8
1993    February    8   10
1993    February    9   9
1993    February    10  6
1993    February    11  6
1993    February    12  9
1993    February    13  8
1993    February    14  6
1993    February    15  6
1993    February    16  9
1993    February    17  10
1993    February    18  5
1993    February    19  7
1993    February    20  6
1993    February    21  8
1993    February    22  9
1993    February    23  5
1993    February    24  10
1993    February    25  10
1993    February    26  8
1993    February    27  10
1993    February    28  9
1993    March   1   10
1993    March   2   9
1993    March   3   9
1993    March   4   6
1993    March   5   7
1993    March   6   6
1993    March   7   5
1993    March   8   10
1993    March   9   9
1993    March   10  8
1993    March   11  9
1993    March   12  7
1993    March   13  7
1993    March   14  6
1993    March   15  6
1993    March   16  9
1993    March   17  7
1993    March   18  6
1993    March   19  10
1993    March   20  7
1993    March   21  6
1993    March   22  6
1993    March   23  10
1993    March   24  9
1993    March   25  8
1993    March   26  6
1993    March   27  5
1993    March   28  5
1993    March   29  10
1993    March   30  7
1993    March   31  8
1993    April   1   6
1993    April   2   7
1993    April   3   10
1993    April   4   7
1993    April   5   8
1993    April   6   5
1993    April   7   7
1993    April   8   5
1993    April   9   10
1993    April   10  7
1993    April   11  6
1993    April   12  9
1993    April   13  10
1993    April   14  10
1993    April   15  6
1993    April   16  5

There is a thread that shows the code needed to achieve this (How to Create A Time-Spiral Graph Using R); however, I am having a difficulty understanding the code and modifying it to fit my purpose. I am hoping someone can either point me in the right direction or help me customize the code.

Thank you!!


Solution

  • As @42 said, it sounds like you have some other pre-processing to do to get your data ready for what you want.

    In ggplot, here's the approach I would take. First get your data printing as a bar chart. Then add an ascending baseline. Finally, use coord_polar to put it around an annual circle.

    sample <- data.frame(date = seq.Date(from = as.Date("1993-01-01"), to = as.Date("1996-12-31"), by = 1),
                         day_num = 1:1461,
                         temp = rnorm(1461, 10, 2))
    
    # as normal bar
    ggplot(sample, aes(date, temp, fill = temp)) + 
      geom_col() +
      scale_fill_viridis_c() + theme_minimal()
      # or use the fill pattern below to replicate OP picture:
      # scale_fill_gradient2(low="green", mid="yellow", high="red", midpoint=10)
    

    enter image description here

    # as ascending bar
    ggplot(sample, aes(date, 0.01*day_num + temp/2, 
                       height = temp, fill = temp)) + 
      geom_tile() +
      scale_fill_viridis_c() + theme_minimal()
    

    enter image description here

    # as spiral
    ggplot(sample, aes(day_num %% 365, 
                   0.05*day_num + temp/2, height = temp, fill = temp)) + 
      geom_tile() + 
      scale_y_continuous(limits = c(-20, NA)) +
      scale_x_continuous(breaks = 30*0:11, minor_breaks = NULL, labels = month.abb) +
      coord_polar() + 
      scale_fill_viridis_c() + theme_minimal()
    

    enter image description here