微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决将 POSIXct 转换为 ggplot 中的儒略日期

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

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 轴上不那么拥挤。有没有办法做到这一点?

enter image description here

解决方法

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

根据您之前的问题,您的问题可能与尝试将日期时间对象传递给需要日期对象的函数有关。在这种情况下,我使用了 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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。