I have some price data (in the end of this question) and I am trying to plot the geom_candlestick
chart using the following:
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"))
The expected effect of plot will be similar to the figure below (which means based on time is in or out period, we set different background colours), but I get an error I didn't understand where it comes from since I have convert it to as.POSIXct
or as.Date
.
Someone could help to deal with this issue? Thanks.
Error: Invalid input: time_trans works with objects of class POSIXct only
Data:
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"))
EDIT:
Running output from @Marco_CH's code:
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'
CodePudding user response:
Update:
Now without warnings (check if TZ is correct, "CST" doesn't work) and with only needed items in legend.
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'))
Output:
PS:
If you don't need the part with floor_date, just change floor_date(xstart, "day")
to xstart
.
CodePudding user response: