我正在使用ggplot2绘制时间序列.
p1<-ggplot(df1,aes(x=Date,y=Value))+
geom_line(color = "#0099f9",size=0.7)+
geom_point(color = "#0099f9",size=2.5)+
xlab('YEAR') +
ylab('Value')+
scale_x_date(expand = c(0.01, 0))+
stat_smooth(color = "#FC4E07" ,se=F,method = "loess", linetype = "dashed")+
theme(plot.title = element_text(size = 14, hjust = 0.5,face="bold"),
axis.text = element_text(size = 12,face="bold"),
axis.title = element_text(size = 12,face="bold"),
axis.ticks = element_line(size = 1))
这将正常运行并生成一个图形.但是,当我使用以下几行将方程式添加到曲线图上时,我得到错误:Error:Invalid input: date_trans works with objects of class Date only
fit1 <- lm(Value ~ Date, data = df1)
eqn1 <- sprintf("y = %.2 f + %.2f x", coef(fit1)[1], coef(fit1)[2])
p1<-p1+geom_text(aes(x = -Inf, y = Inf,label = eqn1),vjust = 1.5, hjust = -0.1, size = 7, color = "#FC4E07")
我搞不清问题出在哪里.以下是数据框的详细信息.
str(df1)
'data.frame': 200 obs. of 2 variables:
$ Date : Date, format: "1979-01-01" "1979-02-01" "1979-03-01" "1979-04-01" ...
$ Value: num 0.0396 0.7923 0.2091 0.5291 0.6034 ...
日期值在Date类中,因此不会出现错误.
dput(df1)
structure(list(Date = structure(c(3287, 3318, 3346, 3377, 3407,
3438, 3468, 3499, 3530, 3560, 3591, 3621, 3652, 3683, 3712, 3743,
3773, 3804, 3834, 3865, 3896, 3926, 3957, 3987, 4018, 4049, 4077,
4108, 4138, 4169, 4199, 4230, 4261, 4291, 4322, 4352, 4383, 4414,
4442, 4473, 4503, 4534, 4564, 4595, 4626, 4656, 4687, 4717, 4748,
4779, 4807, 4838, 4868, 4899, 4929, 4960, 4991, 5021, 5052, 5082,
5113, 5144, 5173, 5204, 5234, 5265, 5295, 5326, 5357, 5387, 5418,
5448, 5479, 5510, 5538, 5569, 5599, 5630, 5660, 5691, 5722, 5752,
5783, 5813, 5844, 5875, 5903, 5934, 5964, 5995, 6025, 6056, 6087,
6117, 6148, 6178, 6209, 6240, 6268, 6299, 6329, 6360, 6390, 6421,
6452, 6482, 6513, 6543, 6574, 6605, 6634, 6665, 6695, 6726, 6756,
6787, 6818, 6848, 6879, 6909, 6940, 6971, 6999, 7030, 7060, 7091,
7121, 7152, 7183, 7213, 7244, 7274, 7305, 7336, 7364, 7395, 7425,
7456, 7486, 7517, 7548, 7578, 7609, 7639, 7670, 7701, 7729, 7760,
7790, 7821, 7851, 7882, 7913, 7943, 7974, 8004, 8035, 8066, 8095,
8126, 8156, 8187, 8217, 8248, 8279, 8309, 8340, 8370, 8401, 8432,
8460, 8491, 8521, 8552, 8582, 8613, 8644, 8674, 8705, 8735, 8766,
8797, 8825, 8856, 8886, 8917, 8947, 8978, 9009, 9039, 9070, 9100,
9131, 9162, 9190, 9221, 9251, 9282, 9312, 9343), class = "Date"),
Value = c(0.0396257280372083, 0.792254076339304, 0.209117455175146,
0.529065352398902, 0.603375660488382, 0.319143296219409,
0.395686875795946, 0.907367086969316, 0.214899873128161,
0.845133831258863, 0.729593698866665, 0.23446642444469, 0.695456632180139,
0.609369153855368, 0.804903780575842, 0.717942415038124,
0.708948610117659, 0.586231412598863, 0.428744627162814,
0.289207409368828, 0.261673229746521, 0.0517897910904139,
0.399881781078875, 0.491419325815514, 0.660340357106179,
0.397569235647097, 0.719583918107674, 0.0527276170905679,
0.299225802300498, 0.600920942146331, 0.692732113646343,
0.359461600426584, 0.41012356011197, 0.654954002005979, 0.00501723401248455,
0.95377110154368, 0.745722888037562, 0.781670887488872, 0.297899815021083,
0.544447399210185, 0.270234991330653, 0.744834579760209,
0.457057158928365, 0.319076047977433, 0.467465160181746,
0.873203304363415, 0.00973224942572415, 0.525288483127952,
0.987205622019246, 0.821595306508243, 0.444444488268346,
0.423219494288787, 0.957183523569256, 0.700562145095319,
0.0627007535658777, 0.85160772013478, 0.112801872659475,
0.860109773464501, 0.839778766036034, 0.0800019965972751,
0.570089245447889, 0.138796605635434, 0.0599930980242789,
0.850726855220273, 0.187702437164262, 0.477955089416355,
0.44590290565975, 0.591855063103139, 0.220722594996914, 0.137945054331794,
0.276140772737563, 0.555247570155188, 0.583037694916129,
0.414306864375249, 0.259164260001853, 0.704047953011468,
0.820138399256393, 0.187290345318615, 0.021821113768965,
0.480921486392617, 0.335172405699268, 0.65701438812539, 0.719875351525843,
0.826985993655398, 0.188403950538486, 0.979418985778466,
0.880327768158168, 0.459435713244602, 0.899967525154352,
0.63023396464996, 0.429708898067474, 0.419689932372421, 0.679789466783404,
0.935549516929314, 0.15216495259665, 0.58040455612354, 0.588773664552718,
0.302787747932598, 0.477910435758531, 0.790613159071654,
0.298687025438994, 0.351667754119262, 0.993604320334271,
0.647046210477129, 0.284127758815885, 0.70262410887517, 0.0148184688296169,
0.0293688937090337, 0.681327062658966, 0.401758651249111,
0.417004751972854, 0.49612541985698, 0.441743229515851, 0.724668584531173,
0.82554557477124, 0.66471484862268, 0.707551871426404, 0.924636604497209,
0.891580238705501, 0.575800632592291, 0.48037875094451, 0.537902092793956,
0.639664534013718, 0.338856110116467, 0.889717331621796,
0.7493839727249, 0.313021246809512, 0.593599291983992, 0.152630586409941,
0.0162873985245824, 0.443004894768819, 0.137859313283116,
0.770382518880069, 0.825071637053043, 0.547108234604821,
0.762059377972037, 0.184374007163569, 0.0704278845805675,
0.975533499382436, 0.935448106378317, 0.0500200022943318,
0.190880338894203, 0.279884484829381, 0.380133863538504,
0.660563811194152, 0.984935140237212, 0.204079145332798,
0.692034333012998, 0.477057519135997, 0.398255903739482,
0.391803733538836, 0.825130457058549, 0.455573290586472,
0.0229971604421735, 0.0992102834861726, 0.540488281752914,
0.487516192952171, 0.574387381318957, 0.620537696639076,
0.384882270591334, 0.657185624819249, 0.747470932547003,
0.538966090651229, 0.884251141222194, 0.398574377875775,
0.75206046900712, 0.544822150375694, 0.448297741822898, 0.369472151389346,
0.332336842315271, 0.145391083555296, 0.876680750632659,
0.963439451064914, 0.157352658221498, 0.799375151982531,
0.442906379466876, 0.591339654987678, 0.00191973126493394,
0.340572453802451, 0.466652521165088, 0.383389285299927,
0.627100618788972, 0.0985251953825355, 0.452199894469231,
0.22930586966686, 0.620492672314867, 0.23723890632391, 0.849632628029212,
0.587177567649633, 0.0356330594513565, 0.530038081342354,
0.782372932881117, 0.931947859935462, 0.706536986399442,
0.778071614913642, 0.486462311586365, 0.901525277877226,
0.98282943549566, 0.913252690341324, 0.280951054533944)), row.names = c(NA,
-200L), class = "data.frame")