【问题标题】:Adding date ticks to ggplot in R在 R 中向 ggplot 添加日期刻度
【发布时间】:2015-09-16 07:05:14
【问题描述】:

我正在尝试在此图中的 x 轴上添加刻度以显示一年中的所有月份:

我的代码如下:

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

我的时间变量的类:

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

【问题讨论】:

  • 嗯,df_test$time的类是什么??
  • 已编辑以显示正在使用的数据框。我相信它是类日期时间,但不完全确定如何检查。
  • class(df_test$time)。您应该使用dput(df_test) 提供您的数据或代表性样本(足以证明问题)。
  • 我已经输出了 dput(df_test) 但是共享这些数据的最佳做法是什么?
  • 取决于有多少。如果这足以证明问题,您可以dput(head(df_test, 100))(前 100 行)。或者,您可以将数据集上传到某处并发布链接,但在这种情况下,这可能有点矫枉过正。无论如何,我的回答能解决你的问题吗? POSIXct 对scale_x_date(...) 来说不是问题。

标签: r ggplot2


【解决方案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"))

【讨论】:

  • 或者例如,你可以使用scale_x_date(date_breaks="2 months", date_labels = "%b")
【解决方案2】:

使用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"))

【讨论】:

  • 我收到以下错误:错误:输入无效:date_trans 仅适用于 Date 类的对象
  • @ttct 所以time 变量可能不是日期对象,而可能是一个字符。
  • @PaulHiemstra 他在他的问题中发布了class(df_test$time)
  • 可以公开吗? Google 想要我登录。
【解决方案3】:

结合@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"))

给出结果:

【讨论】:

    【解决方案4】:

    您正在尝试在 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 包后,我收到以下错误:错误:找不到函数“as.date”。你对如何解决这个问题有什么建议吗?谢谢
    • 已修复,只需将 as.date(time) 中的 'd' 大写
    • @tctt 还修正了我回答中的错字,感谢您的提醒。
    猜你喜欢
    • 1970-01-01
    • 2017-08-15
    • 2021-11-01
    • 2015-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    相关资源
    最近更新 更多