在R中的ggplot图表中添加日期刻度

6

我正在尝试为这张图表的x轴添加刻度,以显示一年中的所有月份:

enter image description here

我的代码如下:

library(ggplot2)
library(scales)
p <- ggplot(df_test, aes(time, reading))
p + geom_point(alpha = 1/4) + geom_smooth()

我尝试使用scale_x_date,但遇到了以下错误:
Error: Invalid input: date_trans works with objects of class Date only

这是我正在使用的数据框:

 hour   reading   date                time
 1      53        1/1/15 2015-01-01 01:00:00
 2      55        1/1/15 2015-01-01 02:00:00
 3      56        1/1/15 2015-01-01 03:00:00

我的时间变量的类:

class(df_test$time) "POSIXct" "POSIXt"


1
df_test$time的类是什么? - jlhoward
编辑以显示使用的数据框架。我相信它是datetime类,但不确定如何检查。 - timothyylim
请提供您的数据或代表性样本(足以演示问题),使用 dput(df_test),并输入以下代码 class(df_test$time) - jlhoward
我已经输出了dput(df_test),但是分享这些数据的最佳实践是什么? - timothyylim
取决于数据量大小。如果前100行足以说明问题,您可以使用dput(head(df_test, 100))命令。或者您可以将数据集上传到某个地方并发布链接,但在这种情况下可能有些过度。无论如何,我的回答是否解决了您的问题?对于scale_x_date(...),POSIXct不应该是问题。 - jlhoward
4个回答

4
使用scale_x_date(breaks="month", labels=date_format("%b%))。以下是示例。
library(quantmod)
sp500 <- getSymbols("SP500", src="FRED", auto.assign=FALSE)
sp500 <- sp500["2015-01-01::"]
sp500 <- data.frame(time=as.POSIXct(index(sp500), origin="1970-01-01"),sp500)
class(sp500$time)
# [1] "POSIXct" "POSIXt" 

library(ggplot2)
library(scales)    # for date_format(...)
ggplot(sp500, aes(x=as.Date(time), y=SP500))+
  geom_line()+
  scale_x_date(breaks="month", labels=date_format("%b"))

enter image description here


我遇到了以下错误:错误:无效输入:date_trans仅适用于Date类的对象。 - timothyylim
@ttct 所以很可能 time 变量不是一个日期对象,而是一个字符。 - Paul Hiemstra
@PaulHiemstra 在他的问题中发布了 class(df_test$time) - jlhoward
我已经在这里分享了数据框: https://drive.google.com/open?id=0B-utsAVxMcQ5ckNxVjRZYWFKT0k - timothyylim
你能公开一下吗?谷歌要求我登录。 - jlhoward
POSIXlt/POSIXct 对象转换为 date 应该可以解决问题,参见 as.date。@jlhoward 我没有看到编辑。 - Paul Hiemstra

3

您正在尝试在POSIXct对象上使用针对Date的比例。解决方案是使用as.datePOSIXct对象转换为date

> Sys.time()
[1] "2015-09-16 09:52:42 CEST"
> as.Date(Sys.time())
[1] "2015-09-16"

如果您想在 data.frame 上实现此功能,我建议使用 dplyr 包:

df_test = df_test %>% mutate(time = as.Date(time))

安装了dplyr包之后,我遇到了以下错误:Error: could not find function "as.date"。您有什么建议可以解决这个问题吗?谢谢。 - timothyylim
修复了,只需要将 as.date(time) 中的 'd' 大写即可。 - timothyylim
@tctt 还纠正了我回答中的拼写错误,感谢提醒。 - Paul Hiemstra

2

结合 @PaulHiemstra 和 @jihoward 的答案,我已经成功地找到了答案。

首先使用 dplyr 库重新处理数据:

library(dplyr)
df_test1 = df_test %>% mutate(time = as.Date(time))

然后使用scale_x_dates:
library(ggplot2)
library(scales)
p <- ggplot(df_test1, aes(time, reading))
p + geom_point(alpha = 1/4)+
  scale_x_date(breaks="month", labels=date_format("%b"))

给出的结果如下:

enter image description here


1
自2015年以来,date_format()已被弃用。请使用label_date()scales::label_date()进行替换。它可能没有被加载到您的命名空间中,但应该随ggplot一起提供,因此可能需要使用scales::。以下是@timothyylim接受的答案副本,已经进行了更改。
library(dplyr)
df_test1 = df_test %>% mutate(time = as.Date(time))

library(ggplot2)
library(scales)
p <- ggplot(df_test1, aes(time, reading))
p + geom_point(alpha = 1/4)+
  scale_x_date(breaks="month", labels = scales::label_date("%b"))

1
或者例如,您可以使用 scale_x_date(date_breaks="2 months", date_labels = "%b") - Habib Karbasian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接