r - ChartSeries - 如何将绘图 x 标签设置为数据框中的原始日期

标签 r quantmod

当我使用 quantmod::chartSeries 绘制图表时,如何使绘图 x 标签格式与 data.frame 中的原始格式相同?感谢! 下面的代码,图表x标签不正确

test_data <- data.frame(mydate=as.Date(c('2013-1-1','2023-1-6','2023-1-20')),
                       open = c(1,7,8),
                       high=c(7,10,9),
                       low= c(1,3,4),
                       close=c(1.5,6,7.5))

quantmod::chartSeries(ts(test_data))

最佳答案

library(quantmod)
library(xts)

# Create the data.frame
test_data <- data.frame(mydate = as.Date(c('2013-1-1', '2023-1-6', '2023-1-20')),
                        open = c(1, 7, 8),
                        high = c(7, 10, 9),
                        low = c(1, 3, 4),
                        close = c(1.5, 6, 7.5))

# Convert the data.frame to an xts object
test_xts <- xts(test_data[, -1], order.by = test_data$mydate)

# Plot the chart
chartSeries(test_xts, name = "Test Chart")

关于r - ChartSeries - 如何将绘图 x 标签设置为数据框中的原始日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75887917/

相关文章:

r - 将文件从 R 保存到 SharePoint 文件夹

R:ifelse 条件 -> 替换为条件为真的最后一个值

r - 如何使用 Quantmod 查询 Yahoo 是否存在股票代码

r - 保存到 CSV 时日期信息消失

r - 使用 dplyr、自定义函数或 purr 的多个条件 if-else

regex - 在 Sweave 表达式中查找 R 代码的正则表达式

r - 如何更快地渲染 ggplot2+gganimate+ggflags

r - chartSeries 灰线

r - 如何将滚动四分位数应用于 R 中的 xts 时间序列?

r - csv 中的时间序列顺序错误