简体   繁体   English

如何使用R创建时间序列螺旋图

[英]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 ); 有一个线程显示实现此目标所需的代码( 如何使用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!! 谢谢!!

As @42 said, it sounds like you have some other pre-processing to do to get your data ready for what you want. 正如@ 42所说,听起来您还需要进行一些其他预处理才能为所需的数据做好准备。

In ggplot, here's the approach I would take. 在ggplot中,这是我要采用的方法。 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. 最后,使用coord_polar将其环绕一个年轮。

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)

在此处输入图片说明

# 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()

在此处输入图片说明

# 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()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM