我有一个包含10个组的动物园对象,其中每个组都包含NA值,我试图创建一个图,其中每个组的值在子图中相对于时间绘制,忽略NA:s。我希望所有组都可以在整个时间段内进行绘制,如果它只包含部分时间段的值,则需要。我试过使用na.exclude和na.pass,但它没有用。
这是我的数据:
structure(c(-4.10947262625989, NA, -6.35834864986063, NA, -5.91266537625275,
NA, -6.52008097909069, NA, -5.2902016879408, NA, -3.11303759188572,
NA, -0.306205546663778, NA, -1.16249420065793, NA, -3.33688404352456,
NA, -8.13394337994639, NA, -4.97829512928346, NA, -4.91184324092269,
NA, -0.426243026538591, NA, -2.47957454922956, NA, -3.3329046491404,
NA, -4.36366791265756, NA, -6.1055782340265, NA, -6.2094702018437,
NA, -2.96526179126904, NA, -3.36132436304139, NA, -1.06078300406145,
NA, -0.656336560364218, NA, -2.04074053746388, NA, -4.99967426633704,
NA, -4.04800553172208, NA, -3.31189396136818, NA, -2.36440148960283,
NA, -1.18900512784083, NA, -3.05795867861728, NA, NA, NA, -4.97174143754589,
NA, -6.23509102350769, NA, -5.89843588741174, NA, -4.18645165800088,
NA, -2.0212883057441, NA, -1.04550830382834, NA, -1.27999574705108,
NA, -3.14638335358303, NA, -3.86222685146702, NA, -4.21772272823727,
NA, -2.58543072976183, NA, -2.48846140494414, NA, -2.22821228166982,
NA, NA, NA, NA, NA, -5.28108123680546, NA, -6.06192246724695,
NA, -4.56130486243214, NA, -2.74677229492713, NA, -1.7789934443997,
NA, -1.44728905496209, NA, -2.08633334103407, NA, -2.99380214399451,
NA, -4.04643083407379, NA, -3.43524805580059, NA, -2.5415459951728,
NA, -2.75734663004611, NA, NA, NA, NA, NA, NA, NA, -5.1830394775654,
NA, -4.8164568203158, NA, -3.18719721106969, NA, -2.37873600997152,
NA, -2.00135150333387, NA, -2.08291969862349, NA, -2.25691836415158,
NA, -3.23822518515935, NA, -3.52333679362043, NA, -3.32001852877501,
NA, -2.75530170365368, NA, NA, NA, NA, NA, NA, NA, NA, NA, -4.60120959896844,
NA, -3.49697326894922, NA, -2.75887253351057, NA, -2.4962239838783,
NA, -2.46788431125548, NA, -2.2640901134155, NA, -2.51329703125672,
NA, -2.9862663107103, NA, -3.44439144612439, NA, -3.3266447343254,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -3.60784867185276,
NA, -3.07207080168394, NA, -2.81269574155185, NA, -2.86157315340287,
NA, -2.52790961472606, NA, -2.49684311839687, NA, -2.42078499136711,
NA, -2.98180846872684, NA, -3.40641608489269, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, -3.23223720384766, NA, -3.10313101000138,
NA, -3.11608282590682, NA, -2.85334053214087, NA, -2.70174906729397,
NA, -2.44411673514141, NA, -2.46253325647867, NA, -2.99992356599436,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, -3.24370475890788,
NA, -3.36581515217865, NA, -3.08076778703707, NA, -2.98623115636078,
NA, -2.61154777515114, NA, -2.49224003994964, NA, -2.52774960963835,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, -3.45699179158273, NA, -3.28582189621058, NA, -3.19061533966329,
NA, -2.87595330453915, NA, -2.63679933171846, NA, -2.55051771717749,
NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = c(29L, 10L), .Dimnames = list(
NULL, c("structure(c(-4.10947262625989, -7.35834864986063, -5.91266537625275, ",
"structure(c(-4.36366791265756, -6.1055782340265, -6.2094702018437, ",
"structure(c(-4.97174143754589, -6.23509102350769, -5.89843588741174, ",
"structure(c(-5.28108123680546, -6.06192246724695, -4.56130486243214, ",
"structure(c(-5.1830394775654, -4.8164568203158, -3.18719721106969, ",
"structure(c(-4.60120959896844, -3.49697326894922, -2.75887253351057, ",
"structure(c(-3.60784867185276, -3.07207080168394, -2.81269574155185, ",
"structure(c(-3.23223720384766, -3.10313101000138, -3.11608282590682, ",
"structure(c(-3.24370475890788, -3.36581515217865, -3.08076778703707, ",
"structure(c(-3.45699179158273, -3.28582189621058, -3.19061533966329, "
)), index = structure(c(17393.375, 17393.875, 17394.375,
17394.875, 17395.375, 17395.875, 17396.375, 17396.875, 17397.375,
17397.875, 17398.375, 17398.875, 17399.375, 17399.875, 17400.375,
17400.875, 17401.375, 17401.875, 17402.375, 17402.875, 17403.375,
17403.875, 17404.375, 17404.875, 17405.375, 17405.875, 17406.375,
17406.875, 17407.375), format = structure(c("m/d/y", "h:m:s"), .Names = c("dates",
"times")), origin = c(1, 1, 1970), class = c("chron", "dates",
"times")), class = "zoo")
有没有人对如何在r中做这个有一些想法?提前谢谢!
答案 0 :(得分:2)
在您的数据中,值之间始终存在NA,所以当您说"忽略NAs"时,我假设您的意思是连接点,即使它们已经存在介于两者之间。
首先,我们明确加载动物园库:
library(zoo)
然后,我们将数据保存到变量中:
z <- structure(c(-4.10947262625989, NA, -6.35834864986063, NA, -5.91266537625275,
NA, -6.52008097909069, NA, -5.2902016879408, NA, -3.11303759188572,
NA, -0.306205546663778, NA, -1.16249420065793, NA, -3.33688404352456,
NA, -8.13394337994639, NA, -4.97829512928346, NA, -4.91184324092269,
# I'm not pasting the whole data block, but you get the idea
然后,我们使用zoo来插入现有值之间的所有NA,但是我们不会在头/尾中插入缺失值:
filled <- na.fill(z, list(NA, "extend", NA))
zoo包具有绘制动物园对象中所有系列的功能
autoplot(filled, facets = NULL) +
theme(legend.position="none")