英文:
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图中已经实现了这个,但我想使用ggplot
或autoplot
来实现。我的代码如下:
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 <- runif(180,100,1000)
ts.data <- ts(data, frequency = 12, start = c(2003,1))
autoplot(ts.data, xlab="Year", ylab="Number of Tourists")
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="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")
<!-- -->
<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 <- runif(180,100,1000)
ts.data <- ts(data, frequency = 12, start = c(2003,1))
autoplot(ts.data, xlab="Year", ylab="Number of Tourists") +
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="train data"))+
geom_text(aes(x = as.Date("2013-01-01"), y = 1020, label="test"))+
geom_text(aes(x = as.Date("2016-07-01"), y = 1020, label="predictions"))
I understand you asked about adding vertical lines, just wonder if it is better to use horizontal lines:
autoplot(ts.data, xlab="Year", ylab="Number of Tourists") +
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="train data"), color = "red")+
geom_text(aes(x = as.Date("2013-01-01"), y = 1020, label="test"), color = "blue")+
geom_text(aes(x = as.Date("2017-01-01"), y = 1020, label="predictions"), color="green")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论