遮蔽ZERO线以外的正面和正面区域:R中的异常

时间:2018-01-09 22:00:06

标签: r time-series area anomaly-detection

鉴于标准化的时间序列,我想分别用蓝色和红色遮蔽正面和负面区域。从以下post获得灵感,我尝试了以下内容:

library (ggplot2)
ggplot(b, aes(dates, dat)) + geom_area(data=subset(b, dat<=0),fill="red",position = 'identity') + geom_area(data=subset(b, dat>=0), fill="blue",position = 'identity')+ 
 geom_smooth(method = "lm",se=F,size=1.2)+geom_line(aes(colour = "red"),size=0.5)+
 scale_x_date(breaks = seq(as.Date("1951-01-01"), as.Date("2013-12-31"), by="10 years"), 
              labels=date_format("%Y"))+theme(axis.text.x  = element_text(angle=0, vjust=0.5, size=10))+
 scale_y_continuous(limits=c(-3,3),breaks = seq(-3,3, by = 1))+ylab(NULL) + xlab("Time (year)")

enter image description here

阴影区域未正确跟随geom_line。即便如此,该地区看起来也很尴尬。

如何以整洁的方式填充geom_lines下的区域?

示例数据:

    b= structure(list(dates = structure(c(-6940, -6909, -6881, -6850, 
-6820, -6789, -6759, -6728, -6697, -6667, -6636, -6606, -6575, 
-6544, -6515, -6484, -6454, -6423, -6393, -6362, -6331, -6301, 
-6270, -6240, -6209, -6178, -6150, -6119, -6089, -6058, -6028, 
-5997, -5966, -5936, -5905, -5875, -5844, -5813, -5785, -5754, 
-5724, -5693, -5663, -5632, -5601, -5571, -5540, -5510, -5479, 
-5448, -5420, -5389, -5359, -5328, -5298, -5267, -5236, -5206, 
-5175, -5145, -5114, -5083, -5054, -5023, -4993, -4962, -4932, 
-4901, -4870, -4840, -4809, -4779, -4748, -4717, -4689, -4658, 
-4628, -4597, -4567, -4536, -4505, -4475, -4444, -4414, -4383, 
-4352, -4324, -4293, -4263, -4232, -4202, -4171, -4140, -4110, 
-4079, -4049, -4018, -3987, -3959, -3928), class = "Date"), dat = c(-0.910625605549408, 
-0.965393470171649, -1.09716673692565, -1.11364372522414, -0.966928227483839, 
-0.910113178396864, -0.630295785110983, -0.195304139966426, -0.135086300125106, 
-0.125590841642733, -0.137958304991406, 0.0292016064073559, 0.076904806101527, 
0.0978149453048161, 0.155702136185981, 0.487748711269244, 0.819056198490012, 
0.885907744155614, 0.639001596177642, 0.122744861489825, -0.0535439932455298, 
0.00848176075044228, -0.0256120780520969, -0.268691565651955, 
-0.261482420394633, -0.261118385414154, -0.250228992827904, -0.219694376386043, 
-0.660730544599343, -0.879357276742173, -0.836054538921174, -0.0247531668208166, 
0.341918262849067, 0.326418683049986, 0.41600402395692, 0.818673624667216, 
0.956600038145188, 0.868547394585506, 0.962924798622049, 0.507840261383761, 
0.555739351610198, 0.333775341549737, 0.818872529340592, 1.3044454120894, 
1.36146482036678, 1.67015073361349, 1.74841533722962, 1.65069696771922, 
1.57425380503261, 1.57750737078759, 1.60470949976585, 1.76584338785841, 
1.78747286516521, 2.04291142516622, 1.65691986614514, 0.83435364531951, 
0.366895222917817, 0.106061443191605, -0.0136257949563308, -0.039172450722867, 
-0.106395821358355, -0.109865867287957, -0.140110797453417, -0.233859633193537, 
-0.524821103280969, -0.475822623121396, -0.091938340606421, -0.312738655577179, 
-0.218992519559913, -0.41729583138858, -0.341109703626088, -0.178764615943057, 
-0.10437976030027, -0.127138438602851, -0.329999135840519, -0.441987514925649, 
-0.347126807394235, -0.685007291681231, -1.31251477363399, -1.16026862098407, 
-0.857848717241228, -0.719142935845841, -0.843613199408936, -0.88500351882596, 
-0.916860816746853, -0.831325622290225, -0.851113829318015, -0.982876516574583, 
-0.821655087299125, -0.725824711405517, -0.605505392406424, -0.419847379860051, 
-0.614459124334038, -0.323828057338646, -0.257470581680261, -0.243811093634083, 
-0.210160321834246, -0.17882228119167, 0.100567046322816, 0.333897813715549
)), .Names = c("dates", "dat"), row.names = c(NA, 100L), class = "data.frame")

这是在MATLAB中绘制的相同数字,可以找到here。 ggplot2以外的解决方案也很受欢迎。

MATLABanomaly function

中绘制的相同数字

enter image description here

1 个答案:

答案 0 :(得分:0)

经过几次挣扎,我认为这应该适合我的情况,尽管不是最好的。我使用geom_area

而不是geom_linearrange
ggplot(b, aes(dates, dat, ymin = 0,ymax = dat)) + geom_linerange(data = b, aes(colour = ifelse(dat <0, "blue", "red")),stat = "identity",
                                                                 position = "identity",size=0.5)

结果如下。它看起来比我之前的情节更好,并且类似于在MATLAB中绘制的情节。您可以根据需要使用线条大小来获得更多密度。

enter image description here