我有以下数据:
library(dplyr)
set.seed(122)
df <- as_tibble(rlnorm(1260, meanlog = 0.06, sdlog = 0.20))
date <- rep(c("Jan", "Feb", "Mär", "Apr", "Mai", "Jun",
"Jul", "Aug", "Sep", "Okt", "Nov", "Dez"), 5)
这些都应该是每天1260倍的价格用一年时间= 252天和1月= 21天。
现在,我想画一个线图与日常价格在y轴个月x轴。 下面的代码被适配形式这个线程作图的时间序列数据时的日期的列不存在 :
library(tidyverse)
df %>%
as.data.frame() %>%
rename(price = 1) %>%
mutate(rnames = rownames(.)) %>%
ggplot(aes(x = as.numeric(rnames), y = price,
group = rep(1:5, times=1, each=252))) +
geom_line() +
labs(title = "Stock Price Chart", y = "Price", x = "date") +
scale_x_continuous(breaks = seq(1, 1260, by = 21), labels = date)
不过,我略微改变了我的df
通过插入一个新的第一行与值1
。
df <- rbind(df[0,],c(1),df[1:nrow(df),])
这被认为是在t = 0的起始价格。 不幸的是,代码现在不工作了。 是否有一个快速修复这个?