我正在使用R中的晶格包在单个图形上绘制多个时间序列。每当我尝试这样做时,我都会收到错误消息“NA强制引入NA”。我认为问题在于我在系列中缺少数据(即它们不是连续的,一些系列在以后的日期开始等)。
具体做法是:
1: In order(as.numeric(x)) : NAs introduced by coercion
2: In diff(as.numeric(x[ord])) : NAs introduced by coercion
这是我的代码:
library("lattice")
df1 <- structure(list(STRING = c("1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC"
), time = c("2006-04-01", "2006-10-01", "2006-12-01", "2007-02-01",
"2007-03-01", "2007-04-01", "2007-05-01", "2007-06-01", "2007-07-01",
"2007-08-01", "2007-09-01", "2007-10-01", "2007-11-01", "2007-12-01",
"2008-01-01", "2008-02-01", "2008-03-01", "2008-04-01", "2008-05-01",
"2008-06-01", "2008-07-01", "2008-08-01", "2008-09-01", "2008-10-01",
"2008-11-01", "2008-12-01", "2009-01-01", "2009-02-01", "2009-03-01",
"2009-04-01", "2009-05-01", "2009-06-01", "2009-07-01", "2009-08-01",
"2009-09-01", "2009-10-01", "2009-11-01", "2009-12-01", "2010-01-01",
"2010-02-01", "2010-03-01", "2010-04-01", "2010-05-01"), ee_qty = c(NA,
2302, 3434, 1800, 2766, 4408, 6314, 2860, 2476, 1572, 4950, 1812,
14100, 7591, NA, NA, 11823, NA, NA, 11829, 3836, 5378, 4804,
4356, 3655, 5955, 1345, 4717, 8854, 11494, 8260, 4330, 8210,
5875, NA, 10955, 12018, 13052, 5332, 8831, 14260, 4998, NA),
ee_amt = c(NA, 33815.88, 60107.4, 66279.2, 66814.46, 129042.18,
151649.49, 58576.2, 61984.58, 61782, 102648.16, 67844.4,
362371.82, 159274.66, NA, NA, 285847.62, NA, NA, 258045.26,
82306.74, 119702.9, 113514.6, 118765.82, 86298.66, 155659.83,
40246.14, 134675.59, 236084.63, 256467.56, 210629.14, 99311.04,
173380.64, 131071.08, NA, 299148.999999999, 268373.8, 299873.68,
78714.6, 230198.46, 310660.74, 96227.82, NA), elite_qty = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1281, 3037, 2141, 1764,
1661, 1838, 2780, 2627, 2498, 3137, 2238, 2470, 1107, 1886,
844, 798, NA, 1588, 1919, 2603, 2680, 2496, 2464, 3041, 3276,
5102, 5337, 4140, 5029, 4565, 5570, 6298, NA), elite_amt = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 34956.08, 48618.37, 58880.59,
49141.23, 49067.42, 40105.91, 89717.69, 78612.5, 40608.3599999999,
65893.38, 33706.66, 77191.76, 45096.9, 55742.83, 29993.7600000000,
27171.22, NA, 1427.23, 2466.64, 54500.19, 66476.09, 66041.74,
59235.3, 75234.11, 78109.54, 129537.51, 121485.25, 94928.86,
112024.69, 110419.7, 128789.94, 134046.79, NA), Sabry_Qty = c(596,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Sabry_amt = c(17761.06,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), depo_qty = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 902, 319, 883, 597, 854, 710, 911, 573, 447, 857, 795,
997, 822, 640, 716, 708, 449, 535, 823, 916, 787, NA, NA,
NA, NA, NA), depo_amt = c(NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 31011.68, 13556.65, 32134.72,
23106.94, 36681.95, 30220.74, 38051.93, 24941.85, 19337.46,
31360.4, 30182.18, 41363.71, 34419.34, 25034.18, 29616, 27125.93,
18230.98, 21119.9100000000, 33614.83, 37555.37, 33671.35,
NA, NA, NA, NA, NA), tyc_qty = c(NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 58, 116, 136, 167, 186, 139, 165,
169, 337, 219, 207, 210, 280, 366, 393, 216), tyc_amt = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3103.73, 7538.71,
7497.34, 14938.68, 12864.65, 10368.91, 12501.45, 12580.67,
28185.21, 17052.22, 15202.93, 16106.3, 19176.72, 24466.38,
26443.53, 15373.87), string2 = c("1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC", "1A AUTO INC", "1A AUTO INC", "1A AUTO INC",
"1A AUTO INC")), .Names = c("STRING", "time", "ee_qty", "ee_amt",
"elite_qty", "elite_amt", "Sabry_Qty", "Sabry_amt", "depo_qty",
"depo_amt", "tyc_qty", "tyc_amt", "string2"), class = "data.frame", row.names = c(NA,
43L))
graph1 <- xyplot(ee_amt + elite_amt + Sabry_amt + depo_amt + tyc_amt ~ time, data = df1, type = "1", auto.key = TRUE)
print(graph1)
答案 0 :(得分:2)
我建议这个错误的一部分是时间不在合适的time
格式中,即
is.character(df1$time)
见
df1$random<-1:length(df1[[1]])
graph1 <- xyplot(ee_amt + elite_amt + Sabry_amt + depo_amt + tyc_amt ~ random, data = df1, type = "l", auto.key = TRUE)
编辑:
或@plannapus建议
df1$time <- as.Date(df1$time)