Error: Invalid input: time_trans works with objects of class POSIXct only for geom_candlestick plot
Error: Invalid input: time_trans works with objects of class POSIXct only for geom_candlestick plot
我有一些价格数据(在这个问题的末尾),我正在尝试使用以下方法绘制 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"))
plot的预期效果会和下图差不多(意思是根据时间是不是周期,我们设置不同的背景颜色),但是我报错了,我不知道从哪里来的因为我已将其转换为 as.POSIXct
或 as.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'
更新:
现在没有警告(检查 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
即可。
试试这个:
看这里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"))
我有一些价格数据(在这个问题的末尾),我正在尝试使用以下方法绘制 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"))
plot的预期效果会和下图差不多(意思是根据时间是不是周期,我们设置不同的背景颜色),但是我报错了,我不知道从哪里来的因为我已将其转换为 as.POSIXct
或 as.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'
更新:
现在没有警告(检查 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
即可。
试试这个: 看这里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"))