簡體   English   中英

R:將多個背景矩形添加到ggplot2時間序列

[英]R: Add multiple background rectangles to ggplot2 time series

我想在ggplot2時間序列數據上添加多個矩形。 我有一個xts對象lang ,其中ssc是數字,而he是一個因素。

lang
                         ssc he
2015-06-09 04:42:00  127.214  1
2015-06-09 05:12:00  128.935  1
2015-06-09 05:42:00  146.458  1
2015-06-09 06:12:00  144.035  1
2015-06-09 06:42:00  131.288  1
2015-06-09 07:12:00  117.733  1
2015-06-09 07:42:00  120.718  1
2015-06-09 08:12:00  101.896  1
2015-06-09 08:42:00   98.138  1
2015-06-09 09:12:00   94.486  1
2015-06-09 09:42:00  121.034  1
2015-06-09 10:12:00  124.721  1
2015-06-09 10:42:00  121.420  1
2015-06-09 11:12:00  126.828  1
2015-06-09 11:42:00  102.177  1
2015-06-09 12:12:00   99.648  1
2015-06-09 12:42:00  128.865  1
2015-06-09 13:12:00  122.193  1
2015-06-09 13:42:00  122.403  1
2015-06-09 14:12:00  113.835  1
2015-06-09 14:42:00  126.898  1
2015-06-09 15:12:00  108.568  1
2015-06-09 15:42:00  105.161  1
2015-06-09 16:12:00  123.492  1
2015-06-09 16:42:00  104.213  1
2015-06-09 17:12:00  102.949  1
2015-06-09 17:42:00  126.336  1
2015-06-09 18:12:00   93.363  1
2015-06-09 18:42:00  136.380  1
2015-06-09 19:12:00  115.275  1
2015-06-09 19:42:00  107.128  1
2015-06-09 20:12:00  117.803  1
2015-06-09 20:42:00  121.771  1
2015-06-09 21:12:00   98.560  1
2015-06-09 21:42:00   98.349  1
2015-06-09 22:12:00   93.363  1
2015-06-09 22:42:00  108.006  1
2015-06-09 23:12:00   94.802  1
2015-06-09 23:42:00  100.280  1
2015-06-10 00:12:00   92.028  1
2015-06-10 00:42:00   97.717  1
2015-06-10 01:12:00  125.564  1
2015-06-10 01:42:00   90.659  1
2015-06-10 02:12:00   85.321  1
2015-06-10 02:42:00   85.742  1
2015-06-10 03:12:00   92.028  1
2015-06-10 03:42:00   91.185  1
2015-06-10 04:12:00   90.659  1
2015-06-10 04:42:00  109.902  1
2015-06-10 05:12:00   87.674  1
2015-06-10 05:42:00   98.314  1
2015-06-10 06:12:00  102.809  1
2015-06-10 06:42:00   93.257  1
2015-06-10 07:12:00   99.016  1
2015-06-10 07:42:00   89.289  1
2015-06-10 08:12:00   86.199  1
2015-06-10 08:42:00  102.177  1
2015-06-10 09:12:00  129.181  1
2015-06-10 09:42:00   94.346  1
2015-06-10 10:12:00   86.023  1
2015-06-10 10:42:00   86.550  1
2015-06-10 11:12:00  101.369  1
2015-06-10 11:42:00  122.158  1
2015-06-10 12:12:00   78.719  1
2015-06-10 12:42:00  101.509  1
2015-06-10 13:12:00   79.386  1
2015-06-10 13:42:00   87.604  1
2015-06-10 14:12:00  108.111  1
2015-06-10 14:42:00   86.901  1
2015-06-10 15:12:00   97.717  1
2015-06-10 15:42:00  118.330  1
2015-06-10 16:12:00   84.232  1
2015-06-10 16:42:00   87.744  1
2015-06-10 17:12:00   82.793  1
2015-06-10 17:42:00  111.482  1
2015-06-10 18:12:00   95.540  1
2015-06-10 18:42:00   85.988  1
2015-06-10 19:12:00  116.083  1
2015-06-10 19:42:00   78.052  1
2015-06-10 20:12:00  109.551  1
2015-06-10 20:42:00   98.630  1
2015-06-10 21:12:00   85.813  1
2015-06-10 21:42:00   88.868  1
2015-06-10 22:12:00   97.787  1
2015-06-10 22:42:00   78.473  1
2015-06-10 23:12:00   82.371  1
2015-06-10 23:42:00   80.931  1
2015-06-11 00:12:00   73.979  1
2015-06-11 00:42:00  100.140  1
2015-06-11 01:12:00   81.318  1
2015-06-11 01:42:00   86.304  1
2015-06-11 02:12:00   83.425  1
2015-06-11 02:42:00  110.394  1
2015-06-11 03:12:00   91.817  1
2015-06-11 03:42:00  109.165  1
2015-06-11 04:12:00   96.418  1
2015-06-11 04:42:00   76.015  1
2015-06-11 05:12:00  103.827  1
2015-06-11 05:42:00   75.137  1
2015-06-11 06:12:00   75.102  1
2015-06-11 06:42:00   96.312  1
2015-06-11 07:12:00   85.391  1
2015-06-11 07:42:00   79.070  1
2015-06-11 08:12:00   74.927  1
2015-06-11 08:42:00   91.396  2
2015-06-11 09:12:00   71.836  2
2015-06-11 09:42:00   72.855  2
2015-06-11 10:12:00   87.744  2
2015-06-11 10:42:00   69.343  2
2015-06-11 11:12:00   85.777  2
2015-06-11 11:42:00   83.319  2
2015-06-11 12:12:00   58.141  2
2015-06-11 12:42:00   58.914  2
2015-06-11 13:12:00   78.087  2
2015-06-11 13:42:00   62.917  2
2015-06-11 14:12:00   81.599  2
2015-06-11 14:42:00   65.340  2
2015-06-11 15:12:00   71.872  2
2015-06-11 15:42:00   97.682  2
2015-06-11 16:12:00   98.770  2
2015-06-11 16:42:00   76.402  2
2015-06-11 17:12:00  107.058  2
2015-06-11 17:42:00   74.821  2
2015-06-11 18:12:00  100.386  2
2015-06-11 18:42:00   75.664  2
2015-06-11 19:12:00   71.942  2
2015-06-11 19:42:00   92.836  2
2015-06-11 20:12:00   77.490  2
2015-06-11 20:42:00   72.398  2
2015-06-11 21:12:00   74.189  2
2015-06-11 21:42:00  111.728  2
2015-06-11 22:12:00  101.755  2
2015-06-11 22:42:00   71.520  2
2015-06-11 23:12:00   93.538  2
2015-06-11 23:42:00  108.006  2
2015-06-12 00:12:00   70.081  2
2015-06-12 00:42:00   93.222  2
2015-06-12 01:12:00  108.778  2
2015-06-12 01:42:00   68.992  2
2015-06-12 02:12:00   86.866  2
2015-06-12 02:42:00   66.780  2
2015-06-12 03:12:00   69.940  2
2015-06-12 03:42:00   77.736  2
2015-06-12 04:12:00   84.443  2
2015-06-12 04:42:00   71.099  2
2015-06-12 05:12:00   87.077  2
2015-06-12 05:42:00   70.502  2
2015-06-12 06:12:00   70.923  2
2015-06-12 06:42:00   71.872  3
2015-06-12 07:12:00   89.324  3
2015-06-12 07:42:00   84.303  3
2015-06-12 08:12:00   87.990  3
2015-06-12 08:42:00   85.672  3
2015-06-12 09:12:00   68.395  3
2015-06-12 09:42:00   74.295  3
2015-06-12 10:12:00   84.689  3
2015-06-12 10:42:00   87.709  3
2015-06-12 11:12:00   69.835  3
2015-06-12 11:42:00   83.811  3
2015-06-12 12:12:00   69.238  3
2015-06-12 12:42:00   89.746  3
2015-06-12 13:12:00   81.142  3
2015-06-12 13:42:00   65.586  3
2015-06-12 14:12:00   92.379  3
2015-06-12 14:42:00   76.823  3
2015-06-12 15:12:00   59.405  3
2015-06-12 15:42:00   85.181  3
2015-06-12 16:12:00   60.248  3
2015-06-12 16:42:00   73.101  3
2015-06-12 17:12:00   76.542  3
2015-06-12 17:42:00   68.395  3
2015-06-12 18:12:00   70.256  3
2015-06-12 18:42:00   97.717  3
2015-06-12 19:12:00   83.390  3
2015-06-12 19:42:00   64.006  3
2015-06-12 20:12:00   83.951  3
2015-06-12 20:42:00   84.092  3
2015-06-12 21:12:00   68.606  3
2015-06-12 21:42:00   58.422  3
2015-06-12 22:12:00   73.592  3
2015-06-12 22:42:00   61.618  3
2015-06-12 23:12:00   97.682  3
2015-06-12 23:42:00   83.319  3
2015-06-13 00:12:00   64.848  3
2015-06-13 00:42:00   61.161  3
2015-06-13 01:12:00   61.302  3
2015-06-13 01:42:00   59.124  3
2015-06-13 02:12:00   77.104  3
2015-06-13 02:42:00   61.723  3
2015-06-13 03:12:00   57.860  3
2015-06-13 03:42:00   60.424  3
2015-06-13 04:12:00   87.639  3
2015-06-13 04:42:00   67.236  3
2015-06-13 05:12:00   57.579  3
2015-06-13 05:42:00   75.383  3
2015-06-13 06:12:00   68.957  3
2015-06-13 06:42:00   57.509  3
2015-06-13 07:12:00   63.584  3
2015-06-13 07:42:00   63.549  4
2015-06-13 08:12:00   69.905  4
2015-06-13 08:42:00   57.088  4
2015-06-13 09:12:00   61.864  4
2015-06-13 09:42:00   59.651  4
2015-06-13 10:12:00   72.082  4
2015-06-13 10:42:00   71.310  4
2015-06-13 11:12:00   74.716  4
2015-06-13 11:42:00   71.942  4
2015-06-13 12:12:00   60.810  4
2015-06-13 12:42:00   59.827  4
2015-06-13 13:12:00   75.243  4
2015-06-13 13:42:00   62.109  4
2015-06-13 14:12:00   76.226  4
2015-06-13 14:42:00   64.743  4
2015-06-13 15:12:00   68.325  4
2015-06-13 15:42:00   75.875  4
2015-06-13 16:12:00   77.455  4
2015-06-13 16:42:00   67.587  4
2015-06-13 17:12:00   61.969  4
2015-06-13 17:42:00   62.039  4
2015-06-13 18:12:00   60.845  4
2015-06-13 18:42:00   74.786  4
2015-06-13 19:12:00   75.559  4
2015-06-13 19:42:00   56.350  4
2015-06-13 20:12:00   61.231  4
2015-06-13 20:42:00   64.076  4
2015-06-13 21:12:00   57.018  4
2015-06-13 21:42:00   61.267  4
2015-06-13 22:12:00   57.615  4
2015-06-13 22:42:00   60.880  4
2015-06-13 23:12:00   74.505  4
2015-06-13 23:42:00   86.550  4
2015-06-14 00:12:00   62.636  4
2015-06-14 00:42:00   76.998  4
2015-06-14 01:12:00   83.600  4
2015-06-14 01:42:00   63.198  4
2015-06-14 02:12:00   79.000  4
2015-06-14 02:42:00   77.069  4
2015-06-14 03:12:00  109.305  4
2015-06-14 03:42:00  117.241  4
2015-06-14 04:12:00  117.347  4
2015-06-14 04:42:00  113.554  4
2015-06-14 05:12:00  102.458  4
2015-06-14 05:42:00   66.710  4
2015-06-14 06:12:00   71.064  4
2015-06-14 06:42:00   79.562  4
2015-06-14 07:12:00  135.186  4

我已經成功繪制了它:

library(ggplot2)
library(scales)
g <-  ggplot(lang, aes(x = Index, y = ssc)) + 
  geom_line() +
  labs(x = "Date", y = expression(SSC ~ (g/m^3)))+
  scale_x_datetime(date_minor_breaks = "1 day", labels = date_format("%d-%m-%Y")) +
  theme_minimal() +
  stat_smooth(color = "#FC4E07", se = FALSE,
    method = "loess")

我想為每個he分組添加矩形。 這里顯示一個示例。

但是當我嘗試這樣做時:

g + geom_rect(lang,
               aes(xmin = Index,xmax = Index,ymin= -Inf,ymax=+Inf,
                   group = he))

我返回一個錯誤:

ggplot2不知道如何處理uneval類的數據。

這是來自xts包的示例數據的解決方案。

library(xts)
library(scales)
library(tidyverse)
library(timetk)

data(sample_matrix) # see ?xts
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
sample.xts <- sample.xts[, 1]
sample.xts$he <- rep(1:4, each = length(sample.xts[, 1]) / 4)
head(sample.xts)
#               Open he
#2007-01-02 50.03978  1
#2007-01-03 50.23050  1
#2007-01-04 50.42096  1
#2007-01-05 50.37347  1
#2007-01-06 50.24433  1
#2007-01-07 50.13211  1

現在,我創建第二個數據集,其中包含he每個值的開始和結束日期。 這是我們需要的geom_rect數據集。

sample_tbl <- timetk::tk_tbl(data = sample.xts) %>% 
 mutate(he = factor(he)) %>% 
 group_by(he) %>% 
 summarise(start = first(index),
           end = last(index))
sample_tbl
#  A tibble: 4 x 3
#  he    start               end                
#  <fct> <dttm>              <dttm>             
#1 1     2007-01-02 00:00:00 2007-02-15 00:00:00
#2 2     2007-02-16 00:00:00 2007-04-01 00:00:00
#3 3     2007-04-02 00:00:00 2007-05-16 00:00:00
#4 4     2007-05-17 00:00:00 2007-06-30 00:00:00

現在我們可以繪制情節了。

g <-  ggplot(sample.xts, aes(x = Index, y = Open)) + 
 geom_line() +
 labs(x = "Date", y = expression(SSC ~ (g/m^3)))+
 scale_x_datetime(date_minor_breaks = "1 day", labels = date_format("%d-%m-%Y")) +
 theme_minimal() +
 stat_smooth(color = "#FC4E07", se = FALSE,
             method = "loess")

g <- g + geom_rect(data = sample_tbl,
                   aes(xmin = start, xmax = end, ymin = -Inf, ymax = +Inf, group = he),
                   alpha = .3, inherit.aes = FALSE)

g

在此處輸入圖片說明

我建議盡管先繪制geom_rect層。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM