使用xblocks在r中的时间序列中插入值

问题描述 投票:1回答:1

我无法弄清楚如何使用xblocks()来工作。首先,这是一个来自更大数据集的小例子:

data <- data.frame(
    Date = sample(c("1993-07-05", "1993-07-05", "1993-07-05", "1993-08-30", "1993-08-30", "1993-08-30", "1993-08-30", "1993-09-04", "1993-09-04")),   
    Oxygen = sample(c("0.9", "0.4", "4.2", "5.6", "7.3", NA, "9.5", NA, "0.3")))

然后我使用xts平均每个月的值:

xtsAveragedata <- xts(Averagedata[-1], Averagedata[[1]])
xtsAverageMonthlyData <- apply.monthly(xtsAveragedata, FUN = mean)

现在,我线性插入我的数据:

Interpolateddata <- na.approx(xtsAverageMonthlyData)

我想创建一个图形,其中我使用xblocks()或类似的东西来显示我的数据中使用插值的区域,就像我在网上找到的那样:enter image description here

如何为我的整个数据集的所有值/自动化执行此操作?我没有例子可以从参考指南中翻译成这样的东西。

谢谢您的帮助。非常感谢。

r time-series interpolation xts zoo
1个回答
1
投票

所以这不使用xtszoo,但也许这个演练将有所帮助。我使用的是稍大(和每日)的数据集,但它应该是可重现的:

library(tidyverse)
library(lubridate)

set.seed(4)
df <- tibble(
  Date = seq.Date(ymd("1993-07-01"), by = "1 day", length.out = 100),
  Oxygen = runif(100, 0, 10)
)

# Randomly assign 20 records to NA
df[sample(1:nrow(df), 20), "Oxygen"] <- NA

df_for_plot <- df %>%
  arrange(Date) %>%
  group_by(month(Date)) %>%
  mutate(
    is_na = is.na(Oxygen),
    month_avg = mean(Oxygen, na.rm = TRUE),
    oxygen_to_plot = if_else(is_na, month_avg, Oxygen)
  )

df_for_plot
#> # A tibble: 100 x 6
#> # Groups:   month(Date) [4]
#>    Date        Oxygen `month(Date)` is_na month_avg oxygen_to_plot
#>    <date>       <dbl>         <dbl> <lgl>     <dbl>          <dbl>
#>  1 1993-07-01  5.86               7 FALSE      5.87         5.86  
#>  2 1993-07-02  0.0895             7 FALSE      5.87         0.0895
#>  3 1993-07-03  2.94               7 FALSE      5.87         2.94  
#>  4 1993-07-04  2.77               7 FALSE      5.87         2.77  
#>  5 1993-07-05  8.14               7 FALSE      5.87         8.14  
#>  6 1993-07-06 NA                  7 TRUE       5.87         5.87  
#>  7 1993-07-07  7.24               7 FALSE      5.87         7.24  
#>  8 1993-07-08  9.06               7 FALSE      5.87         9.06  
#>  9 1993-07-09  9.49               7 FALSE      5.87         9.49  
#> 10 1993-07-10  0.731              7 FALSE      5.87         0.731 
#> # ... with 90 more rows

# Plot the regular data, but for the geom_rect use only the filtered data where the is_na column is TRUE.
# Assuming you have daily data, you just set the xmax to be that Date + 1.
ggplot(df_for_plot, aes(x = Date, y = oxygen_to_plot)) +
  geom_line() +
  geom_rect(
    data = df_for_plot %>% filter(is_na), 
    aes(xmin = Date, xmax = Date + 1, ymin = -Inf, ymax = +Inf), fill = "skyblue", alpha = 0.5
  )

© www.soinside.com 2019 - 2024. All rights reserved.