簡體   English   中英

ggplot2,x軸不識別日期

[英]ggplot2, x-axis not recognizing dates

嘗試繪制以下數據框(稱為 bob):

1

由於原始日期以 d/m/y 為單位,因此我使用 Finaldate 和 Value 來繪制圖表。 這是用於繪制圖形的代碼:

ggplot(Bob, aes(Finaldate, Value)) +geom_line() + geom_point(size = 3) + labs(title = "TITLE",subtitle = "SUBTITLE", y = "Y", x = "X") + theme_fivethirtyeight()+scale_y_continuous(name="name", labels = scales::comma)+theme(legend.title = element_blank())+scale_x_discrete(guide = guide_axis(check.overlap = TRUE))

雖然我確實得到了輸出,但它不是時間序列,而是日期不按順序排列,而且情節毫無意義。 附上劇情副本。

在此處輸入圖像描述

不知道如何解決這個問題,並嘗試了幾種不同的方法

您是否嘗試過使用+ scale_x_date(date_labels = "%d %m %Y") (ggplot2)

https://r-graph-gallery.com/279-plotting-time-series-with-ggplot2.html

您需要將Finaldate轉換為日期——它被視為一個字符,因此所有日期都按“字母”順序排列。 嘗試:

Bob$finalDate <- as.Date(Bob$finalDate, format = "%m/%d/%Y")

暫無
暫無

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

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