smparlow
smparlow

Reputation: 1

Trying to plot a column in R

I am trying to plot a column I created from the original data set called "Daily Changes" but this is not working and I do not know why. I created "Daily Changes" using a loop in #2. #3 is how I am trying to plot this.

#libraries 
pacman::p_load(dplyr, fpp3, GGally, tsibbledata)
theme_set(theme_classic())
#1
AMZN_2018 <- gafa_stock %>%
  filter(Date >= "2018-01-01" & Date <= "2018-12-31" & Symbol == "AMZN")
print(AMZN_2018)
#2
AMZN_2018 %>%
  group_by(Symbol) %>%
  arrange(Date) %>%
  mutate("Daily Changes" = Close - lag(Close, default = first(Close)))
#3
AMZN_Plot <- autoplot("Daily Changes") + ylab("Daily Change") + xlab("Date") +
  ggtitle("Daily AMZN Stock Change 2018")
AMZN_Plot

Upvotes: 0

Views: 42

Answers (1)

Shane Smith
Shane Smith

Reputation: 96

It looks like your step 2 is just outputting, not assigning the output back to anything. Try using the assignment pipe operator, %<>% instead of %>% in the first part of the pipe. As @zephryl notes, you'll also need the {magrittr} package:

library(magrittr) 
 
AMZN_2018 %<>%
  group_by(Symbol) %>%
  arrange(Date) %>%
  mutate("Daily Changes" = Close - lag(Close, default = first(Close)))

Upvotes: 2

Related Questions