我如何在夜间使用geom_rect添加阴影而不使小平面比例扩展?

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

我有一个数据框,其值在连续时间段内在四个位置上用仪器测量。我希望将晚上的时间(下午6点至凌晨6点)涂成灰色以帮助解释。下面的代码正确地绘制了此代码,除了当仪器未在每个位置收集数据时,我也想使用facet_grid(..., scales = 'free_x')删除不必要的时间段。

require(ggplot2)

df.long <- data.frame(Timestamp = seq.POSIXt(as.POSIXct("2018-07-05 18:00:00", format = '%Y-%m-%d %H:%M:%S'), by = 'hour', length.out = 192),
                      Location = c(rep('A', 48), rep('B', 48), rep('C', 48), rep('D', 48)),
                      value = sin(seq(1:192)/4))

shade <- data.frame(dusk = seq.POSIXt(as.POSIXct("2018-07-05 18:00:00", format = '%Y-%m-%d %H:%M:%S'), by = 'day', length.out = 8), 
                    dawn = seq.POSIXt(as.POSIXct("2018-07-06 06:00:00", format = '%Y-%m-%d %H:%M:%S'), by = 'day', length.out = 8),
                    top = Inf,
                    bottom = -Inf)

ggplot(df.long) +
  geom_rect(data = shade, 
            aes(xmin = dusk, xmax = dawn,ymin = bottom, ymax = top), 
            fill = 'light grey', alpha = 0.5) +
  geom_line(aes(x = Timestamp, y = value, col = Location)) +
  geom_point(aes(x = Timestamp, y = value, fill = Location), pch = 21) +
  facet_grid( ~ Location, scales = 'free_x') +
  ylab('Flux (mg O2 m-2 h-1)') +
  theme_bw()

enter image description here

当我排除对facet_grid(..., scales = 'free_x')的调用时,用geom_rect正确缩放了X轴。我如何在第二张图上绘制阴影的geom_rect而无需扩展X轴刻度?

ggplot(df.long) +
  # geom_rect(data = shade, aes(xmin = dusk, xmax = dawn,
  #                             ymin = bottom, ymax = top), fill = 'light grey', alpha = 0.5) +
  geom_line(aes(x = Timestamp, y = value, col = Location)) +
  geom_point(aes(x = Timestamp, y = value, fill = Location), pch = 21) +
  facet_grid( ~ Location, scales = 'free_x') +
  ylab('Flux (mg O2 m-2 h-1)') +
  theme_bw() +
  theme(legend.position = 'none')

enter image description here

我也尝试用geom_rect替换为:

annotate("rect", 
         xmin = shade$dusk, xmax = shade$dawn, ymin = shade$bottom, ymax = shade$top, 
         fill = "light grey", alpha = 0.5) +
r ggplot2 posixct facet-grid
1个回答
0
投票

一种可能的方法是仅为每个构面定义相关的阴影矩形:

library(dplyr)

shade2 <- shade %>%
  # replicate shade for each facet
  slice(rep(seq(1, n()),
            times = n_distinct(df.long$Location))) %>%
  mutate(Location = rep(sort(unique(df.long$Location)),
                        each = n()/4)) %>%

  # calculate actual x-range associated with each facet, & join with shade
  left_join(df.long %>%
              group_by(Location) %>%
              summarise(xmin = min(Timestamp),
                        xmax = max(Timestamp)) %>%
              ungroup(),
            by = "Location") %>%

  # for each shade facet, keep only rows within relevant x-range
  filter(dusk >= xmin & dawn <= xmax)

ggplot(df.long) +
  geom_rect(data = shade2, # replace shade with shade2, everything else is unchanged
            aes(xmin = dusk, xmax = dawn,
                ymin = bottom, ymax = top),
            fill = 'light grey', alpha = 0.5) +
  geom_line(aes(x = Timestamp, y = value, col = Location)) +
  geom_point(aes(x = Timestamp, y = value, fill = Location), pch = 21) +
  facet_grid( ~ Location, scales = 'free_x') +
  ylab('Flux (mg O2 m-2 h-1)') +
  theme_bw()

plot

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