如何使用 geom_tile 绘制连续数值数据(x = 时间,y = 变量)后面的阶乘变量(光周期)

How to use geom_tile to plot factorial variable (photoperiod) behind continuous numeric data (x = time, y = variable)

我正在处理时间序列数据,在每个时间点,我对最多 16 个对象中的每一个对象进行多次测量,跨越多天。我目前已尽可能整齐地组织数据 - 这是一个片段。

> df
# A tibble: 23,844 x 40
   DateTime             Time exp_day Photoperiod    ZT Sex   Group Treatment Cohort Animal
   <dttm>              <dbl>   <dbl>       <dbl> <dbl> <chr> <chr> <chr>      <dbl>  <dbl>
 1 2021-10-18 12:47:00  0          1           1     6 M     mHFD  chABC          1    264
 2 2021-10-18 12:50:00  0.05       1           1     6 M     mHFD  chABC          1    264
 3 2021-10-18 12:53:00  0.1        1           1     6 M     mHFD  chABC          1    264
 4 2021-10-18 12:56:00  0.15       1           1     6 M     mHFD  chABC          1    264
 5 2021-10-18 12:59:00  0.2        1           1     6 M     mHFD  chABC          1    264
 6 2021-10-18 13:02:00  0.25       1           1     7 M     mHFD  chABC          1    264
 7 2021-10-18 13:05:00  0.3        1           1     7 M     mHFD  chABC          1    264
 8 2021-10-18 13:08:00  0.35       1           1     7 M     mHFD  chABC          1    264
 9 2021-10-18 13:11:00  0.4        1           1     7 M     mHFD  chABC          1    264
10 2021-10-18 13:14:00  0.45       1           1     7 M     mHFD  chABC          1    264
... with 23,834 more rows, and 30 more variables

每只动物的日期时间都是重复的 - 所以虽然我总共有 23,844 次观察,但每个对象有 1987 次:

> n_distinct(df$DateTime)
[1] 1987
> n_distinct(df$Animal)
[1] 12

我想做的是在 x 轴上绘制时间,在 y 轴上绘制变量,但将光周期(光与暗)绘制为数据后面的阴影或光条,如下所示.

我发现 几乎 解决了我的问题,但是,因为我对许多主题都有完全相同的日期时间值,所以最终发生的是每个时间点的光周期值被尽可能多的动物复制,导致阴影变得更暗而不是透明:

ggplot(data = df) + 
  aes(x = Time, y = RER) + 
  geom_tile(mapping = aes(
    fill = as.factor(Photoperiod), 
    y = mean(RER)),
    size = max(df$RER+1), 
    alpha = 0.05, 
    height = max(df$RER+1)
    ) +
  theme_classic()

总的来说,我对 ggplot 和 R 还很陌生,所以非常感谢有关清理它的建议(以及重新着色图块?!我仍然对 ggplot 的语法感到困惑)。谢谢你们,善良的网络陌生人!

编辑:我被要求提供玩具数据 - 抱歉,这太庞大了,但我想包含足够的数据点来充分演示这个问题,所以需要重复 2 只动物的时间:

> dput(toy_data)
structure(list(Time = c(0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 
0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 
1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 1.5, 1.55, 
1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2, 2.05, 2.1, 2.15, 
2.2, 2.25, 2.3, 2.35, 2.4, 2.45, 2.5, 2.55, 2.6, 2.65, 2.7, 2.75, 
2.8, 2.85, 2.9, 2.95, 3, 3.05, 3.1, 3.15, 3.2, 3.25, 3.3, 3.35, 
3.4, 3.45, 3.5, 3.55, 3.6, 3.65, 3.7, 3.75, 3.8, 3.85, 3.9, 3.95, 
4, 4.05, 4.1, 4.15, 4.2, 4.25, 4.3, 4.35, 4.4, 4.45, 4.5, 4.55, 
4.6, 4.65, 4.7, 4.75, 4.8, 4.85, 4.9, 4.95, 5, 5.05, 5.1, 5.15, 
5.2, 5.25, 5.3, 5.35, 5.4, 5.45, 5.5, 5.55, 5.6, 5.65, 5.7, 5.75, 
5.8, 5.85, 5.9, 5.95, 6, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 
0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 
0.9, 0.95, 1, 1.05, 1.1, 1.15, 1.2, 1.25, 1.3, 1.35, 1.4, 1.45, 
1.5, 1.55, 1.6, 1.65, 1.7, 1.75, 1.8, 1.85, 1.9, 1.95, 2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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), Animal = c(264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 
264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 264, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 265, 
265, 265, 265), RER = c(0.8160802, 0.8414883, 0.8616111, 0.8799883, 
0.8853653, 0.8618485, 0.8510329, 0.767729, 0.8031961, 0.8323785, 
0.746919, 0.735468, 0.7643967, 0.7355957, 0.7695087, 0.7857639, 
0.7985682, 0.8040746, 0.7941816, 0.7780811, 0.7847954, 0.7520697, 
0.771462, 0.8457935, 0.792997, 0.800769, 0.8275111, 0.8667183, 
0.9003724, 0.9300525, 0.9354021, 0.950434, 0.9377719, 0.9537385, 
0.957497, 0.8965999, 0.9208428, 0.9974157, 0.9558442, 0.9601937, 
0.9428826, 0.9235089, 0.954285, 0.9506574, 0.9658971, 0.943697, 
0.8846716, 0.9046962, 0.8838141, 0.880153, 0.893438, 0.8523828, 
0.8407152, 0.8374369, 0.8187368, 0.8196678, 0.818763, 0.798008, 
0.7942439, 0.7859463, 0.8233737, 0.8148571, 0.7615421, 0.7648527, 
0.8054302, 0.7941433, 0.8022749, 0.7896502, 0.7637175, 0.7835857, 
0.7571101, 0.7631419, 0.757715, 0.7518733, 0.7706109, 0.7968383, 
0.7996781, 0.7944622, 0.771527, 0.7670471, 0.7703733, 0.7666674, 
0.7745026, 0.7769082, 0.8292344, 0.8614953, 0.8414221, 0.8535554, 
0.8817732, 0.9425337, 0.9397846, 0.944727, 0.934262, 0.9363095, 
0.9287465, 0.9235471, 1.016768, 1.032561, 0.9664637, 0.9525408, 
0.9237975, 0.9090067, 0.9142333, 0.8927826, 0.9324146, 0.8827952, 
0.8865094, 0.8999404, 0.9107332, 0.9153765, 0.8917224, 0.85414, 
0.8840183, 0.8951464, 0.8506548, 0.8448542, 0.9129671, 0.9588402, 
0.9359728, 0.92337, 0.9468017, 0.7427233, 0.7401292, 0.731786, 
0.7219006, 0.8070412, 0.801992, 0.9282705, 0.7020188, 0.7323134, 
0.8721051, 0.7774209, 0.7546358, 0.7937038, 0.7963611, 0.8187793, 
0.8353969, 0.8687972, 0.8970048, 0.9179802, 0.8970544, 0.934752, 
0.9264608, 0.8939873, 0.8947772, 0.9109767, 0.9517013, 0.9122902, 
0.89452, 0.8663988, 0.8703817, 0.9046032, 0.9098795, 0.9236861, 
0.9013131, 0.9125994, 0.8989074, 0.8592472, 0.9292116, 0.8844017, 
0.9101141, 0.9072555, 0.9100726, 0.9335378, 0.938631, 0.9414188, 
0.9116636, 0.8453088, 0.8754182, 0.8618876, 0.8399428, 0.8514538, 
0.875167, 0.8847721, 0.8606132, 0.8260812, 0.8763582, 0.8457979, 
0.8227465, 0.8381892, 0.8416376, 0.8579757, 0.8317919, 0.7697672, 
0.8201662, 0.8380933, 0.8529457, 0.8165758, 0.83944, 0.7748498, 
0.8073791, 0.8753202, 0.8358103, 0.8230301, 0.825459, 0.8067374, 
0.8479258, 0.9074212, 0.8845561, 0.904029, 0.9196977, 0.8838978, 
0.8963885, 0.894173, 0.8619009, 0.8742555, 0.8846812, 0.8626546, 
0.7966505, 0.7857755, 0.8116125, 0.8324698, 0.818864, 0.7777231, 
0.7958539, 0.7686762, 0.7712164, 0.8568692, 0.7960808, 0.7915141, 
0.7687887, 0.7816828, 0.8098081, 0.7885166, 0.7697541, 0.7951097, 
0.8199027, 0.8549485, 0.8789132, 0.9114284, 0.8974345, 0.8821911, 
0.8926013, 0.9105431, 0.9270198, 0.8770642, 0.8436169, 0.9331129, 
0.9502046, 0.923188, 0.9427496, 0.9521099)), row.names = c(NA, 
-242L), class = c("tbl_df", "tbl", "data.frame"))

我会为光周期绘图层创建一个单独的 de-duplicated 数据框。像这样:

library(dplyr)

pp_data = toy_data %>% distinct(Time, Photoperiod)

ggplot(data = toy_data) + 
  aes(x = Time, y = RER) +
  geom_line(aes(color = factor(Animal))) +
  geom_tile(
    data = pp_data,
    mapping = aes(
      fill = factor(Photoperiod), 
      y = 1 ## since we're using Inf height, this doesn't matter
    ),
    alpha = 0.2, 
    height = Inf ## tiles will go all the way up and down
  ) +
  ## colors for photoperiod tiles, and no legend/guide
  scale_fill_manual(values = c("0" = "darkblue", "1" = "yellow"), guide = "none") +
  ## colors for the animals
  scale_color_manual(values = c("black", "darkorange3")) +
  ## no padding on the x-axis
  scale_x_continuous(expand = expansion(0, 0)) +
  theme_classic()