I am working with two data frames
. In data1
there are 3 stock symbols, AAPL
, GOOG
and NFLX
in data2
there is another benchmark index, say the S&P500
.
I have the following code which I am using a package called tidyquant
:
Ra_annual_returns %>%
ggplot(aes(x = date, y = yearly.returns, fill = symbol)) +
geom_bar(stat = "identity") +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
scale_y_continuous(labels = scales::percent) +
labs(title = "FB, AMZN, NFLX, GOOG Annual Returns",
subtitle = "Get annual returns quickly with tq_transmute",
y = "Annual Returns", x = "") +
facet_wrap(~ symbol, ncol = 2) +
theme_tq() +
scale_fill_tq()
What I am trying to do is to overlay the yearly data2
bar_chart onto the current ggplot
I have, so I can see the difference between the stock returns against the benchmark but I am having trouble since data1
is in long format and data2
is not.
Data1:
Ra_annual_returns <- structure(list(symbol = c("AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AAPL", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "GOOG", "NFLX",
"NFLX", "NFLX", "NFLX", "NFLX", "NFLX"), date = structure(c(14974,
15338, 15705, 16070, 16435, 16595, 14974, 15338, 15705, 16070,
16435, 16595, 14974, 15338, 15705, 16070, 16435, 16595), class = "Date"),
yearly.returns = c(0.507219845299458, 0.255580319466791,
0.399062561777016, 0.258354130986155, 0.509186792546526,
0.163772585970537, -0.0523015585522321, 0.0874286460637179,
0.0951849907027951, 0.584311213430311, -0.059656111290302,
0.00605243426685909, 2.28534031413613, -0.605634621513944,
0.336267932007559, 2.97634734878121, -0.0721405726683247,
0.894411639167131)), row.names = c(NA, -18L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), .Names = c("symbol", "date",
"yearly.returns"), vars = "symbol", drop = TRUE, indices = list(
0:5, 6:11, 12:17), group_sizes = c(6L, 6L, 6L), biggest_group_size = 6L, labels = structure(list(
symbol = c("AAPL", "GOOG", "NFLX")), row.names = c(NA, -3L
), class = "data.frame", vars = "symbol", drop = TRUE, .Names = "symbol"))
Data 2:
Rb_annual_returns <- structure(list(date = structure(c(14974, 15338, 15705, 16070,
16435, 16595), class = "Date"), yearly.returns = c(0.0976698601525789,
0.0261384119933952, 0.152962677798244, 0.262436498950858, 0.178441518781995,
0.0288720123502142)), .Names = c("date", "yearly.returns"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
EDIT: I have the data in the following format: So I am trying to plot yearly.returns.x
and overlay yearly.returns.y
using facet_wrap
symbol.
# A tibble: 16 x 4
# Groups: symbol [?]
symbol date yearly.returns.x yearly.returns.y
<chr> <date> <dbl> <dbl>
1 FB 2013-12-31 0.952 0.222
2 FB 2014-12-31 0.428 0.178
3 FB 2015-12-31 0.341 0.0547
4 FB 2016-12-30 0.0993 0.150
5 AMZN 2013-12-31 0.550 0.222
6 AMZN 2014-12-31 -0.222 0.178
7 AMZN 2015-12-31 1.18 0.0547
8 AMZN 2016-12-30 0.109 0.150
9 NFLX 2013-12-31 3.00 0.222
10 NFLX 2014-12-31 -0.0721 0.178
11 NFLX 2015-12-31 1.34 0.0547
12 NFLX 2016-12-30 0.0824 0.150
13 GOOG 2013-12-31 0.550 0.222
14 GOOG 2014-12-31 -0.0597 0.178
15 GOOG 2015-12-31 0.450 0.0547
16 GOOG 2016-12-30 0.0171 0.150
Data:
RaRb <- structure(list(symbol = c("FB", "FB", "FB", "FB", "AMZN", "AMZN",
"AMZN", "AMZN", "NFLX", "NFLX", "NFLX", "NFLX", "GOOG", "GOOG",
"GOOG", "GOOG"), date = structure(c(16070, 16435, 16800, 17165,
16070, 16435, 16800, 17165, 16070, 16435, 16800, 17165, 16070,
16435, 16800, 17165), class = "Date"), yearly.returns.x = c(0.951785785714286,
0.427630267973275, 0.34145101287302, 0.0992738257491372, 0.549842649332266,
-0.221770859359719, 1.17783148681492, 0.109455648638337, 3.00141285726741,
-0.0721405726683247, 1.34378371699354, 0.082357110046086, 0.549547256602323,
-0.059656111290302, 0.449568196562607, 0.0170514467567242), yearly.returns.y = c(0.22178112867623,
0.178441518781995, 0.0547092831470988, 0.150127297343264, 0.22178112867623,
0.178441518781995, 0.0547092831470988, 0.150127297343264, 0.22178112867623,
0.178441518781995, 0.0547092831470988, 0.150127297343264, 0.22178112867623,
0.178441518781995, 0.0547092831470988, 0.150127297343264)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L), .Names = c("symbol",
"date", "yearly.returns.x", "yearly.returns.y"), vars = "symbol")