-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization.R
More file actions
26 lines (23 loc) · 1005 Bytes
/
visualization.R
File metadata and controls
26 lines (23 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# obtain the stocks data and filter it by date
ProcterGamble <- getSymbols("PG",return.class="data.frame",from="2021-01-01")
ProcterGamble <- PG%>%
mutate(Date=as.Date(row.names(.)))%>%
select(Date,PG.Close)%>%
rename(Close=PG.Close)%>%
mutate(Company="P&G")
Johnson <- getSymbols("JNJ",return.class="data.frame",from="2021-01-01")
Johnson <- JNJ %>%
mutate(Date=as.Date(row.names(.)))%>%
select(Date,JNJ.Close)%>%
rename(Close= JNJ.Close)%>%
mutate(Company="Johnson&Johnson")
# Let's bind these two time series together
Time_series <- rbind(ProcterGamble, Johnson)
# After binding it into a dataframe, here's how you can visualize it:
ggplot(Time_series,
aes(x=Date, y= Close, color=Company)) +
geom_line(size=1.5) + geom_smooth() +
scale_y_continuous(labels = scales::dollar)+
scale_x_date(date_breaks = '1 month',labels = date_format("%b-%y")) +
labs(title = "S&P 500 Stocks",subtitle="Health sectors vs Consumer Staples Stocks",y="Close Price") +
theme_minimal()