简体   繁体   English

ggplot2,x轴不识别日期

[英]ggplot2, x-axis not recognizing dates

Trying to plot the following data frame (call it bob):尝试绘制以下数据框(称为 bob):

1 1

Since the original date is in d/m/y, I use Finaldate and Value to graph.由于原始日期以 d/m/y 为单位,因此我使用 Finaldate 和 Value 来绘制图表。 Here is the code used to graph:这是用于绘制图形的代码:

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)) 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))

While I do get an output, it is not as a time series but rather the dates are not in order and the plot makes no sense.虽然我确实得到了输出,但它不是时间序列,而是日期不按顺序排列,而且情节毫无意义。 Attached a copy of the plot as well.附上剧情副本。

enter image description here在此处输入图像描述

Not sure how to fix this problem, and have tried a couple of different things不知道如何解决这个问题,并尝试了几种不同的方法

Have you tried using + scale_x_date(date_labels = "%d %m %Y") (ggplot2)您是否尝试过使用+ scale_x_date(date_labels = "%d %m %Y") (ggplot2)

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

You need to convert Finaldate to a date -- it is being treated as a character so all the dates are in "alphabetical" order.您需要将Finaldate转换为日期——它被视为一个字符,因此所有日期都按“字母”顺序排列。 Try:尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM