R 使用时间序列数据在ggplot中创建垂直线

R 使用时间序列数据在ggplot中创建垂直线,r,time-series,data-visualization,ggplot2,R,Time Series,Data Visualization,Ggplot2,我有标记为tsdb的tsibble格式的数据: A tsibble: 15,000 x 6 [1M] # Key: Industry [60] # Groups: Industry [60] Industry Period Sales inventory Purchase Sales.diff <chr> <mth> <dbl> <dbl>

我有标记为
tsdb
tsibble
格式的数据:

A tsibble: 15,000 x 6 [1M]
# Key:       Industry [60]
# Groups:    Industry [60]
   Industry                Period Sales inventory Purchase Sales.diff
   <chr>                    <mth> <dbl>     <dbl>    <dbl>      <dbl>
1  Apparel manufacturing 2000 Feb  5321     12215     5228         NA
这很好,我得到了这个情节

我想在所有三个图表的选择点上添加垂直线,比如“2005年5月”和“2008年6月”。为此,我尝试添加:

 geom_vline(xintercept = yearmonth("2005 May"), linetype = 4) +
 
我得到了这个错误

 Error in UseMethod("rescale") : 
   no applicable method for 'rescale' applied to an object of class "c('yearmonth', 'vctrs_vctr')"
我甚至试过:

 geom_vline(xintercept = yearmonth(424), linetype = 4) +

 geom_vline(xintercept = scale_x_yearmonth(424), linetype = 4) +

 geom_vline(xintercept = scale_x_yearmonth("2005 May"), linetype = 4) +
我在这里得到的错误是:

 Error in xj[i] : object of type 'environment' is not subsettable

我想需要对
yearmonth
进行缩放,才能理解
ggplot
,但我不确定如何做到这一点。我试图得到一条垂直线,然后希望我能使用
c()
得到我需要的所有垂直线。我的目标是通过指定时间段来获取垂直线,例如“2005年5月”。

获取所需内容的一种方法是从周期变量创建日期变量。周期仅列出年和月,因此您需要在年和月中添加一个人工月日(例如1)以获取日期。 大概是这样的:

tsdb$Date <- paste(tsdb$Period, "1") 

谢谢你,这就是我所做的,而且成功了!GeoMyVPLY(xTncPtP= YMD(“2005”可以,截断=1),LyeType=4)+对于不同的方面,你需要不同的垂直线吗?请考虑把它作为评论和/或接受@ IsabelaGHeM'的回答。请不要添加“谢谢”作为答案。相反,你认为这是最有帮助的我用伊莎贝拉·盖曼特的答案作为提示,写下并创造出对我来说可能是最好的答案,但@KarolisKoncevicius将我接受的答案编辑成了一条评论。不知道为什么会这样做?想知道为什么托马斯·比拉赫要我把我的回答作为评论发表?对不起,我不知道这些行为的规则和逻辑,我很想知道。
tsdb$Date <- paste(tsdb$Period, "1") 
tsdb$Date <- lubridate::ymd(tsdb$Date) 
geom_vline(xintercept = lubridate::ymd("2005 May 1"), linetype = 4)