簡體   English   中英

在ggplot中將POSIXct轉換為朱利安日期

[英]Converting POSIXct to Julian Dates in 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))

當我 plot 得到這個時,朱利安日期是垂直的,但它們仍然重疊。 我想讓圖表更明顯地顯示朱利安日期,並顯示每隔一個朱利安日期,這樣它在 x 軸上就不會那么狹窄。 有沒有辦法做到這一點?

在此處輸入圖像描述

這是完整的代碼。 沒有任何樣本數據,很難提供一個准確的例子。

從您之前的問題來看,您的問題可能與嘗試將日期時間 object 傳遞給 function 期望日期 object 有關。 在這種情況下,我使用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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM