在 ggplot 中将 POSIXct 转换为儒略日期

Converting POSIXct to Julian Dates in ggplot

我一直在尝试转换 POSIXct 格式,以便我的日期和时间能够反映 Julian 日期。

ind$DateAndTime <- as.POSIXct(ind$DateAndTime, tz = "UTC",
                                  origin = '1970-01-01')

ind$DateAndTime<- format(as.POSIXct(ind_steps$t2),"%y%j")

我曾使用这两行代码来这样做,但我现在无法使用 ggplot 绘制它们。

    plot_list[[i]]  <- ggplot(ind, aes(x = DateAndTime, y = NSD)) + 
      geom_line() + theme_bw() +
      ggtitle(random_tables[i]) +
      theme(axis.text.x = element_text(angle = 90))

当我绘制它时,我得到了这个,朱利安日期是垂直的,但它们仍然重叠。我想让图表更明显地显示儒略日期,并显示所有其他儒略日期,这样它在 x 轴上就不会那么拥挤。有办法吗?

这是完整的代码。没有任何样本数据,很难提供一个确切的例子。

根据您之前的问题,您的问题可能与尝试将日期时间对象传递给需要日期对象的函数有关。在这种情况下,我使用 as.Date()scale_x_date(),在您的情况下,您可能想要使用 as.POSIXct()scale_x_datetime()

#create dummy data
DateAndTime = 18000:18300
NSD = DateAndTime/10000
ind <-data.frame(DateAndTime, NSD)

#convert the DateAndTime column into a date object
ind$DateAndTime <- as.Date(ind$DateAndTime, tz = "UTC",
                              origin = '1970-01-01')

#Plot table and format x-axis
ggplot(ind, aes(x = DateAndTime, y = NSD)) + 
   geom_line() + theme_bw() +
   ggtitle("Demo Title") +
   scale_x_date(date_breaks = "1 month", date_labels = "%y-%j")
   theme(axis.text.x = element_text(angle = 90))