尝试映射 geom_vline 的值,但未使用 R 中的 ggplot 在 x 轴上的正确位置绘制

Trying to map a value for geom_vline, but is not plotting in the correct place on the x axis with ggplot in R

我目前正在尝试生成 NOAA 潮汐预测图(x = 日期时间,y = 水位),其中 dawn/sunrise/dusk/sunset 时间作为沿 x 轴时间轴的垂直线。

rnoaa 包调用数据并以 POSIXct 格式提供预测日期时间。 suncalc 库为我提供了一个数据框,其中每个日期都在 POSIXct 格式的范围内的日出、日落等。

library(rnoaa)
library(tidyverse)
library(ggplot2)
library(suncalc)
march.tides <- as.data.frame(coops_search(station_name = 8551762, 
                                          begin_date = 20200301, end_date = 20200331, 
                                          datum = "mtl", product = "predictions"))
march.tides <- march.tides %>%
  mutate(yyyy.mm.dd = as.Date(predictions.t))
dates <- unique(march.tides$yyyy.mm.dd)
sunlight.times <- getSunlightTimes(date = seq.Date(as.Date("2020/3/1"), as.Date("2020/3/31"), by = 1), 
                lat = 39.5817, lon = -75.5883, tz = "EST")

然后我有一个循环,为每个日历日期吐出单独的图 - 这很有效。垂直线在图表上绘制没有错误,但肯定是在错误的位置(日出在上午 11 点左右绘制,而应该是 06:30)。

for (i in seq_along(dates)) {
  plot <- ggplot(subset(march.tides, march.tides$yyyy.mm.dd==dates[i])) +
    aes(x = predictions.t, y = predictions.v) +
    geom_line(size = 1L, colour = "#0c4c8a") +
    theme_bw() + 
    geom_vline(xintercept = sunlight.times$sunrise) +
    geom_vline(xintercept = sunlight.times$sunset) +
    geom_vline(xintercept = sunlight.times$dawn, linetype="dotted") +
    geom_vline(xintercept = sunlight.times$dusk, linetype="dotted") +
    ggtitle(dates[i])
  print(plot)
}

我也可以对单独的日期进行分面,而不是这种循环方法。即使我将数据子集化为单个日期,垂直线仍然无法正确绘制。

我想知道问题是否与时区有关。如果我尝试将时区参数附加到潮汐预测数据调用中,我会收到错误消息:

Error in if (!repeated && grepl("%[[:xdigit:]]{2}", URL, useBytes = TRUE)) return(URL) : 
  missing value where TRUE/FALSE needed

您似乎想使用 EST 作为您的时区,因此您可以将 predictions.t.

包含在您的转换中

我会在 ggplot 中使用 scale_x_datetime 明确说明您想在 x 轴上标记的内容,包括时区。

library(rnoaa)
library(tidyverse)
library(ggplot2)
library(suncalc)
library(scales)

march.tides <- as.data.frame(coops_search(station_name = 8551762, 
                                          begin_date = 20200301, end_date = 20200331, 
                                          datum = "mtl", product = "predictions"))

march.tides <- march.tides %>%
  mutate(yyyy.mm.dd = as.Date(predictions.t, tz = "EST"))
dates <- unique(march.tides$yyyy.mm.dd)
sunlight.times <- getSunlightTimes(date = seq.Date(as.Date("2020/3/1"), as.Date("2020/3/31"), by = 1), 
                                   lat = 39.5817, lon = -75.5883, tz = "EST")

for (i in seq_along(dates)) {
  plot <- ggplot(subset(march.tides, march.tides$yyyy.mm.dd==dates[i])) +
    aes(x = predictions.t, y = predictions.v) +
    geom_line(size = 1L, colour = "#0c4c8a") +
    theme_bw() + 
    geom_vline(xintercept = sunlight.times$sunrise) +
    geom_vline(xintercept = sunlight.times$sunset) +
    geom_vline(xintercept = sunlight.times$dawn, linetype="dotted") +
    geom_vline(xintercept = sunlight.times$dusk, linetype="dotted") +
    ggtitle(dates[i]) +
    scale_x_datetime(labels = date_format("%b %d %H:%M", tz = "EST"))
  print(plot)
}

情节