gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 06:23:16 32 4
gpt4 key购买 nike

我有一个数据框,其中包含一个仪器在连续时间段内在四个位置测量的值。我希望将夜间(下午 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

当我排除对 geom_rect 的调用时,X 轴使用 facet_grid(..., scales = 'free_x') 正确缩放。我如何在 X 轴刻度不扩展的情况下在第二个图上绘制阴影 geom_rect

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) +

最佳答案

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

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

关于r - 我如何使用 geom_rect 在夜间添加阴影而不扩展刻面比例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58600521/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com