我想重现以下情节,但我想每 4 个月添加一次日期标签而不是数字。
我必须将 date_time 值转换为数字才能绘制
annotate("rect")
,因为我希望它将矩形的长度延伸到绘图的边缘,类似于 geom_hline
。我发现 annotate
方法最快。我现在有一个问题,因为我想在 x 轴上使用日期标签而不是数字。
我尝试过使用
scale_x_continuous
,但这花了太长时间并且似乎不起作用。
感谢您的帮助。
# Reproducible data
set.seed(123)
start_date_time <- as.POSIXct("2023-07-01 00:00:00")
end_date_time <- as.POSIXct("2023-07-02 00:00:00")
date_time <- seq(from = start_date_time, to = end_date_time, by = "15 min")
depth <- runif(length(date_time), min = 0, max = 0.5)
df <- data.frame(date_time = date_time, depth = depth)
df$N_dt <- as.numeric(df$date_time)
x_point <- min(df$N_dt)
full_level <- 0.4
pipe_bottom <- 0.15
pipe_top <- 0.35
pipe <- 0.25
df %>%
ggplot(aes(x = N_dt,
y = depth)) +
geom_line(size = 0.25) +
labs(x = "Date",
y = "Water level (m)") +
geom_hline(yintercept = full_level, linetype = 'dashed',
color = 'black') +
annotate("text", x = x_point, y = (full_level + 0.03),
label = "Full level", size = 8 / .pt) +
annotate("text", x = x_point, y = pipe,
label = "Pipe", size = 8 / .pt) +
annotate("rect", xmin = -Inf, xmax = Inf,
ymin = pipe_bottom, ymax = pipe_top, alpha = .1) +
theme_classic() +
theme(
plot.title = element_blank(),
axis.text = element_text(size = 8),
axis.title = element_text(size = 8.5),
legend.title = element_blank(),
legend.position = "top",
panel.border = element_rect(color = "black", fill = NA)
)
您可以使用
labels
的 scale_x_continuous
参数将数字转换回日期时间。
df %>%
ggplot(aes(x = N_dt,
y = depth)) +
geom_line(size = 0.25) +
labs(y = "Water level (m)") +
scale_x_continuous(labels = ~ format(as.POSIXct(.x, origin = '1970-01-01'),
'%d %b %Y %H:%M')) +
geom_hline(yintercept = full_level, linetype = 'dashed',
color = 'black') +
annotate("text", x = x_point, y = (full_level + 0.03),
label = "Full level", size = 8 / .pt) +
annotate("text", x = x_point, y = pipe,
label = "Pipe", size = 8 / .pt) +
annotate("rect", xmin = -Inf, xmax = Inf,
ymin = pipe_bottom, ymax = pipe_top, alpha = .1) +
theme_classic() +
theme(
plot.title = element_blank(),
axis.text = element_text(size = 8),
axis.title = element_text(size = 8.5),
legend.title = element_blank(),
legend.position = "top",
panel.border = element_rect(color = "black", fill = NA)
)
一种选择是坚持日期时间,这样您就可以通过
scale_x_datetime
轻松设置所需的日期休息时间。为了使这项工作与您的矩形一起工作,我直接根据您的日期范围设置 xmin
和 xmax
。这一步的棘手部分是,这样做我们必须考虑扩展:
library(ggplot2)
# Set the range for the rects
range_rect <- range(df$date_time)
# Add some expansion
range_rect <- range_rect + .1 * c(-1, 1) * diff(range_rect)
x_point <- min(df$date_time)
df %>%
ggplot(aes(
x = date_time,
y = depth
)) +
geom_line(size = 0.25) +
labs(
x = "Date",
y = "Water level (m)"
) +
geom_hline(
yintercept = full_level, linetype = "dashed",
color = "black"
) +
annotate("text",
x = x_point, y = (full_level + 0.03),
label = "Full level", size = 8 / .pt
) +
annotate("text",
x = x_point, y = pipe,
label = "Pipe", size = 8 / .pt
) +
annotate("rect",
xmin = range_rect[[1]], xmax = range_rect[[2]],
ymin = pipe_bottom, ymax = pipe_top, alpha = .1
) +
# As we already added the expansion via range_rect,
# set it to zero for the scale
scale_x_datetime(
expand = c(0, 0)
) +
theme_classic() +
theme(
plot.title = element_blank(),
axis.text = element_text(size = 8),
axis.title = element_text(size = 8.5),
legend.title = element_blank(),
legend.position = "top",
panel.border = element_rect(color = "black", fill = NA)
)