我已经在R中从Netfilx下载了股票价格数据:
library(tidyquant)
library(timetk)
library(ggplot2)
library(tibble)
library(dplyr)
library(corrplot)
library(tidyr)
netflix <- tq_get("NFLX",
from = '2009-01-01',
to = "2018-03-01",
get = "stock.prices")
netflix %>%
ggplot(aes(x = date, y = adjusted)) +
geom_line() +
ggtitle("Netflix since 2009") +
labs(x = "Date", "Price") +
scale_x_date(date_breaks = "years", date_labels = "%Y") +
labs(x = "Date", y = "Adjusted Price") +
theme_bw()但现在我想画一张类似于这张图的图纸。我该怎么做呢?enter image description here
发布于 2021-02-02 19:23:18
也许你可以从这个开始。以百分比计算提款将取决于您想要使用的统计数据。这里只是一个总值的例子和一条带有平均下降的线。
# add drawdown to each date
nfdd <- netflix %>% arrange(date) %>% mutate(drawdown=adjusted-lag(adjusted))
# calculate mean drawdown
meandd <- mean(nfdd$drawdown[nfdd$drawdown<=0], na.rm = TRUE)
# change drawdown >=0 to 0
nfdd2 <- nfdd %>% mutate(drawdown = ifelse(drawdown>=0, 0, drawdown))
meandd2 <- mean(nfdd2$drawdown[nfdd2$drawdown<=0], na.rm = TRUE)
# plot
nfdd2 %>%
ggplot(aes(x = date, y = drawdown)) +
geom_line(color="blue", alpha=0.7) +
ggtitle("Netflix drawdown since 2009") +
labs(x = "Date", "Drawdown") +
scale_x_date(date_breaks = "years", date_labels = "%Y") +
labs(x = "Date", y = "Drawdown") +
scale_y_continuous(limits = c(-10, 0)) +
geom_hline(yintercept=meandd2, linetype="dashed", color="red", size=1)+
theme_bw()

https://stackoverflow.com/questions/66007617
复制相似问题