我有一些價格資料(在這個問題的最后),我正在嘗試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'
uj5u.com熱心網友回復:
更新:
現在沒有警告(檢查 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.
uj5u.com熱心網友回復:
試試這個:見這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396752.html
