如何在时间序列图中添加垂直线和文本?

huangapple go评论60阅读模式
英文:

How to add vertical lines and text to time series plot?

问题

我有一些时间序列数据,使用R中的autoplot函数来绘制我的时间序列图表。我想要在图表上添加垂直线和文本。例如,在2003-2010之间添加一条线,上面写着"train data",在2010-2015之间写着"test data",在2015-2018之间写着"predictions"。我在普通的R图中已经实现了这个,但我想使用ggplotautoplot来实现。我的代码如下:

data <- runif(180, 100, 1000)
ts.data <- ts(data, frequency = 12, start = c(2003, 1))
autoplot(ts.data, xlab="Year", ylab="Number of Tourists")

提前感谢。

英文:

I have some time series data and used autoplot function in R to plot my time series. I would like add vertical lines to the plot and text. For example, a line between 2003-2010 with text "train data", 2010-2015 "test" data and 2015- 2018 "predictions". I did it in ordinary R plot, but it is not fancy. I would like to do it with ggplot or autoplot. My code is as follows.

data &lt;- runif(180,100,1000)
ts.data &lt;- ts(data, frequency = 12, start = c(2003,1))
autoplot(ts.data, xlab=&quot;Year&quot;, ylab=&quot;Number of Tourists&quot;)

Thanks in advance

答案1

得分: 2

你可以使用geomtextpath包与geom_textvline来向autoplot添加带有标签的垂直线,因为它是一个ggplot对象。你必须确保你的xintercept是正确的数字,因为你的数据是日期。以下是一些可重现的代码:

library(ggplot2)
library(ggfortify)
library(geomtextpath)
autoplot(ts.data, xlab="Year", ylab="Number of Tourists") +
  geom_textvline(xintercept = as.numeric(as.Date(c("2005-01-01"))),
                 label = "train data", vjust = 1.3, col = "blue") +
  geom_textvline(xintercept = as.numeric(as.Date(c("2012-01-01"))),
                 label = "test data", vjust = 1.3, col = "blue") +
  geom_textvline(xintercept = as.numeric(as.Date(c("2017-01-01"))),
                 label = "Predictions", vjust = 1.3, col = "blue")

如何在时间序列图中添加垂直线和文本?

创建于2023-02-18,使用reprex v2.0.2

英文:

You could use the geomtextpath package with geom_textvline to add vertical lines with labels to autoplot since it is a ggplot object. You have to make sure your xintercept is the right number because your data is in dates. Here is some reproducible code:

library(ggplot2)
library(ggfortify)
library(geomtextpath)
autoplot(ts.data, xlab=&quot;Year&quot;, ylab=&quot;Number of Tourists&quot;) +
  geom_textvline(xintercept = as.numeric(as.Date(c(&quot;2005-01-01&quot;))),
                 label = &quot;train data&quot;, vjust = 1.3, col = &quot;blue&quot;) +
  geom_textvline(xintercept = as.numeric(as.Date(c(&quot;2012-01-01&quot;))),
                 label = &quot;test data&quot;, vjust = 1.3, col = &quot;blue&quot;) +
  geom_textvline(xintercept = as.numeric(as.Date(c(&quot;2017-01-01&quot;))),
                 label = &quot;Predictions&quot;, vjust = 1.3, col = &quot;blue&quot;)

如何在时间序列图中添加垂直线和文本?<!-- -->

<sup>Created on 2023-02-18 with reprex v2.0.2</sup>

答案2

得分: 2

你也可以使用ggplot2本身来完成:

library(ggplot2)
data <- runif(180,100,1000)
ts.data <- ts(data, frequency = 12, start = c(2003,1))
autoplot(ts.data, xlab="年份", ylab="游客人数") +
  geom_vline(xintercept = as.Date(c("2003-01-01", "2010-01-01", "2015-01-01", "2018-01-01")), color = "blue")+
  geom_text(aes(x = as.Date("2007-01-01"), y = 1020, label="训练数据"))+
  geom_text(aes(x = as.Date("2013-01-01"), y = 1020, label="测试"))+
  geom_text(aes(x = as.Date("2016-07-01"), y = 1020, label="预测数据"))

如何在时间序列图中添加垂直线和文本?

我理解你问的是如何添加垂直线,只是想知道是否使用水平线更好:

autoplot(ts.data, xlab="年份", ylab="游客人数") +
  geom_segment(aes(x = as.Date("2003-01-01"), y = 1000, xend = as.Date("2010-01-01"), yend = 1000), color = "red") +
  geom_segment(aes(x = as.Date("2010-01-01"), y = 1000, xend = as.Date("2015-01-01"), yend = 1000), color = "blue") +
  geom_segment(aes(x = as.Date("2015-01-01"), y = 1000, xend = as.Date("2018-01-01"), yend = 1000), color = "green") +
  geom_text(aes(x = as.Date("2007-01-01"), y = 1020, label="训练数据"), color = "red")+
  geom_text(aes(x = as.Date("2013-01-01"), y = 1020, label="测试"), color = "blue")+
  geom_text(aes(x = as.Date("2017-01-01"), y = 1020, label="预测数据"), color="green")

如何在时间序列图中添加垂直线和文本?

英文:

You can also use ggplot2 itself to get it done:

library(ggplot2)
data &lt;- runif(180,100,1000)
ts.data &lt;- ts(data, frequency = 12, start = c(2003,1))
autoplot(ts.data, xlab=&quot;Year&quot;, ylab=&quot;Number of Tourists&quot;) +
  geom_vline(xintercept = as.Date(c(&quot;2003-01-01&quot;, &quot;2010-01-01&quot;, &quot;2015-01-01&quot;, &quot;2018-01-01&quot;) ), color = &quot;blue&quot;)+
  geom_text(aes(x = as.Date(&quot;2007-01-01&quot;), y = 1020, label=&quot;train data&quot;))+
  geom_text(aes(x = as.Date(&quot;2013-01-01&quot;),  y = 1020, label=&quot;test&quot;))+
  geom_text(aes(x = as.Date(&quot;2016-07-01&quot;),  y = 1020, label=&quot;predictions&quot;))

如何在时间序列图中添加垂直线和文本?

I understand you asked about adding vertical lines, just wonder if it is better to use horizontal lines:

autoplot(ts.data, xlab=&quot;Year&quot;, ylab=&quot;Number of Tourists&quot;) +
  geom_segment(aes(x = as.Date(&quot;2003-01-01&quot;) , y = 1000, xend = as.Date(&quot;2010-01-01&quot;), yend = 1000), color = &quot;red&quot;) +
  geom_segment(aes(x = as.Date(&quot;2010-01-01&quot;) , y = 1000, xend = as.Date(&quot;2015-01-01&quot;), yend = 1000), color = &quot;blue&quot;) +
  geom_segment(aes(x = as.Date(&quot;2015-01-01&quot;) , y = 1000, xend = as.Date(&quot;2018-01-01&quot;), yend = 1000), color = &quot;green&quot;) +
  geom_text(aes(x = as.Date(&quot;2007-01-01&quot;), y = 1020, label=&quot;train data&quot;), color = &quot;red&quot;)+
  geom_text(aes(x = as.Date(&quot;2013-01-01&quot;),  y = 1020, label=&quot;test&quot;), color = &quot;blue&quot;)+
  geom_text(aes(x = as.Date(&quot;2017-01-01&quot;),  y = 1020, label=&quot;predictions&quot;), color=&quot;green&quot;)

如何在时间序列图中添加垂直线和文本?

huangapple
  • 本文由 发表于 2023年2月18日 17:27:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定