1

I'm trying to convert some simple data into a form I thought ggplot2 would accept.

I snag some simple stock data and now I just want to plot, later I want to plot say a 10-day moving average or a 30-day historical volatility period to go with it, which is I'm using ggplot.

I thought it would work something like this line of pseudocode

ggplot(maindata)+geom_line(moving average)+geom_line(30dayvol)

library(quantmod)
library(ggplot2)
start = as.Date("2008-01-01")
end = as.Date("2019-02-13")
start
tickers = c("AMD")
getSymbols(tickers, src = 'yahoo', from = start, to = end)

closing_prices = as.data.frame(AMD$AMD.Close)

ggplot(closing_prices, aes(y='AMD.Close'))

But I can't even get this to work. The problem of course appears to be that I don't have an x-axis. How do I tell ggplot to use the index column as a. Can this not work? Do I have to create a new "date" or "day" column?

This line for instance using the Regular R plot function works just fine

plot.ts(closing_prices) 

This works without requiring me to enter a hard x-axis, and produces a graph, however I haven't figured out how to layer other lines onto this same graph, evidently ggplot is better so I tried that.

Any advice?

1 Answer 1

1

as.Date(rownames(df)) will get you the rownames and parse it as a date. You also need to specify a geom_line()

library(quantmod)
library(ggplot2)
start = as.Date("2008-01-01")
end = as.Date("2019-02-13")
start
tickers = c("AMD")
getSymbols(tickers, src = 'yahoo', from = start, to = end)

closing_prices = as.data.frame(AMD$AMD.Close)

ggplot(closing_prices, aes(x = as.Date(rownames(closing_prices)),y=AMD.Close))+
  geom_line()

Edit

Thought it would be easier to explain in the answers as opposed to the comments.

ggplot and dplyr have two methods of evaluation. Standard and non standard evaluation. Which is why in ggplot you have both aes and aes_(). The former being non standard evaluation and the later being standard evaluation. In addition there is also aes_string() which is also standard evaluation.

How are these different?

Its easy to see when we explore all the methods,

#Cleaner to read, define every operation in one step
#Non Standard Evaluation
closing_prices%>%
  mutate(dates = as.Date(rownames(.)))%>%
  ggplot()+
  geom_line(aes(x = dates,y = AMD.Close))

#Standard Evaluation

closing_prices%>%
  mutate(dates = as.Date(rownames(.)))%>%
  ggplot()+
  geom_line(aes_(x = quote(dates),y = quote(AMD.Close)))

closing_prices%>%
  mutate(dates = as.Date(rownames(.)))%>%
  ggplot()+
  geom_line(aes_string(x = "dates",y = "AMD.Close"))

Why are there so many different ways of doing the same thing? In most cases its okay to use non standard evaluation. However if we want to wrap these plots in functions and dynamically change the column to plot based on function parametrs passed as strings. It is helpful to plot using the aes_ and aes_string.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked! But i don't understand why at all. So what is the x-axis in this case? I did rownames(closing_prices) and it only gave me the "AMD.Close" so I only have 1 column to work with. What is ggplot assigning as the x-axis,it seems like your way it assigns "AMD.Close" since the date number has no as the name which is what I want the Y-variable to be here right?
Added more of an explanation. I believe the main issue was using the quote "AMD.Close" instead of AMD.Close. This is because of standard and non standard evaluation. I edited the answer showing the different methods. @jed Bartlet

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.