【问题标题】:Error: Invalid input: time_trans works with objects of class POSIXct only for geom_candlestick plot错误:输入无效:time_trans 仅适用于 geom_candlestick 绘图的 POSIXct 类对象
【发布时间】:2021-12-29 09:25:21
【问题描述】:

我有一些价格数据(在这个问题的结尾),我正在尝试使用以下方法绘制 geom_candlestick 图表:

library(scales)
library(ggplot2)
library(tidyquant)

rects <- data.frame(xstart = as.POSIXct(c("2019-01-24 21:40:00", "2019-01-24 23:15:00"), tz="CST"),
                    xend = as.POSIXct(c("2019-01-24 22:10:00", "2019-01-24 21:45:00"), tz="CST"),
                    col = c('in_period', 'out_period'))

ggplot(df, aes(x = date)) +
  geom_candlestick(aes(open = open, high = high, low = low, close = close), alpha=0.7) +
  geom_rect(data = rects, aes(xmin = as.Date(xstart), xmax = as.Date(xend), ymin = -Inf, ymax = Inf, fill = col),
            alpha = 0.1, inherit.aes = FALSE) +
  scale_x_datetime(labels = date_format("%H:%M:%S"))

情节的预期效果将类似于下图(这意味着根据时间进入或退出时间段,我们设置不同的背景颜色),但我得到一个错误,我不明白它来自哪里,因为我已将其转换为as.POSIXctas.Date

有人可以帮助解决这个问题吗?谢谢。

Error: Invalid input: time_trans works with objects of class POSIXct only

数据:

df <- structure(list(date = structure(c(1548342900, 1548342600, 1548342300, 
1548342000, 1548341700, 1548341400, 1548341100, 1548340800, 1548340500, 
1548340200, 1548339900, 1548339600, 1548339300, 1548339000, 1548338700, 
1548338400, 1548338100, 1548337800, 1548337500, 1548337200), class = c("POSIXct", 
"POSIXt"), tzone = ""), low = c(101.95, 102.1, 102.28, 102.29, 
102.31, 102.33, 102.33, 102.34, 102.34, 102.5, 102.59, 102.51, 
102.51, 102.5, 102.6, 102.59, 102.68, 102.67, 102.71, 102.8), 
    high = c(102.11, 102.29, 102.36, 102.34, 102.4, 102.35, 102.43, 
    102.42, 102.51, 102.61, 102.68, 102.59, 102.57, 102.67, 102.7, 
    102.7, 102.69, 102.83, 102.91, 102.95), open = c(102.11, 
    102.29, 102.29, 102.32, 102.34, 102.34, 102.34, 102.34, 102.51, 
    102.61, 102.59, 102.51, 102.57, 102.67, 102.7, 102.67, 102.68, 
    102.83, 102.86, 102.82), close = c(101.96, 102.1, 102.29, 
    102.32, 102.32, 102.34, 102.34, 102.34, 102.34, 102.5, 102.68, 
    102.59, 102.51, 102.5, 102.66, 102.7, 102.69, 102.73, 102.83, 
    102.9), volume = c(68.47, 55.24, 12.54, 128.77, 86.44, 45.84, 
    47.4, 31.97, 61.93, 31.02, 4.85, 33.49, 3.01, 180.48, 43.45, 
    61.53, 17.27, 34.86, 47.1, 14.61)), row.names = c(NA, -20L
), class = c("tbl_df", "tbl", "data.frame"))

编辑:

@Marco_CH 代码的运行输出:

Warning messages:
1: In strptime(xx, f, tz = tz) : unknown timezone 'CST'
2: In as.POSIXct.POSIXlt(x) : unknown timezone 'CST'
3: In strptime(x, f, tz = tz) : unknown timezone 'CST'
4: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone 'CST'
5: In strptime(xx, f, tz = tz) : unknown timezone 'CST'
6: In as.POSIXct.POSIXlt(x) : unknown timezone 'CST'
7: In strptime(x, f, tz = tz) : unknown timezone 'CST'
8: In as.POSIXct.POSIXlt(as.POSIXlt(x, tz, ...), tz, ...) :
  unknown timezone 'CST'

【问题讨论】:

    标签: r datetime ggplot2 lubridate posixct


    【解决方案1】:

    试试这个: 看这里https://www.r-bloggers.com/2011/11/doing-away-with-%E2%80%9Cunknown-timezone%E2%80%9D-warnings/

    library(scales)
    library(ggplot2)
    library(tidyquant)
    
    
    Sys.getenv("TZ")
    Sys.setenv(TZ="Europe/Berlin")
    Sys.getenv("TZ")
    #as.POSIXct(t, tz=getOption("tz"))
    
    
    rects <- data.frame(xstart = as.POSIXct(c("2019-01-24 21:40:00", "2019-01-24 23:15:00")),
                        xend = as.POSIXct(c("2019-01-24 22:10:00", "2019-01-24 21:45:00")),
                        col = c('in_period', 'out_period'))
    
    
    rects1 <- rects %>% 
      mutate(across(starts_with("x"), ~as.POSIXct(., tz=getOption("tz"))))
    
    ggplot(df, aes(x = date)) +
      geom_candlestick(aes(open = open, high = high, low = low, close = close), alpha=0.7) +
      geom_rect(data = rects1, aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf, fill = col),
                alpha = 0.1, inherit.aes = FALSE) +
      scale_x_datetime(labels = date_format("%H:%M:%S"))
    

    【讨论】:

    • 谢谢,你知道如何从图例中隐藏红色和深蓝色吗?或者我应该问另一个问题。
    【解决方案2】:

    更新:

    现在没有警告(检查 TZ 是否正确,“CST”不起作用)并且只有图例中需要的项目。

    df <- structure(list(date = structure(c(1548342900, 1548342600, 1548342300, 
                                            1548342000, 1548341700, 1548341400, 1548341100, 1548340800, 1548340500, 
                                            1548340200, 1548339900, 1548339600, 1548339300, 1548339000, 1548338700, 
                                            1548338400, 1548338100, 1548337800, 1548337500, 1548337200), class = c("POSIXct", 
                                                                                                                   "POSIXt"), tz = "CST6CDT"), low = c(101.95, 102.1, 102.28, 102.29, 
                                                                                                                                                   102.31, 102.33, 102.33, 102.34, 102.34, 102.5, 102.59, 102.51, 
                                                                                                                                                   102.51, 102.5, 102.6, 102.59, 102.68, 102.67, 102.71, 102.8), 
                         high = c(102.11, 102.29, 102.36, 102.34, 102.4, 102.35, 102.43, 
                                  102.42, 102.51, 102.61, 102.68, 102.59, 102.57, 102.67, 102.7, 
                                  102.7, 102.69, 102.83, 102.91, 102.95), open = c(102.11, 
                                                                                   102.29, 102.29, 102.32, 102.34, 102.34, 102.34, 102.34, 102.51, 
                                                                                   102.61, 102.59, 102.51, 102.57, 102.67, 102.7, 102.67, 102.68, 
                                                                                   102.83, 102.86, 102.82), close = c(101.96, 102.1, 102.29, 
                                                                                                                      102.32, 102.32, 102.34, 102.34, 102.34, 102.34, 102.5, 102.68, 
                                                                                                                      102.59, 102.51, 102.5, 102.66, 102.7, 102.69, 102.73, 102.83, 
                                                                                                                      102.9), volume = c(68.47, 55.24, 12.54, 128.77, 86.44, 45.84, 
                                                                                                                                         47.4, 31.97, 61.93, 31.02, 4.85, 33.49, 3.01, 180.48, 43.45, 
                                                                                                                                         61.53, 17.27, 34.86, 47.1, 14.61)), row.names = c(NA, -20L
                                                                                                                                         ), class = c("tbl_df", "tbl", "data.frame"))
    library(scales)
    library(ggplot2)
    library(tidyquant)
    library(lubridate)
    
    rects <- data.frame(xstart = as.POSIXct(c("2019-01-24 21:40:00", "2019-01-24 23:15:00"), tz = "CST6CDT"),
                        xend = as.POSIXct(c("2019-01-24 22:10:00", "2019-01-24 21:45:00"), tz="CST6CDT"),
                        col = c('in_period', 'out_period'))
    
    ggplot(df, aes(x = date)) +
      geom_candlestick(aes(open = open, high = high, low = low, close = close), alpha=0.7) +
      geom_rect(data = rects, aes(xmin = floor_date(xstart, "day"), xmax = xend, ymin = -Inf, ymax = Inf, fill=col),
                alpha = 0.1, inherit.aes = FALSE) +
      scale_x_datetime(labels = date_format("%H:%M:%S")) + 
      scale_fill_discrete(name = "Period", label = c('in_period', 'out_period'), limits = c('in_period', 'out_period'))
    

    输出:

    PS: 如果您不需要带有 floor_date 的部分,只需将 floor_date(xstart, "day") 更改为 xstart

    【讨论】:

    • 谢谢,你知道如何在图例中隐藏红色和深蓝色吗?
    • 我在运行你的新代码时收到警告,我粘贴在问题的最后部分。
    • 我刚刚更新了我的帖子。现在没有警告,只有图例中的必需项。
    • 就我而言,它没有改变。它仍然像以前一样是蓝色和红色。也许最好为此启动一个单独的线程,因为这与 posixct 问题无关:-)
    • 关于这个问题,我在这里提出一个新问题:stackoverflow.com/questions/70522654/…,请查收。
    猜你喜欢
    • 2020-05-03
    • 1970-01-01
    • 2022-01-13
    • 2020-07-20
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    相关资源
    最近更新 更多