Set UP

library(tidyverse)
library(quantmod)
library(plotly)
library(tidyquant)

Data

Analzing Apple and Walmart

# Import stock prices
stocks <- tq_get(c("AAPL", "WMT"),
                 get = "stock.prices",
                 from = "2016-01-01",
                 to = "2017-01-01")
stocks
## # A tibble: 504 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2016-01-04  25.7  26.3  25.5  26.3 270597600     24.2
##  2 AAPL   2016-01-05  26.4  26.5  25.6  25.7 223164000     23.5
##  3 AAPL   2016-01-06  25.1  25.6  25.0  25.2 273829600     23.1
##  4 AAPL   2016-01-07  24.7  25.0  24.1  24.1 324377600     22.1
##  5 AAPL   2016-01-08  24.6  24.8  24.2  24.2 283192000     22.2
##  6 AAPL   2016-01-11  24.7  24.8  24.3  24.6 198957600     22.6
##  7 AAPL   2016-01-12  25.1  25.2  24.7  25.0 196616800     22.9
##  8 AAPL   2016-01-13  25.1  25.3  24.3  24.3 249758400     22.3
##  9 AAPL   2016-01-14  24.5  25.1  23.9  24.9 252680400     22.8
## 10 AAPL   2016-01-15  24.0  24.4  23.8  24.3 319335600     22.3
## # … with 494 more rows

Plots for Walmart and Apple

g <- ggplot(data = stocks) + 
  geom_point(mapping = aes(x = volume, y = adjusted, color = symbol))

g
Revenue by Category

Revenue by Category

Interactive Plot

ggplotly(g)

Static Plot

g <- ggplot(data = stocks) + 
  geom_point(mapping = aes(x = volume, y = adjusted, color = symbol))

g
Revenue by Category

Revenue by Category

Apply the dplyr verbs learned in chapter five

Filter rows

stocks %>% filter(adjusted > 24)
## # A tibble: 392 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2016-01-04  25.7  26.3  25.5  26.3 270597600     24.2
##  2 AAPL   2016-03-15  26.0  26.3  26.0  26.1 160270800     24.1
##  3 AAPL   2016-03-16  26.2  26.6  26.1  26.5 153214000     24.4
##  4 AAPL   2016-03-17  26.4  26.6  26.2  26.5 137682800     24.4
##  5 AAPL   2016-03-18  26.6  26.6  26.3  26.5 176820800     24.4
##  6 AAPL   2016-03-21  26.5  26.9  26.3  26.5 142010800     24.4
##  7 AAPL   2016-03-22  26.3  26.8  26.3  26.7 129777600     24.6
##  8 AAPL   2016-03-23  26.6  26.8  26.5  26.5 102814000     24.5
##  9 AAPL   2016-03-24  26.4  26.6  26.2  26.4 104532000     24.4
## 10 AAPL   2016-03-28  26.5  26.5  26.3  26.3  77645600     24.2
## # … with 382 more rows
filter(stocks, open > 26.5)
## # A tibble: 371 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2016-03-18  26.6  26.6  26.3  26.5 176820800     24.4
##  2 AAPL   2016-03-23  26.6  26.8  26.5  26.5 102814000     24.5
##  3 AAPL   2016-03-30  27.2  27.6  27.2  27.4 182404400     25.3
##  4 AAPL   2016-03-31  27.4  27.5  27.2  27.2 103553600     25.1
##  5 AAPL   2016-04-01  27.2  27.5  27.0  27.5 103496000     25.4
##  6 AAPL   2016-04-04  27.6  28.0  27.6  27.8 149424800     25.6
##  7 AAPL   2016-04-05  27.4  27.7  27.4  27.5 106314800     25.3
##  8 AAPL   2016-04-06  27.6  27.7  27.3  27.7 105616400     25.6
##  9 AAPL   2016-04-07  27.5  27.6  27.0  27.1 127207600     25.0
## 10 AAPL   2016-04-08  27.2  27.4  27.0  27.2  94326800     25.0
## # … with 361 more rows

Arrange rows

arrange(stocks, desc(date), high, low)
## # A tibble: 504 × 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2016-12-30  29.2  29.3  28.9  29.0 122345200     27.1
##  2 WMT    2016-12-30  69.1  69.4  68.8  69.1   6889500     61.7
##  3 AAPL   2016-12-29  29.1  29.3  29.1  29.2  60158000     27.4
##  4 WMT    2016-12-29  69.2  69.5  69.1  69.3   4298400     61.8
##  5 AAPL   2016-12-28  29.4  29.5  29.0  29.2  83623600     27.4
##  6 WMT    2016-12-28  69.9  70    69.3  69.3   4875700     61.9
##  7 AAPL   2016-12-27  29.1  29.5  29.1  29.3  73187600     27.5
##  8 WMT    2016-12-27  69.3  69.8  69.2  69.7   4435700     62.2
##  9 AAPL   2016-12-23  28.9  29.1  28.9  29.1  56998000     27.3
## 10 WMT    2016-12-23  69.4  69.8  69.4  69.5   4803900     62.1
## # … with 494 more rows

Select columns

select(stocks, symbol:close)
## # A tibble: 504 × 6
##    symbol date        open  high   low close
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>
##  1 AAPL   2016-01-04  25.7  26.3  25.5  26.3
##  2 AAPL   2016-01-05  26.4  26.5  25.6  25.7
##  3 AAPL   2016-01-06  25.1  25.6  25.0  25.2
##  4 AAPL   2016-01-07  24.7  25.0  24.1  24.1
##  5 AAPL   2016-01-08  24.6  24.8  24.2  24.2
##  6 AAPL   2016-01-11  24.7  24.8  24.3  24.6
##  7 AAPL   2016-01-12  25.1  25.2  24.7  25.0
##  8 AAPL   2016-01-13  25.1  25.3  24.3  24.3
##  9 AAPL   2016-01-14  24.5  25.1  23.9  24.9
## 10 AAPL   2016-01-15  24.0  24.4  23.8  24.3
## # … with 494 more rows

Add colums

mutate(stocks,
       daygain = open - close) %>%
# Select day gain

select(symbol:close, daygain) %>%

mutate(stocks, 
       fluctuation = high - low) %>%
# Select day fluctuation
    
select(symbol:close, fluctuation, daygain)
## # A tibble: 504 × 8
##    symbol date        open  high   low close fluctuation daygain
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>       <dbl>   <dbl>
##  1 AAPL   2016-01-04  25.7  26.3  25.5  26.3       0.843 -0.685 
##  2 AAPL   2016-01-05  26.4  26.5  25.6  25.7       0.860  0.760 
##  3 AAPL   2016-01-06  25.1  25.6  25.0  25.2       0.625 -0.0350
##  4 AAPL   2016-01-07  24.7  25.0  24.1  24.1       0.925  0.558 
##  5 AAPL   2016-01-08  24.6  24.8  24.2  24.2       0.587  0.398 
##  6 AAPL   2016-01-11  24.7  24.8  24.3  24.6       0.430  0.110 
##  7 AAPL   2016-01-12  25.1  25.2  24.7  25.0       0.463  0.148 
##  8 AAPL   2016-01-13  25.1  25.3  24.3  24.3       0.973  0.732 
##  9 AAPL   2016-01-14  24.5  25.1  23.9  24.9       1.19  -0.390 
## 10 AAPL   2016-01-15  24.0  24.4  23.8  24.3       0.587 -0.233 
## # … with 494 more rows

Sumarize with groups

mutate(stocks,
       daygain = open - close) %>%
# Select day gain

select(symbol:close, daygain) %>%

mutate(stocks, 
       fluctuation = high - low) %>%
# Select day fluctuation
    
    select(symbol:close, fluctuation, daygain)%>%
mutate(stocks,
       daygain = open - close) %>%
# Select day gain

select(symbol:close, daygain) %>%

mutate(stocks, 
       fluctuation = high - low) %>%
# Select day fluctuation

summarise(stocks, daygain, fluctuation, avepricegain = mean(daygain, na.rm = TRUE))%>%

select(symbol:close, daygain, fluctuation, avepricegain)
## # A tibble: 504 × 9
##    symbol date        open  high   low close daygain fluctuation avepricegain
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>       <dbl>        <dbl>
##  1 AAPL   2016-01-04  25.7  26.3  25.5  26.3 -0.685        0.843      -0.0449
##  2 AAPL   2016-01-05  26.4  26.5  25.6  25.7  0.760        0.860      -0.0449
##  3 AAPL   2016-01-06  25.1  25.6  25.0  25.2 -0.0350       0.625      -0.0449
##  4 AAPL   2016-01-07  24.7  25.0  24.1  24.1  0.558        0.925      -0.0449
##  5 AAPL   2016-01-08  24.6  24.8  24.2  24.2  0.398        0.587      -0.0449
##  6 AAPL   2016-01-11  24.7  24.8  24.3  24.6  0.110        0.430      -0.0449
##  7 AAPL   2016-01-12  25.1  25.2  24.7  25.0  0.148        0.463      -0.0449
##  8 AAPL   2016-01-13  25.1  25.3  24.3  24.3  0.732        0.973      -0.0449
##  9 AAPL   2016-01-14  24.5  25.1  23.9  24.9 -0.390        1.19       -0.0449
## 10 AAPL   2016-01-15  24.0  24.4  23.8  24.3 -0.233        0.587      -0.0449
## # … with 494 more rows

Group Stocks

    mutate(stocks,
       daygain = open - close) %>%
     mutate(stocks,
           avepricegain = mean(daygain)) %>%
    
    select(symbol, date, close, open, daygain, avepricegain) %>%
    
    group_by(symbol) %>%
    
   summarize(count = n(), 
              date = date, na.rm = TRUE,
              daygain = daygain, na.rm = TRUE) %>%
        # Plot
    ggplot(mapping = aes(x = date, y = daygain, color = symbol)) + geom_point(aes(size = count) + geom_smooth(se = FALSE))