问题
I am using this code to download data from yahoo finance
and plot some stocks against the S&P500 after normalising the adjusted
prices.
The following code returns;
Ra <- c("NFLX") %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
Rb <- "SPY" %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
stock_returns_daily <- Ra
benchmark_returns_daily <- Rb
RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]
RaRb %>%
ggplot(aes(x = date)) +
geom_line(aes(y = normalise_series(adjusted.x)-1), linetype = "dashed") +
geom_line(aes(y = normalise_series(adjusted.y)-1), color = "red") +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
#facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
This figure (Which is the normalised Netflix stock price against the S&P500) :
This looks intuitive and correct to me, both start at the origin 0
, however when I try to add in other stocks AMZN
, FB
, GOOG
and NFLX
and also uncommenting the facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
I do not get the same plot any more. I use the same code and it gives me two different outputs.
Ra <- c("AMZN","FB","GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
Rb <- "SPY" %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
stock_returns_daily <- Ra
benchmark_returns_daily <- Rb
RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]
RaRb %>%
ggplot(aes(x = date)) +
geom_line(aes(y = normalise_series(adjusted.x) -1), color = "red") +
geom_line(aes(y = normalise_series(adjusted.y) -1), linetype = "dashed") +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
Giving me the following;
Now NFLX
is negative and gives me a different plot.
回答1:
To answer my own question thanks to a comment from @Noah in this question and to some guidance from @MrFlick in a question I posted here.
The following code seems to get what I want.
Ra <- c("AMZN","FB","GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
Rb <- "SPY" %>%
tq_get(get = "stock.prices",
from = "2013-01-01",
to = "2016-12-31")
stock_returns_daily <- Ra
benchmark_returns_daily <- Rb
RaRb <- left_join(stock_returns_daily, benchmark_returns_daily, by = c("date" = "date"))
normalise_series <- function(xdat) xdat / coredata(xdat)[1]
RaRb <- RaRb %>%
group_by(symbol) %>%
select(symbol, date, adjusted.x, adjusted.y) %>%
mutate(adj.x = normalise_series(adjusted.x)) %>%
mutate(adj.y = normalise_series(adjusted.y))
RaRb %>%
ggplot(aes(x = date)) +
geom_line(aes(y = adj.x -1), color = "red") +
geom_line(aes(y = adj.y -1), linetype = "dashed") +
labs(title = "Daily Stock Prices",
x = "", y = "Adjusted Prices", color = "") +
facet_wrap(~ symbol, ncol = 2, scales = "free_y") +
scale_y_continuous(labels = scales::dollar) +
theme_tq() +
scale_color_tq()
Which is this output:
The NFLX
is now identical to the first plot in the original message.
来源:https://stackoverflow.com/questions/51330838/obtaining-different-results-when-using-facet-wrap-in-ggplot